Dockfile and Docker data volume

One, Docker image

1.1, three ways to build a mirror

■ dockerfile
■ Create image based on existing image container
■ Build based on local template

1.2, the layering of docker images

■ Each instruction in the Dockerfile will create a new image layer
■ The image layer will be cached and reused
■ When the instructions of the Dockerfile are modified, the copied file changes, or the variable specified when building the image is different, the corresponding image Layer cache will be invalidated
■ After a certain layer of mirrored cache fails, the subsequent mirrored layer caches will be invalidated.
■ The mirrored layer is immutable. If you add a file to one layer and then delete it in the next layer, then The file will still be included in the image
Insert picture description here

二、Dockerfire

2.1, dockerfile operation instructions

Insert picture description here
Note: The difference between ADD and COPY
● COPY: copy only
● ADD: copy, decompress, and the operable object is not only a file but also a URL

Note: The difference between CMD and ENTRYPOINT
● CMD: The CMD command is customized by us. It is an executable program when the container is running. If there are multiple CMD commands in the dockerfile, only the last one will take effect.
● ENTRYPOINT: refers to the system being started. The first executor that runs by default when

2.2, use dockerfile to create an apache image

[root@docker opt]# mkdir apache
[root@docker opt]# cd apache/
[root@docker apache]# ls
[root@docker apache]# vim Dockerfile
FROM centos                #基于的基础镜像
MAINTAINER apache    #维护镜像的用户信息
RUN yum -y update   #镜像操作指令安装apache软件
RUN yum -y install httpd
EXPOSE 80                        #开启80端口
ADD index.html /var/www/html/index.html        #复制网站首页文件
ADD run.sh /run.sh                                               #将执行脚本复制到镜像中
RUN chmod 755 /run.sh
CMD ["/run.sh"]                #启动容器时执行脚本
[root@docker apache]# vim index.html
[root@docker apache]# vim run.sh
[root@docker apache]# cat index.html 
Hello world!
[root@docker apache]# cat run.sh 
#! /bin/bash
rm -rf /run/httpd/*           #删除apache缓存
exec /usr/sbin/apachectl -D FOREGROUND   #exec执行,apachectl -D守护
[root@docker apache]# docker build -t httpd:centos .  #生成镜像,.表示本地目录

Insert picture description here

[root@docker apache]# docker run -d -p 1216:80 httpd:centos  # -p:表示指定的端口映射
   -P:表示随机映射的端口
[root@docker apache]# docker ps -a

Insert picture description here
Insert picture description here
Insert picture description here

2.3. Create an image based on an existing image container

[root@docker ~]# docker images
[root@docker ~]# docker run -itd nginx:latest /bin/bash
[root@docker ~]# docker ps -a

Insert picture description here

Third, the establishment of private warehouses

3.1, the difference between registry and harbor

registry: character interface
harbor: own web page

3.2. Example

[root@docker ~]# docker pull registry

Insert picture description here

[root@docker ~]# vim /etc/docker/daemon.json

Insert picture description here

[root@docker ~]# systemctl restart docker
[root@docker ~]# docker create -it registry /bin/bash
[root@docker ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
#宿主机的/data/registry自动创建挂载容器中的/tmp/registry

Insert picture description here

[root@docker ~]# curl -XGET http://192.168.140.20:5000/v2/_catalog
#获取私有仓库列表

Insert picture description here

[root@docker ~]# docker tag nginx:latest 192.168.140.20:5000/nginx
#打标签

Insert picture description here

[root@docker ~]# docker push 192.168.140.20:5000/nginx   #上传到仓库
[root@docker ~]# curl -XGET http://192.168.140.20:5000/v2/_catalog

Insert picture description here

[root@docker ~]# docker images

Insert picture description here

[root@docker ~]# docker pull 192.168.140.20:5000/nginx   #进行本地下载
[root@docker ~]# docker images

Insert picture description here
Summary: registry

  • pull download
  • First change the configuration file: daemon.json docking with private warehouse
  • Run the operation of the registry
    user
  • Tag
  • push upload
  • pull

Four, docker data volume and data volume container

4.1, docker data volume

■ 数据卷是一个提供容器使用的特殊目录
■ 数据卷是宿主机和容器间目录的挂载
■ 创建数据卷
docker run -d -v /data1 -v /data2 --name webhttpd:centos
■ 挂载主机目录作为数据卷
docker run -d -v /var/www:/data1 --name web-1httpd:centos
 -v表示将宿主机的目录和容器的目录实现共享
[root@docker ~]# docker run -v /var/www:/data1 --name web1 -it centos:7 /bin/bash
[root@1c13c7f2d3be /]# cd /data1/
[root@1c13c7f2d3be data1]# touch test1
[root@1c13c7f2d3be data1]# ll

Insert picture description here

[root@docker ~]# cd /var/   #另起一个窗口进行验证
[root@docker var]# cd www
[root@docker www]# ll

Insert picture description here

4.2, data volume container

■ 数据卷容器是一个容器中的目录和另一个容器中的目录进行挂载
[root@docker ~]# docker run --name web10 -v /data1 -v /data2 -it centos:7 /bin/bash
另起窗口进行验证
[root@docker ~]# docker run -it --volumes-from web10 --name dc1 centos:7 /bin/bash
[root@f9d06e0697f8 /]# cd data1/
[root@f9d06e0697f8 data1]# touch 1.txt
[root@f9d06e0697f8 data1]# cd ..
[root@f9d06e0697f8 /]# cd data2/
[root@f9d06e0697f8 data2]# touch 2.txt

Insert picture description here

Five, container interconnection (using centos mirroring)

[root@docker ~]# docker run -itd -P --name b1 centos:7 /bin/bash
c927e250afc9cbea2a2d80719a5ddce28ae4b762a62ab39b3c2cbbbce76c5e22   #创建并运行容器取名b1,端口号自动映射
[root@docker ~]# docker exec -it c927e250afc9 /bin/bash
[root@c927e250afc9 /]# yum -y install net-tools
[root@docker ~]# docker run -itd -P --name b2 --link b1:b1 centos:7 /bin/bash 
a8fb67c2c153ecefad53b3a284a1b11715c074a29109f2c03fa2ae29db6d860b
[root@docker ~]# docker exec -it a8fb67c2c153 /bin/bash
[root@a8fb67c2c153 /]# yum -y install net-tools
[root@c927e250afc9 /]# ifconfig
[root@a8fb67c2c153 /]# ifconfig

Insert picture description here

Insert picture description here

[root@a8fb67c2c153 /]# ping 172.17.0.3    

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50344814/article/details/114678952