Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override default docker container command or revert to previous container state?

Tags:

docker

I have a docker image running a wordpress installation. The image by executes the apache server as default command. So when you stop the apache service the container exits.

The problem comes after messing up the apache server config. The container cannot start and I cannot recover the image contents.

My options are to either override the command that the container runs or revert last file system changes to a previous state.

Is any of these things possible? Alternatives?

like image 337
Daniel Cerecedo Avatar asked Feb 22 '15 23:02

Daniel Cerecedo


People also ask

How do I override a docker container?

As the operator (the person running a container from the image), you can override that CMD just by specifying a new COMMAND. If the image also specifies an ENTRYPOINT then the CMD or COMMAND get appended as arguments to the ENTRYPOINT. So to do what you want you need only specify a cmd, and override using /bin/bash .

Can we override CMD in Dockerfile?

The ENTRYPOINT or CMD that you specify in your Dockerfile identify the default executable for your image. However, the user has the option to override either of these values at run time.

What is the command to gracefully stop a docker container and retain its state?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command.


2 Answers

When you start a container with docker run, you can provide a command to run inside the container. This will override any command specified in the image. For example:

docker run -it some/container bash

If you have modified the configuration inside the container, it would not affect the content of the image. So you can "revert the filesystem changes" just by starting a new container from the original image...in which case you still have the original image available.

The only way to that changes inside a container affect an image are if you use the docker commit command to generate a new image containing the changes you made in the container.

like image 197
larsks Avatar answered Sep 19 '22 21:09

larsks


If you just want to copy the contents out you can use the command below with a more specific path.

sudo docker cp containername:/var/ /varbackup/

https://docs.docker.com/reference/commandline/cli/#cp

The file system is also accessible from the host. Run the command below and in the volumes section at the bottom it should have a path to where your file system modifications are stored.
This is not a good permanent solution.

docker inspect containername

If you re-create the container later you should look into keeping your data outside of the container and linking it into the container as a virtual path when you create the container. If you link your apache config file into the container this way you can edit it while the container is not running

Managing Data in Containers
http://docs.docker.com/userguide/dockervolumes/

Edit 1: Not suggesting this as a best practice but it should work.
This should display the path to the apache2.conf on the host.
Replace some-wordpress with your container name.

CONTAINER_ID=$(docker inspect -f   '{{.Id}}' some-wordpress)
sudo find /var/lib/docker/ -name apache2.conf | grep $CONTAINER_ID
like image 43
Nathan Smith Avatar answered Sep 16 '22 21:09

Nathan Smith