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

HTML+Sass implements HambergurMenu (hamburger menu)

A few days ago, I watched a video of a foreign gu...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...

Analysis of Sysbench's benchmarking process for MySQL

Preface 1. Benchmarking is a type of performance ...

Solve the problem of docker's tls (ssl) certificate expiration

Problem phenomenon: [root@localhost ~]# docker im...

How to avoid duplication of data when inserting in MySql batch

Table of contents Preface 1. insert ignore into 2...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Detailed explanation of EXT series file system formats in Linux

Linux File System Common hard disks are shown in ...

About Zabbix forget admin login password reset password

The problem of resetting the password for Zabbix ...

Calculation of percentage value when the css position property is absolute

When position is absolute, the percentage of its ...

In-depth study of vue2.x--Explanation of the h function

Table of contents Solution, Summarize: vue projec...

How to install pyenv under Linux

Prerequisites Need to install git Installation St...

Introduction to JavaScript strict mode use strict

Table of contents 1. Overview 1.1 What is strict ...