How to create new docker image based on existing image?

时光怂恿深爱的人放手 提交于 2019-12-22 01:58:33

问题


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




回答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.




回答5:


I am also new in docker but what i find might be helpful.

1) whenever you write "FROM" and run docker file, docker look in his repo and first load that image. so if you have any local image you want to use in "FROM", then it should be loaded.

2) what parameter you give in "FROM" is important, as if you give repo_name or tag wrong it give the error msg. so for this run "docker images" command to see your image correct repo_name and tag.

3) now you can start your new docker file like this

FROM REPOSITORY:TAG

and it will work



来源:https://stackoverflow.com/questions/36808396/how-to-create-new-docker-image-based-on-existing-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!