Article Directory
Docker image overview
I have written some theories about Docker and the theory about mirroring in the previous blog. If you need it, you can directly read the previous Weibo.
Here is the link to the previous Weibo, click me to jump! !
Three ways to make Docker images
- Create based on an existing image
- Create based on local template
- Based on the Dockerfile
, each instruction in the Dockerfile will create a new mirror layer. The
mirror layer will be cached and reused.
When the Dockerfile instructions are modified, the copied file changes, or the variables specified when building the mirror are different, the corresponding mirror layer is cached It will be invalidated. After
a certain layer of mirroring cache fails, the subsequent mirroring layer caches will be invalidated. The
mirroring layer is immutable. If you add a file to one layer and then delete it in the next layer, the mirroring remains Will contain the article
Dockerfile
Dockerfile is a file composed of a set of instructions. The
Dockerfile structure is divided into four parts:
- Basic image information
- Maintainer information
- Mirror operation instructions
- Specify instructions when the container starts
Show it directly!
Mirror establishment
1. Create based on an existing image
Package the program and operating environment running in the container into a new image
docker commit option Container ID/name warehouse name: label
Common options:
-m description information
-a author information
-p stop the operation of the container during the generation process
[root@5centos ~]# docker create -it centos:8 /bin/bash ##创建容器
d61a1f121f3a48ed81ef00da86bb7c573f81235a1fcffb6115e439fd64c328bd
[root@5centos ~]# docker ps -a ##查看ID
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d61a1f121f3a centos:8 "/bin/bash" 30 seconds ago Created wizardly_varahamihira
[root@5centos ~]# docker commit -m "基于镜像建" -a "橘籽酱" d61a1f121f3a oracentos:8 ##基于已有容器镜像创建
sha256:cd067ed5ce2254d10b1d7f96b97ba371cfac18899e53b1d39594ac56dd21f73b
[root@5centos ~]# docker images ##查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
oracentos 8 cd067ed5ce22 36 seconds ago 237MB
2233466866/centos8 latest a2f2e369e78e 6 months ago 237MB
centos 8 a2f2e369e78e 6 months ago 237MB
2. Create based on local template
Mirror can be generated by importing the system template file. The template can be downloaded from the OPENVZ open source project. The
address is: https://wiki.openvz.org/Download/template/precreated
or using wget to download: wget http://download.openvz.org/ template/precreated/debian-7.0-x86-minimal.tar.gz
I have a centos8 mirror under /opt
[root@5centos opt]# ls
centos8 containerd rh
[root@5centos opt]# cat centos8 |docker import - os8:cent
sha256:9997abab01f04c169446e89555a155bd36f040f261134a1740c78a794cba9e3e
[root@5centos opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
os8 cent 9997abab01f0 22 seconds ago 245MB
oracentos 8 cd067ed5ce22 8 minutes ago 237MB
2233466866/centos8 latest a2f2e369e78e 6 months ago 237MB
centos 8 a2f2e369e78e 6 months ago
Three, create based on Dockerfile
Use Dockerfile to create a httpd image and run it (not for anything else, just because apahce's httpd is easy to install)
Dockerfile is a file composed of a set of instructions. The
Dockerfile structure is divided into four parts:
- Basic image information
- Maintainer information
- Mirror operation instructions
- Specify instructions when the container starts
[root@5centos opt]# mkdir /apache
[root@5centos opt]# cd /apache
[root@5centos apache]# vim Dockerfile
FROM centos:8 ##基于的基础镜像
MAINTAIER Ora <CN-Ora> ##维护人员信息
RUN yum -y update ##升级所有包同时也升级软件和系统bai内核du
RUN yum -y install httpd ##安装httpd
EXPOSE 80 ##开启内部端口80
ADD index.html /var/www/html/index.html ##导入文件
ADD run.sh /run.sh ##同上
RUN chmod +x /run.sh ##加权
CMD ["/run.sh"] ##启动容器时启动脚本
[root@5centos apache]# echo "<h1>This is Ora's Web</h1>" > index.html
[root@5centos apache]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/* ##清除容器内的httpd启动
exec /usr/sbin/apachectl -D FOREGROUND ##启动apache
[root@5centos apache]# ls
Dockerfile index.html run.sh
[root@5centos apache]# docker build -t httpd:centos . ##创建镜像,记得空格点
Sending build context to Docker daemon 4.096kB
Step 1/9 : FROM centos:8
---> a2f2e369e78e
Step 2/9 : MAINTAINER Ora <CN-Ora>
---> Running in dff28da90a0d
Removing intermediate container dff28da90a0d
---> 438c17c20473
Step 3/9 : RUN yum -y update
---> Running in bff3e9d792f1
……省略部分……
Successfully tagged httpd:centos
View mirror
[root@5centos apache]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos 6b85ffdb4c45 23 seconds ago 616MB
Run this image
[root@5centos apache]# docker run -d -p 1080:80 httpd:centos ##-p 指定端口号,-P 随机端口号'
a84372f77bc29944addfc436ea6246e399d72d1de123272e759bb04256d6790c
[root@5centos apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a84372f77bc2 httpd:centos "/run.sh" 36 seconds ago Up 34 seconds 0.0.0.0:1080->80/tcp keen_hellman
Docker builds a private warehouse
Private warehouse setup steps
- Download the registry image
- The client sets the daemon.json file and specifies the location of the private warehouse
- Generate registry container and open port 5000
- Mirror tagging
- Upload image, docker push
- Download the image, docker pull
Download the registry image
[root@5centos /]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete
47112e65547d: Pull complete
46bcb632e506: Pull complete
c1cc712bcecd: Pull complete
3db6272dcbfa: Pull complete
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
The client sets the daemon.json file and specifies the location of the private warehouse
[root@5centos /]# vim /etc/docker/daemon.json
{
"insecure-registries": ["20.0.0.5:5000"], ##切记逗号别忘了,registry默认端口为5000
"registry-mirrors": ["https://xh6uo4bd.mirror.aliyuncs.com"]
}
[root@5centos /]# systemctl restart docker
Generate registry container and open port 5000
[root@5centos /]# docker create -it registry /bin/bash
4485837c9052ccc202b343d15ad92dcce29018c842639b80389fde7d5c4697d9
[root@5centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4485837c9052 registry "/entrypoint.sh /bin…" 4 seconds ago Created optimistic_gould
a84372f77bc2 httpd:centos "/run.sh" 12 minutes ago Exited (137) 50 seconds ago keen_hellman
[root@5centos /]# docker run -d -p 5000:5000 -v /opt:/opt registry
447a57de666748033cc1155e5e76bc4dff9c4c674e0dbcfa8da02aa5b5681a1f
Mirror tagging
[root@5centos /]# docker tag httpd:centos 20.0.0.5:5000/httpd
[root@5centos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
20.0.0.5:5000/httpd latest 6b85ffdb4c45 16 minutes ago 616MB
httpd centos 6b85ffdb4c45 16 minutes ago 616MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
Upload image
[root@5centos /]# docker push 20.0.0.5:5000/httpd
The push refers to repository [20.0.0.5:5000/httpd]
61e6c1eb3010: Pushed
4f96d076e862: Pushed
d9d346bd3bd3: Pushed
51693b2732fd: Pushed
3909c87a602a: Pushed
2f00c0cf81bb: Pushed
0683de282177: Pushed
latest: digest: sha256:f7cbad7005013d3ef1958c86bf7c11f83ddb465f4a7ab922cc6827bbf0b66d44 size: 1783
[root@5centos /]#
Download mirror test
[root@5centos /]# curl -XGET http://20.0.0.5:5000/v2/_catalog
{
"repositories":["httpd"]} ##获取私有库镜像列表
[root@5centos /]# docker pull 20.0.0.5:5000/httpd
Using default tag: latest
latest: Pulling from httpd
Digest: sha256:f7cbad7005013d3ef1958c86bf7c11f83ddb465f4a7ab922cc6827bbf0b66d44
Status: Image is up to date for 20.0.0.5:5000/httpd:latest
20.0.0.5:5000/httpd:latest