Article directory
- 1. Classification of container images
- 2. How to obtain the container image
- 3. dockerfile
1. Classification of container images
1. Operating system class
centos
ubuntu
made by itself
2. Application class
tomcat
nginx
mysql
redis
2. How to obtain the container image
1. Download directly from dockerhub
docker pull centos:latest
2. Package the file system in the operating system into a container image
3. Package the running container as a container image, that is, docker commit
[root@mylinux1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca081c0529d1 mysql:5.5 "docker-entrypoint.s…" 28 hours ago Up 2 minutes 0.0.0.0:8888->3306/tcp, :::8888->3306/tcp my_mysql
[root@mylinux1 ~]# docker commit ca081c0529d1 mysql:v1.0
[root@mylinux1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql v1.0 d404d78aa797 3 years ago 205MB
[root@mylinux1 ~]# docker run --name new_mysql -d mysql:v1.0
2fd23f7e01889fbdc74e02305c910a8d8d2ddb48f00241b83469c671d82e586f
[root@mylinux1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fd23f7e0188 mysql:v1.0 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 3306/tcp new_mysql
ca081c0529d1 mysql:5.5 "docker-entrypoint.s…" 28 hours ago Up 4 minutes 0.0.0.0:8888->3306/tcp, :::8888->3306/tcp my_mysql
4. Customize and generate container images through dockerfile
3. dockerfile
1. Introduction to dockerfile
A Dockerfile is a script that can be interpreted by the Docker program. Dockerfile is composed of instructions one by one, and has its own writing format and supported commands. When we need to specify our own additional requirements in the container image, we only need to add or modify instructions on the Dockerfile, and then generate our custom container image (image) through docker build.
2. dockerfile command
build class directive
The specified operations for building images will not be executed on the container running the image (FROM, MAINTAINER, RUN, ENV, ADD, COPY)
Set class instruction
It is used to set the image attribute
and the specified operation will be executed in the container running the image (CMD, ENTRYPOINT, USER, EXPORT, VOLUME, WORKDIR, ONBUILD)
The FROM command is used to specify the base image used to build a new image.
The FROM command must be the first command in the Dockerfile Wenjia. The
base image specified by the FROM command can be in the official remote warehouse or in the local warehouse
format: FROM <image>:<tag>
for example:FROM centos:latest
The RUN command is used to execute commands in building images, and has the following two formats
Note: From the perspective of optimization: when there are multiple commands to be executed, do not use multiple RUNs, and try to use && symbols and \ symbols to connect into a line. Because multiple RUN commands will make the image build multiple layers
(in short, it will become bloated).
CMD: Different from RUN, CMD is used to specify the command to be executed when the container starts, and RUN is used to specify the command to be executed when the image is built.
Each Dockerfile can only have one CMD command. If multiple commands are specified, only the last one will be executed.
If the user specifies the command to run when starting the container, the command specified by CMD will be overwritten.
EXPORT: Used to specify the port that the container listens to at runtime
ENV: used to specify an environment variable
ADD: used to copy files on the host to the image
COPY: Similar to the ADD command, but the source file of COPY can only be a local file
ENTRYPOINT: Very similar to CMD
VOLUME: used to map the directory in the host to the directory in the container,
only specify the mount point, and the directory mapped by the docker host is automatically generated
3. Basic composition of dockerfile
Basic image information
Maintainer information
Image operation instructions
Execute instructions when the container starts
4. Dockerfile generates container image process
5. Dockerfile generates a container image case
Dockerfile generates container image steps
Step 1: Create a folder (directory)
Step 2: Create a Dockerfile (fixed) file in the folder (directory) and write it, as well as other files
Step 3: Use the docker build command to build a mirror
Step 4: Use build The image starts the container
6. Use Dockerfile to generate Nginx container image
[root@mylinux1 dockerfile_nginx]# ls
Dockerfile index.html
[root@mylinux1 dockerfile_nginx]# cat index.html
nginx is running
[root@mylinux1 dockerfile_nginx]# vim Dockerfile
FROM centos:centos7
MAINTAINER "[email protected]"
RUN yum -y install wget
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum -y install nginx
ADD index.html /usr/share/nginx/html/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD /usr/sbin/nginx
generate mirror
[root@mylinux1 dockerfile_nginx]# docker build -t centos7-nginx:v1.0 .
[+] Building 69.6s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 405B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:centos7 25.6s
=> CACHED [1/6] FROM docker.io/library/centos:centos7@sha256:be65f488b7764ad3638f236b7b51 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 114B 0.0s
=> [2/6] RUN yum -y install wget 15.9s
=> [3/6] RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.rep 1.5s
=> [4/6] RUN yum -y install nginx 24.3s
=> [5/6] ADD index.html /usr/share/nginx/html/ 0.1s
=> [6/6] RUN echo "daemon off;" >> /etc/nginx/nginx.conf 1.2s
=> exporting to image 0.8s
=> => exporting layers 0.8s
=> => writing image sha256:337b3bac42dda5f878cb671f3306f7753a0693bbddb74388663c943d51ca20 0.0s
=> => naming to docker.io/library/centos7-nginx:v1.0 0.0s
[root@mylinux1 dockerfile_nginx]# docker images | grep "centos7-nginx"
centos7-nginx v1.0 337b3bac42dd 11 minutes ago 657MB
run container
[root@mylinux1 dockerfile_nginx]# docker run -d centos7-nginx:v1.0
62b15bc281b433f213f019d7ae9f1ef0ceb2f2d6ee98972d58029f28c548b0ab
View running containers
[root@mylinux1 dockerfile_nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62b15bc281b4 centos7-nginx:v1.0 "/bin/sh -c /usr/sbi…" 10 minutes ago Up 10 minutes 80/tcp distracted_knuth
[root@mylinux1 dockerfile_nginx]# docker inspect "62b" | address
-bash: address: 未找到命令
[root@mylinux1 dockerfile_nginx]# docker inspect "62b" | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.5",
"IPAddress": "172.17.0.5",
[root@mylinux1 dockerfile_nginx]# curl http://172.17.0.5
nginx is running
7. Dockerfile generates container image optimization method
a. Reduce mirror layering
The Dockerfile contains a variety of instructions. If it involves deployment, the most used one is the RUN command. When using the RUN command, it is not recommended to use a separate RUN command for each installation.
You can Mirror layering can be reduced.
The optimized content is as follows
b. Clean up useless data
One run forms a new layer. If it is not deleted on the same layer, no matter whether the file is deleted last, it will be brought to the next layer. Therefore, it is necessary to clean up the corresponding residual data in each layer to reduce the size of the image
.
Delete the application software package deployed during the process of generating the container image