Home >Operation and Maintenance >Docker >There are several ways to create a Docker image

There are several ways to create a Docker image

青灯夜游
青灯夜游Original
2022-02-23 18:22:3639727browse

There are two ways to create a Docker image: 1. Use the "docker commit" command to manually build the image based on the existing container; 2. Use Dockerfile to automatically build the image. The Docker program will read the instructions in the Dockerfile build file. Automatically generate images.

There are several ways to create a Docker image

The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.

Sometimes the image downloaded from the Docker image warehouse cannot meet the requirements. We can build an own image based on a basic image.

Introduction to image construction

Under what circumstances Next we need to build the image ourselves?

(1) When we cannot find an existing image, such as an application developed by ourselves

(2) We need to add specific functions to the image

docker build There are two ways to image: docker commitcommand and DockerfileBuild file

docker commit build image

Building an image based on an existing container is mainly done through docker commit command to build a new image.

There are three main steps for dockercommit construction:

  • Run the container
  • Modify the container
  • Save the container as a new image

For example, install the vim editor in the centos image and save it as a new image

(1) Run the container

[root@ken1 docker]# docker run -it centos
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a02a4930cb5d: Pull complete 
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for centos:latest

(2) Install the vim editor

vim The editor confirms that it is not installed

[root@69f501e858a6 /]# vim
bash: vim: command not found

Install it

[root@69f501e858a6 /]# yum install vim -y

(3) Save it as a new image

First check the currently running image

[root@ken1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
69f501e858a6        centos              "/bin/bash"         2 minutes ago       Up 2 minutes                            quizzical_torvalds

Use commit Save as a new image

[root@ken1 ~]# docker commit 69f501e858a6 centos-vim
sha256:42083b89a179368bc29a8f40d14f8824990183c8e4b28fd84411d440c26342e5

69f501e858a6 is the ID of the running container. You can also use the name below name.
centos-vim is the name of the new image.

Check to see if centos-vim is available. Mirror

Restart the new mirror and verify whether the vim editor can be used

You can find that the new mirror can be used with the vim editor

[root@ken1 ~]# docker run -it centos-vim 
[root@61d090898bad /]# vim
[root@61d090898bad /]# vim test

The above demonstrates how to use commit to create a new image, but docker does not recommend using this method to create an image for the following reasons:

  • This is a manual way to create an image. , the container is wrong, and the efficiency is low and the repeatability is weak
  • More importantly. The user does not know how the image is created. Is there any malicious program inside?

Dockerfile builds the image

Dockerfile is a file composed of a set of instructions. Each instruction corresponds to a command in Linux. The Docker program reads the Dockerfile The instructions in ultimately generate the image.

The first Dockerfike

Step one: Create a new directory

[root@ken1 ~]# mkdir /test

Step two: Write a Dockerfile

The name is Dockerfile, and The first D needs to be capitalized

[root@ken1 ~]# cat Dockerfile
FROM centos
RUN yum install vim -y

FROMcentos means using the centos basic image

RUN means installing the vim editor on centos

Step 3: Build the image

[root@ken1 ~]# docker build -t centos-vim2 .

-t is followed by specifying the tag name (tag) of the new image

. The last dot indicates that the docker context is the current directory. Docker will search for the Dockerfile file from the build context by default. We can also specify the location of the Dockerfile through the -f parameter

Step 4: View the image

View the image hierarchical structure

## Docker history will display the construction history of the image, which is the execution process of the Dockerfile.

Dcokerfile common instructions

1.FROM

Specify the base image

2. MAINTAINER

Set the author of the image. Can be any character

 3.COPY

Copy the file from the build context to the image

COPY supports two formats: COPY src dest and COPY ["src", "dest"]

Note: src can only brake files or directories in the build context, that is, in the same directory as the Dockerfile

 4.ADD

and COPY Similarly, copy files from the build context to the image.

The difference is that if src is an archive file (tar, zip, tgz, xz), the file will be automatically connected to dest

 5.ENV

Set the environment Variables and environment variables can be used by subsequent instructions, for example:

ENV name ken RUN echo $name

 6.EXPOSE

The process in the specified container will monitor a certain Port, Docker can expose the port

 7.VOLUME

Declares the file or directory as volume

 8.WORKDIR

is the subsequent RUN ,ENTRYPINT,ADD,COPY instructions set the current working directory in the image

 9.RUN

Run the specified command in the container

  10.CMD

容器启动时运行指定的命令

dockerfile中可以多个CMD指令,但是只要最后一个生效。CMD可以被docker run之后的参数替换

  11.ENTRYPOINT

设置容器启东市的命令

dockerfile中可以有多个ENTRYPOINT,但是只有最后一个生效。

CMD或者docker run之后的参数会被当做参数传递给ENTERYPOINT.

Dockerfile演示

下面演示一个比较全面的dockerfile

[root@ken1 test]# cat Dockerfile 
#my Dockerfile
FROM busybox
MAINTAINER ken
WORKDIR /ken
RUN touch test
COPY ["ken1","."]
ADD ["wordpress.tar.gz","."]
ENV name "ken"

注意:Dockerfile支持以#开头的注释

构建镜像

[root@ken1 test]# docker build -t myimage .
Sending build context to Docker daemon  4.281MB
Step 1/7 : FROM busybox
 ---> 3a093384ac30
Step 2/7 : MAINTAINER ken
 ---> Running in 2a73a83507ce
Removing intermediate container 2a73a83507ce
 ---> 8c3df9b3d823
Step 3/7 : WORKDIR /ken
 ---> Running in 31c6f9fe2195
Removing intermediate container 31c6f9fe2195
 ---> a458cf986072
Step 4/7 : RUN touch test
 ---> Running in e1b08ebd363c
Removing intermediate container e1b08ebd363c
 ---> 41601920009a
Step 5/7 : COPY ["ken1","."]
 ---> 2ebfa0933fca
Step 6/7 : ADD ["wordpress.tar.gz","."]
 ---> d0ad29d3aa34
Step 7/7 : ENV name "ken"
 ---> Running in fceae6e20e63
Removing intermediate container fceae6e20e63
 ---> 7efe0600e48f
Successfully built 7efe0600e48f
Successfully tagged myimage:latest

查看镜像

运行该镜像

[root@ken1 test]# docker run -it myimage
/ken # ls
ken1       test       wordpress
/ken # echo $name
ken
  •  可以发现当前工作目录为/ken,且自动创建
  • ken1是我们从docker context目录中复制过去的
  • test是使用touch创建的
  • wordpres压缩包已经被自动解压
  • $name为变量值为ken

推荐学习:《docker视频教程

The above is the detailed content of There are several ways to create a Docker image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn