I just started using docker. I create an image using docker file. How can I create a new image from that existing image?
问题:
回答1:
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.
回答2:
In order to create a new image from an existing image all you have to do is to specify 'FROM' for e.g:
FROM sergiu/ubuntu
MAINTAINER sergiu
回答3:
Let's say you have a container bd91ca3ca3c8
running, and you want to create an image after you made changes in the container. You may want to generate another image to preserve your changes etc.
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.
回答4:
Docker commit: Creates a new image from a container’s changes.
It can be useful to commit a container’s file changes or settings into a new image. This allows you to debug a container by running an interactive shell, or to export a working dataset to another server. Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way.