Mirror Management, Port Mapping, Container Interconnection for Docker Containers

Layer of docker image

  • Each instruction in the Dockerfile creates a new mirror layer;
  • The mirror layer will be cached and reused;
  • When the instructions of the Dockerfile are modified, the copied file changes, or the variables specified when the image is built are different, the corresponding mirror layer cache will fail.
  • When the mirror cache of a layer fails, the mirror cache after it fails.
  • The mirror layer is unchanged. If you add a file in one layer and delete it in the next layer, it will still be included in the mirror

docker mirror

  • Is the standard format for application Publishing
  • Supports the operation of a docker container

Creation method of docker image

  • Create based on existing mirrors
  • Create based on local template
  • Create based on dockerfile

Create based on existing mirrors

Package the programs and running environments running inside the container to generate a new image

docker commit [options] container ID/name repository name: [label]
-m: Description information
 -a: Author information
 -p: Stop the container running during the build process

Create based on local template

  • Generate a new image by importing operating system template files
  • Import as local mirror using wget command
  • View local image information after successful import

Create based on Dockerfile

  • Dockerfile is a file made up of a set of instructions
  • Four parts of the Dockerfile structure
    • Basic Mirror Information
    • Maintainer Information
    • Mirror Operations Instructions
    • Execute instructions at container startup
  • Create a mirror using Dockerfile and run it in a container

dockerfile operation instructions

Create based on existing mirrors

[root@localhost ~]# docker pull centos //download mirror
[root@localhost ~]# Docker create-it centos/bin/bash //Create container based on CentOS image
30d395e63fc32b9dcf96029869f40a8002990f689410cca2660af4056ed2614f
[root@localhost ~]# Docker PS-A //view container information
CONTAINER ID    IMAGE   COMMAND     CREATED     STATUS      PORTS               NAMES
30d395e63fc3        centos              "/bin/bash"         7 seconds ago       Created                                 inspiring_germain
[root@localhost ~]# Docker commit-m "new" -a "daoke" 30d395e63fc3 daoke:centos //Package the programs and running environments running inside the container to generate a new image
sha256:66d76f9225b94ce6156db953bd16c384f74067f981d45bee99340f3a965506d3
[root@localhost ~]# docker images //view mirror
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
daoke               centos              66d76f9225b9        10 seconds ago      220MB
centos              latest              0f3e07c0138f        3 months ago        220MB

Create based on local template

[root@localhost ~]# Mount.cifs //192.168.100.3/LNMP-C7/mnt//Mount the local template on Linux
Password for root@//192.168.100.3/LNMP-C7:  
[root@localhost ~]# Cd/mnt //Switch directory to/mnt  
[root@localhost docker]# ls
debian-7.0-x86-minimal.tar.gz
[root@localhost mnt]# cat debian-7.0-x86-minimal.tar.gz | docker import - daoke:new//Create a mirror based on a local template
sha256:487145d2411f0440c50fd93d0e8a9e27610d2de745a25d06955f21c80e65753a
[root@localhost mnt]# docker images //view mirror
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
daoke               new                 487145d2411f        8 seconds ago       215MB
centos              latest              0f3e07c0138f        3 months ago        220MB

Create based on dockefile file

