J.Mishra J.Mishra - 17 days ago 5
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 "1.2.3.4 server1.example.com gluster1" >> /etc/hosts
echo "5.6.7.8 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

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>

Example:

docker commit c3f279d17e0a  svendowideit/testimage:version3

Full Reference:

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

Create a new image from a container's changes

Options:
  -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.

Comments