J.Mishra J.Mishra - 1 year ago 119
Bash Question

Startup script for a docker container

I have a docker container already running. I have made some configuration changes, let's say have added some host info in /etc/hosts inside the container. How do I keep the changes saved, so that next time when I open an interactive shell to the container I don't have to do the same stuff again ? For now I have created a mini script as addhosts.sh as below inside the container and have to run it every time.

echo " server1.example.com gluster1" >> /etc/hosts
echo " server2.example.com gluster2" >> /etc/hosts

This is one of the cases. Similarly, I need all my configuration to be intact. Please don't suggest for dockerfile as I'm not creating an image rather I'm just getting into the container.

Answer Source

You can commit the changes you made by:

Short Command reference:

docker commit <container id or name>  <repository name>/<your image name>:<tage aka version>


docker commit c3f279d17e0a  svendowideit/testimage:version3

Full Reference:

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change value     Apply Dockerfile instruction to the created image (default [])
      --help             Print usage
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

Then you can use docker images to view your new Image after commit.

To run a container from your new Image:

docker run -d svendowideit/testimage:version3 <optional startup command>

Another way would be creating your own image via: dockerfile, I'm Just putting it here just incase we can help others.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download