Docker containers - image management, port mapping, container interconnection

Layered mirrored docker

 Each instruction Dockerfile creates a new image layer;
 mirror layer will be cached and reused;
 variable command when Dockerfile modified, copied files changed, or a different construct designated image, the corresponding mirror level cache will fail;
 after a layer of mirrored cache invalidation, mirrored cache layer after it will fail;
 mirror layer is constant, if you add a file in one layer, and then remove it the next layer, you mirror still contain the file

docker Mirror

是应用发布的标准格式
可支撑一个docker容器的运行

Create a method docker mirror

基于已有镜像创建
基于本地模板创建
基于dockerfile创建

Based on the existing image creation

The program running inside the container packed operating environment and generate a new image

docker commit [选项] 容器ID/名称 仓库名称:[标签]
-m:说明信息
-a:作者信息
-p:生成过程中停止容器的运行

Create a template-based local

通过导入操作系统模板文件生成新的镜像
使用wget命令导入为本地镜像
导入成功后可查看本地镜像信息

Based Dockerfile create

Dockerfile is a file consisting of a set of instructions

Dockerfile four-part structure:

基础镜像信息;
维护者信息;
镜像操作指令;
容器启动时执行指令;

Use Dockerfile create a mirror and run in a container

operation instruction dockerfile

Docker containers - image management, port mapping, container interconnection

1, created based on the existing mirror

[root@localhost ~]# docker pull centos   ##下载镜像
[root@localhost ~]# docker create -it centos /bin/bash ##基于centos镜像创建容器
30d395e63fc32b9dcf96029869f40a8002990f689410cca2660af4056ed2614f
[root@localhost ~]# docker ps -a  ##查看容器信息
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
##将容器里面运行的程序及运行环境打包生成新的镜像
sha256:66d76f9225b94ce6156db953bd16c384f74067f981d45bee99340f3a965506d3
[root@localhost ~]# docker images  ##查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
daoke               centos              66d76f9225b9        10 seconds ago      220MB
centos              latest              0f3e07c0138f        3 months ago        220MB

2, based on the local template creation

[root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/  ##将本地模板挂载到Linux上
Password for root@//192.168.100.3/LNMP-C7:  
[root@localhost ~]# cd /mnt     ##切换目录到/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
##基于本地模板创建一个镜像
sha256:487145d2411f0440c50fd93d0e8a9e27610d2de745a25d06955f21c80e65753a
[root@localhost mnt]# docker images   ##查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
daoke               new                 487145d2411f        8 seconds ago       215MB
centos              latest              0f3e07c0138f        3 months ago        220MB

3, create a file based on dockefile

[root@localhost ~]# mkdir apache  ##创建一个目录
[root@localhost ~]# cd apache/
[root@localhost apache]# vim Dockerfile   ##编写一个dockerfile文件
FROM centos                                 ##基于的基础镜像
MAINTAINER The porject <xu>       ##维护镜像的用户信息
RUN yum -y update                           ##镜像操作指令安装Apache软件
RUN yum -y install httpd                    ##安装Apache服务               
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@localhost apache]# vim run.sh   ##编辑run.sh脚本
#!/bin/bash
rm -rf /run/httpd/*   ##清除缓存
exec /usr/sbin/apachectl -D FOREGROUND  ##执行apache
[root@localhost apache]# echo "this is test web" > index.html   ##创建页面信息
[root@localhost apache]# ls
Dockerfile  index.html  run.sh
[root@localhost apache]# docker build -t httpd:centos .  ##执行创建镜像
[root@localhost apache]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              b267aaf2c395        22 seconds ago      401MB
[root@localhost apache]# docker ps -a   ##此时没有容器生成
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost apache]# docker run -d -p 1234:80 httpd:centos   ##创建映射,创建容器
34c424efdab9e381116de697c4971200b1564b1e38644407cc58d5ba8923a0ea
[root@localhost apache]# docker ps -a  ##容器开启,1234是外部端口,80是内部端口
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
##利用浏览器进行访问

Docker containers - image management, port mapping, container interconnection

Public and private warehouses warehouse

With the increase in the mirror log is created, you need to have a local mirror of preservation, this is the warehouse. There are two main warehouses: public warehouse, private warehouse. The most convenient is to use a public warehouse upload and download images, download public warehouse image does not require registration, but is to be registered to upload: public warehouse URL

1, public warehouse

##需要注册docker账号
##将创建好的 httpd:centos 镜像。上传到刚申请的公共仓库中:
docker tag httpd:centos xu/httpd:centos
docker push xu/httpd:centos

2, private warehouse

[root@localhost ~]# docker pull registry   ##下载 registry镜像
[root@localhost ~]# vim /etc/docker/daemon.json
{
    "insecure-registries": ["192.168.13.128:5000"],   ##指定仓库地址和端口号
    "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]  ##镜像加速
}
[root@localhost ~]# systemctl stop docker   ##停止docker,开启docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker create -it registry /bin/bash  ##创建registry镜像容器
209dadd90f5c555ba328fae5763a61ae5fe4489acc4bfb945a99bb2307a9f139
[root@localhost ~]# docker ps -a  ##查看容器
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   ##开启容器
209dadd90f5c
[root@localhost ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
##创建映射端口和数据卷,宿主局的/data自动挂载容器重点的/tmp
fd4185499dfa29f1a1133f59b706a5524572ae3f22140137214ab4c8212ea8a4
[root@localhost ~]# docker images  ##查看一下当前的镜像
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  ##修改标签
[root@localhost ~]# docker push 192.168.13.128:5000/httpd  ##上传镜像
[root@localhost ~]# curl -XGET http://192.168.13.128:5000/v2/_catalog ##获取私有仓库列表
{"repositories":["httpd"]}
[root@localhost ~]# docker pull 192.168.13.128:5000/httpd  ##通过私有仓库下载

Docker Network Communications

docker 提供了映射容器端口到宿主机和容器互联机制来为容器提供网络服务。

Port Mapping

Docker port mapping mechanism to provide services within the container to provide access to external networks, essentially maps the host port to the container, such that the port of the external network to access the host can access services within the container.

1, port mapping

[root@localhost ~]# docker run -d -P nginx  ##随机指定端口
[root@localhost ~]# docker ps -a  ##查看容器
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
##利用浏览器访问32768端口

Docker containers - image management, port mapping, container interconnection

[root@localhost ~]# docker run -d -p 32000:80 nginx  ##指定端口
##利用浏览器访问32000端口

Docker containers - image management, port mapping, container interconnection

2, the container Interconnect (using centos mirror)

[root@localhost ~]# docker run -itd -P --name web1 centos /bin/bash   ##创建web1容器
87c58af3100fbc112bf344a421942dd53451c0c663b697a55a8d410868f314bf
[root@localhost ~]# docker run -itd -P --name web2 --link web1:web1 centos /bin/bash
##创建web2连接web1容器
7a84075802b5689912c323196b5af398fb5912316efda014921c0e23d3e9cdd2
[root@localhost ~]# docker ps -a   ##查看容器信息
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  ##进入web2容器
[root@7a84075802b5 /]# ping web1  ##pingweb1看是否互联互通
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

thanks for reading!

Guess you like

Origin blog.51cto.com/14080162/2463945