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.
Start an app container To do so, you will use the docker run command. You use the -d flag to run the new container in “detached” mode (in the background). You also use the -p flag to create a mapping between the host's port 3000 to the container's port 3000.
Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.
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 <[email protected]>")
-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.
Assuming you do not wish to save the changes into a new image:
docker exec -it container_id echo "1.2.3.4 server1.example.com gluster1" >> /etc/hosts
docker exec -it container_id echo "5.6.7.8 server2.example.com gluster2" >> /etc/hosts
That would connect to the container, run the command and exit without killing the container.
Notice the difference between the following:
docker exec -it container_id ... = does not kill the container.
docker run -it container_id ...= kills the container
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With