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.

  • Docker on Windows getting “Could not locate Gemfile”
  • Loading Elasticsearch from a config file in Docker
  • Can I build a Docker container from the CLI against a remote daemon?
  • Docker volumes binding: container to host
  • How to dockerized java microservices efficiently
  • GitLab CI Docker CakePHP 2 testing doesn't work
  • 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?

  • Best practice to apply patch to a modified docker container?
  • Docker for Mac with WebStorm Node Remote Interpreter
  • Run a service automatically in a docker container
  • How to build a docker container with nix?
  • Best practice re running bash for attaching to Docker running instance
  • How do I push an image to my NON-public repo in dockerhub
  • 3 Solutions collect form web for “How to override default docker container command or revert to previous container state?”

    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/

    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

    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

    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.

    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

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.