Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new docker image based on existing image?

I just started using docker. I create an image using docker file. How can I create a new image from that existing image?

like image 926
Aaron Shen Avatar asked Apr 23 '16 08:04

Aaron Shen


Video Answer


2 Answers

You can create a new image by using docker command $docker build -f docker_filename . , It will first read the Dockerfile where the instructions are written and automatically build the image. The instruction in the Dockerfile contains the necessary commands to assemble an image. Once, the image is build, it will be assigned an image id. The image can be pushed to the docker registry hub. For this, the user must create an account in the docker registry hub.

An example of Dockerfile looks like this,

FROM docker/whalesay:latest RUN apt-get -y update && apt-get install -y fortunes CMD /usr/games/fortune -a | cowsay 

Here, the first instruction tells the new image will be using docker/whalesay:latest image. The second instruction will run the two commands. And the third instruction tells that when the environment is set up "fortune -a" command should run.

like image 51
Arif A. Avatar answered Oct 13 '22 22:10

Arif A.


Let's say you have a container bd91ca3ca3c8 running, and you want to create a new image after you made changes in the container. Generating another image will allow you to preserve your changes.

In that case you can run:

docker commit -p -a "author_here" -m "your_message" bd91ca3ca3c8 name_of_new_image

-p pauses the container while commit command is building the new image.

-a allows you to supply author information of the new image.

-m allows you to add a comment just as in the Git.

like image 21
nPcomp Avatar answered Oct 13 '22 20:10

nPcomp