dockerservicecouchbase

Stop service running inside a docker container without stopping the whole container


I have a docker container running Couchbase, and I need to perform an operation on it that requires the couchbase-server service to temporarily stop. This is required to overwrite some configuration files within the container. Operation steps are described below.

docker exec -it "bumblebase" bash -c "systemctl stop couchbase-server"

docker exec -it "bumblebase" bash -c "echo \"{rest_port, 3456}.\" >> \"/opt/couchbase/etc/couchbase/static_config\""

docker exec -it "bumblebase" bash -c "rm -rf /opt/couchbase/var/lib/couchbase/config/config.dat"

docker exec -it "bumblebase" bash -c "systemctl start couchbase-server"

Running this gives me the following error ;

Failed to connect to bus: No such file or directory

I only found one workaround for it, from the following stackoverflow answer. However, I am not fine with modifying couchbase default image, plus the alternate image is marked as unmaintend for a while now.

I also tried this alternate command from couchbase documentation :

docker exec -it "bumblebase" bash -c "service couchbase-server stop"

But then I have another error :

couchbase-server: unrecognized service

I just want the couchbase-server service to stop within the container, is it possible ? Or is it an alternative solution to overwrite my config files ?


Solution

  • You don't usually stop processes in a container, separately from stopping the whole container itself. In this situation it's pretty common to delete and recreate the container, in fact. Correspondingly, you shouldn't use docker exec to edit files inside a container; that would get lost on a routine restart.

    If you don't already, you can copy the config file out of the container

    docker cp bumblebase:/opt/couchbase/etc/couchbase/static_config .
    

    Now you can edit the local copy of the config file, using your choice of editor.

    Finally, you can stop, delete, and recreate the container. The docker run -v option can inject individual config files into the container.

    docker stop bumblebase
    docker rm bumblebase
    docker run \
      -d \
      -p 8091-8094:8091-8094 -p 11210:11210 \
      --name bumblebase \
      -v $PWD/static_config:/opt/couchbase/etc/couchbase/static_config \
      couchbase:community-6.6.0
    

    For a couple of reasons, commands like service or systemctl basically don't work in Docker at all. Generally the server process a container runs is a foreground process that runs as the main container process; there isn't an init system or any background processes, and if the server process exits, the container will exit with it.