Daniel Cerecedo
Daniel Cerecedo

Reputation: 6207

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

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?

Upvotes: 5

Views: 6431

Answers (4)

KEMBL
KEMBL

Reputation: 624

If it requires starting a command with a set of arguments, for example

ls -al /bin

try to make it like that

docker run --entrypoint ls -it debian /bin -al

where ls goes after --entrypoint and all arguments are placed after the image name

Upvotes: 0

Nathan Smith
Nathan Smith

Reputation: 1813

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

Upvotes: 2

Javier Cortejoso
Javier Cortejoso

Reputation: 9176

There are different ways of overriding the default command of a docker image. Here you have two:

  1. If you have an image with a default CMD command, you can simply override it in docker run giving as last argument the command (with its argument) you wish to run (Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...])
  2. Create a wrapper image with BASE image the one you want to override the CMD or ENTRYPOINT. Example

    FROM my_image CMD ["my-new-cmd"]

Also, you can try to revert the changes in different ways:

  1. If you have the Dockerfile of the image you want to revert, simple rewrite the changes into Dockerfile and run again docker build process.
  2. If you don't have the Dockerfile and you built the image committing the changes, you can use docker history <IMAGE_NAME>:tag, locate the IMAGE_ID of the commit you want, and run that commit or tag that commit with the name (and tag) you wish (using -f option if you are overriding a tag name). Example:

$ docker history docker_io_package:latest

$ docker tag -f c7b38f258a80 docker_io_package:latest

Upvotes: 1

larsks
larsks

Reputation: 312243

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.

Upvotes: 3

Related Questions