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.

  • How can I connect Sendmail MTA and PHP-FPM docker containers
  • How to add more feature to a official postgres image from docker hub?
  • Building an application stack using Docker
  • hub.docker.io reports tags in the web interface but we cannot some of them using the API
  • Docker build just freezes?
  • Docker download layers sequentially
  • 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?

  • Host monitoring from a docker container
  • gcloud ping attempt failure with performing a 'docker push'
  • Error creating DigitalOcean droplet using docker-machine
  • Relationship Between a Deis Cluster and Scale command?
  • How to disable the root access of a docker container?
  • Force Docker to mount code volume in the host not the guest
  • 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/

    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
    

    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.