[root@localhost ~]# mkdir apache //Create a directory
[root@localhost ~]# cd apache/
[root@localhost apache]# vim Dockerfile //Write a dockerfile
FROM centos                                   //Base Mirror Based
MAINTAINER The porject <xu>                //Maintain mirrored user information
RUN yum -y update                           //Mirror Operations Instructions Install Apache Software
RUN yum -y install httpd                       //Install Apache Service               
EXPOSE 80                                 //Open port 80
ADD index.html /var/www/html/index.html     //Copy web address homepage file
ADD run.sh /run.sh                       //Copy execution script to mirror
RUN chmod 755 /run.sh
CMD ["/run.sh"]                           //Execute script when starting container
[root@localhost apache]# vim run.sh //Edit run.sh script
#!/bin/bash
rm -rf /run/httpd/*              //Clear Cache
exec /usr/sbin/apachectl -D FOREGROUND            //Execute apache
[root@localhost apache]# Echo "this is test web" > index.html //Create page information
[root@localhost apache]# ls
Dockerfile  index.html  run.sh
[root@localhost apache]# Docker build-t httpd:centos. //Execute create mirror
[root@localhost apache]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              b267aaf2c395        22 seconds ago      401MB
[root@localhost apache]# Docker PS-A //No container generated at this time
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost apache]# Docker run-d-p 1234:80 httpd:centos//Create mapping, create container
34c424efdab9e381116de697c4971200b1564b1e38644407cc58d5ba8923a0ea
[root@localhost apache]# Docker PS-A //container open, 1234 is an external port, 80 is an internal port
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
34c424efdab9        httpd:centos        "/run.sh"           9 seconds ago       Up 7 seconds        0.0.0.0:1234->80/tcp   great_williamson

Public and Private Warehouses

  • As more mirrored logs are created, there needs to be a place to store the mirrors, which is the warehouse.There are two main warehouses: public warehouse and private warehouse.The most convenient way is to use the public warehouse to upload and download images. Downloading the images in the public warehouse does not require registration, but uploading requires registration: Public warehouse web address

Public warehouse

//Doker account registration required
//The httpd:centos image will be created.Upload to the public warehouse you just applied for:
docker tag httpd:centos xu/httpd:centos
docker push xu/httpd:centos

Private warehouse

[root@localhost ~]# docker pull registry //download registry image
[root@localhost ~]# vim /etc/docker/daemon.json
{
    "insecure-registries": ["192.168.13.128:5000"],         //Specify warehouse address and port number
    "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com "]//Mirror Acceleration
}
[root@localhost ~]# systemctl stop docker //stop docker, open docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# Docker create-it registry/bin/bash //create registry mirror container
209dadd90f5c555ba328fae5763a61ae5fe4489acc4bfb945a99bb2307a9f139
[root@localhost ~]# Docker PS-A //view container
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
209dadd90f5c        registry            "/entrypoint.sh /bin..."   4 seconds ago       Created                                           admiring_dewdney
34c424efdab9        httpd:centos        "/run.sh"                13 minutes ago      Exited (137) 35 seconds ago                       great_williamson
[root@localhost ~]# docker start 209dadd90f5c //open container
209dadd90f5c
[root@localhost ~]# Docker run-d-p 5000:5000-v/data/registry:/tmp/registry registry //create mapping ports and data volumes, host bureau/data automounts container-focused/tmp
fd4185499dfa29f1a1133f59b706a5524572ae3f22140137214ab4c8212ea8a4
[root@localhost ~]# docker images //Take a look at the current image
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              b267aaf2c395        17 minutes ago      401MB
centos              latest              0f3e07c0138f        3 months ago        220MB
registry            latest              f32a97de94e1        10 months ago       25.8MB
[root@localhost ~]# docker tag httpd:centos 192.168.13.128:5000/httpd //modify tag
[root@localhost ~]# docker push 192.168.13.128:5000/httpd  ##Upload Mirror
[root@localhost ~]# Curl-XGET http://192.168.13.128:5000/v2/_catalog //Get Private Warehouse List
{"repositories":["httpd"]}
[root@localhost ~]# docker pull 192.168.13.128:5000/httpd //download via private repository

Docker Network Communication

  • docker provides a mechanism to map container ports to host and container interconnection to provide network services to containers.

Port Mapping

  • Docker provides a port mapping mechanism to provide services in containers to external networks. Essentially, it maps the ports of hosts to containers so that external networks access the ports of hosts to access the services in containers.
    [root@localhost ~]# Docker run-d-P nginx //Randomly specified port
    [root@localhost ~]# Docker PS-A //view container
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                    NAMES
    bcd11c99804e        nginx               "nginx -g 'daemon of..."   13 seconds ago      Up 13 seconds                 0.0.0.0:32768->80/tcp

    Use browser to access port 32768

Containers are interconnected (using centos mirroring)

[root@localhost ~]# Docker run-itd-P --name web1 centos/bin/bash //create web1 container
87c58af3100fbc112bf344a421942dd53451c0c663b697a55a8d410868f314bf
[root@localhost ~]# Docker run-itd-P --name web2 --link web1:web1 centos/bin/bash //create web2 connection web1 container
7a84075802b5689912c323196b5af398fb5912316efda014921c0e23d3e9cdd2
[root@localhost ~]# Docker PS-A //view container information
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                    NAMES
7a84075802b5        centos              "/bin/bash"              6 seconds ago       Up 5 seconds                                           web2
87c58af3100f        centos              "/bin/bash"              42 seconds ago      Up 41 seconds                                          web1
[root@localhost ~]# Docker exec-it 7a84075802b5/bin/bash //enter web2 container
[root@7a84075802b5 /]# Ping web1 //ping web1 to see if they are interconnected
PING web1 (172.17.0.5) 56(84) bytes of data.
64 bytes from web1 (172.17.0.5): icmp_seq=1 ttl=64 time=0.090 ms
64 bytes from web1 (172.17.0.5): icmp_seq=2 ttl=64 time=0.089 ms

Tags: Linux Docker CentOS Apache vim

Posted on Sun, 09 Feb 2020 14:14:03 -0800 by py343