Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Startup script for a docker container

Tags:

shell

docker

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.

like image 831
jagatjyoti Avatar asked Nov 18 '16 09:11

jagatjyoti


People also ask

How do I start a container in docker?

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.

How do I automatically start docker?

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.


2 Answers

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.

like image 118
Farhad Farahi Avatar answered Oct 20 '22 04:10

Farhad Farahi


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 
like image 20
elha2en Avatar answered Oct 20 '22 06:10

elha2en