Docker Containers - Mirror Management, Port Mapping, Container Interconnection

Keywords: Linux Docker CentOS Apache Nginx

Layer of docker image

_Each instruction in the Dockerfile creates a new mirror layer;
_Mirror layers 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 operation instructions;
Execute instructions when the container starts;

Create a mirror using Dockerfile and run it in a container

dockerfile operation instructions

1, based on existing mirrors

[root@localhost ~]# docker pull centos   ##Download Mirror
[root@localhost ~]# docker create -it centos /bin/bash ##Creating containers based on centos images
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

2, based on local templates

[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

3, 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 .  ##Perform 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 containers are generated at this time
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost apache]# docker run -d -p 1234:80 httpd:centos   ##Create maps, create containers
34c424efdab9e381116de697c4971200b1564b1e38644407cc58d5ba8923a0ea
[root@localhost apache]# docker ps -a  ##Container open, 1234 is external port, 80 is 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
##Access with a browser

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

1. Public warehouses

##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

2. Private warehouses

[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 and 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 Containers
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 automatically mount 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 Label
[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 from 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.

1, Port Mapping

[root@localhost ~]# docker run -d -P nginx  ##Randomly specified port
[root@localhost ~]# docker ps -a  ##View Containers
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

[root@localhost ~]# docker run -d -p 32000:80 nginx  ##Specify Port
##Use browser to access port 32000

2, Containers are interconnected (using centos mirroring)

[root@localhost ~]# docker run -itd -P --name web1 centos /bin/bash   ##Create a web1 container
87c58af3100fbc112bf344a421942dd53451c0c663b697a55a8d410868f314bf
[root@localhost ~]# docker run -itd -P --name web2 --link web1:web1 centos /bin/bash
##Create a 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 the web2 container
[root@7a84075802b5 /]# ping web1  ##pingweb1 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

Thank you for reading!

Posted by nadeemshafi9 on Fri, 03 Jan 2020 20:18:01 -0800