Docker's implementation method for building a new image based on an existing image

  • 2020-12-21 18:14:41
  • OfStack

Building new images from existing images is done through the Dockerfile documentation.

1. Create new Dockerfile document

Create a new folder under /home, the /docker/test folder specially used for testing. Create a new Dockerfile document in the folder and write the following contents in the document:


FROM  ubuntu:18.04

RUN   apt-get update
RUN   apt-get install -y vim

EXPOSE 80

In Dockerfile documents, the first keyword on each line must be capitalized.

Line 1 means that the source image of the new image is Ubuntu 18.04.

The second line is the first command executed after the new image, indicating that after the new image, first update the url of the subsequent download of various applications.

The third line is to install vim so that you can edit the script from the command line. -ES26en is to be installed automatically, otherwise the installation process will ask you to type Y/n. If you do not type, the execution will fail.

The last line 4 means exposing port 80, like the webapp port mapping in yesterday's article. If a mapping to port 5000 is performed in this image, it will fail because the port is not open.

2. Execute commands in Dockerfile directory


su root
cd docker/test
docker build -t cdl-test-0.0 .

In the last sentence, -t is followed by the specified mirror name, and the mirror name is followed by a dot to create a new image from the contents of Dockerfile in the current directory, so note that the previous cd command and the last dot of this sentence are not less!!

3. View the new image


docker images

Results:


REPOSITORY     TAG         IMAGE ID      CREATED       SIZE
cdl-test-0.0    latest       da5d6c1147a7    4 minutes ago    185MB
runoob/centos    6.7         542cf01e7692    27 minutes ago   191MB
ubuntu       16.04        52b10959e8aa    5 days ago     115MB
ubuntu       18.04        16508e5c265d    5 days ago     84.1MB
centos       6.7         f2e2f7b8308b    3 weeks ago     191MB
training/webapp   latest       6fae60ef3446    3 years ago     349MB

4. View the applications installed in the image


# Go to the new image command line 
docker run -it cdl-test-0.0 /bin/bash
# Open the vim
vim
# The installation python3.7
apt-get install python3.7

Related articles: