How to build a new image based on an existing image in Docker

How to build a new image based on an existing image in Docker

Building new images from existing images is done through Dockerfile documents.

1. Create a new Dockerfile document

Create a new folder in the /home folder specifically for testing, the /docker/test folder, and create a new Dockerfile document in the folder. Write the following content in the document:

FROM ubuntu:18.04

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

EXPOSE 80

In a Dockerfile document, the first keyword of each line must be capitalized.

The first line means that the source image of the newly created image is Ubuntu 18.04 version.

The second line is the first command executed after the new image is created, which means that after the new image is created, the URLs for downloading various applications will be updated first.

The third line installs vim, which is convenient for editing scripts in the command line later. -y is for automatic installation. Otherwise, the installation process will ask you to enter Y/n. If you do not enter it, the execution will fail.

The fourth line at the end means exposing port 80. Like the webapp port mapping in yesterday’s article, if you perform mapping to port 5000 in this image, it will fail because this port is not open to the outside world.

2. Execute the command in the directory where the Dockerfile is located

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

In the last sentence, -t is followed by the specified image name, and the image name is followed by a dot, which means that a new image is created using the content in the Dockerfile in the current directory. So please note that the dot at the end of the previous cd command and this sentence is indispensable! !

3. View the new image

docker images

result:

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

#Enter the command line of the newly created image docker run -it cdl-test-0.0 /bin/bash
#Open vim
vim
#Install python3.7
apt-get install python3.7

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker practice: error resolution when starting a container from a new image

<<:  Configure Mysql master-slave service implementation example

>>:  WeChat applet custom tabbar component

Recommend

Example of Vue implementing fixed bottom component

Table of contents 【Effect】 【Implementation method...

Why Use DOCTYPE HTML

You know that without it, the browser will use qui...

Bootstrap 3.0 study notes CSS related supplement

The main contents of this article are as follows:...

Summary of Node.js service Docker container application practice

This article will not explain the use and install...

How to implement rounded corners with CSS3 using JS

I found an example when I was looking for a way t...

Hello dialog box design experience sharing

"What's wrong?" Unless you are accus...

HTML code to add quantity badge to message button

HTML code: <a onclick="goMessage();"...

MySQL slow query: Enable slow query

1. What is the use of slow query? It can record a...

How to implement the observer pattern in JavaScript

Table of contents Overview Application scenarios ...

In-depth study of how to use positioning in CSS (summary)

Introduction to Positioning in CSS position attri...

MySQL slow query and query reconstruction method record

Preface What is a slow query and how to optimize ...

Intellij IDEA quick implementation of Docker image deployment method steps

Table of contents 1. Docker enables remote access...

Some pitfalls of JavaScript deep copy

Preface When I went to an interview at a company ...

Using cursor loop to read temporary table in Mysql stored procedure

cursor A cursor is a method used to view or proce...

Detailed explanation of mysql filtering replication ideas

Table of contents mysql filtered replication Impl...