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

Detailed explanation of fuser command usage in Linux

describe: fuser can show which program is current...

Analysis of MySQL general query log and slow query log

The logs in MySQL include: error log, binary log,...

How to add a disk in Vmware: Expand the disk

This article describes how to add or expand a dis...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...

A brief explanation of the reasonable application of table and div in page design

At the beginning of this article, I would like to ...

Understanding MySQL index pushdown in five minutes

Table of contents What is index pushdown? The pri...

Solve the problem of Docker starting Elasticsearch7.x and reporting an error

Using the Docker run command docker run -d -p 920...

Detailed explanation of Linux netfilter/iptables knowledge points

Netfilter Netfilter is a packet processing module...

HTML+CSS+JS sample code to imitate the brightness adjustment effect of win10

HTML+CSS+JS imitates win10 brightness adjustment ...

Nginx proxy axios request and precautions

Preface I recently wrote a small demo. Because I ...

In-depth analysis of MySQL database transactions and locks

Table of contents 1. Basic Concepts ACID 3.AutoCo...

Detailed explanation of the basic usage of the Linux debugger GDB

Table of contents 1. Overview 2. gdb debugging 2....

JavaScript Function Currying

Table of contents 1 What is function currying? 2 ...

Example of making XML online editor using js

Table of contents Preface The need for online XML...