私有仓库建立
建立私有仓库的步骤
(1)下载registry镜像,在字符终端
(2)客户端设置daemon.json文件,指定私有仓库位置
(3)生成registry容器,开放5000端口
(4)镜像打标签:docker tag 原镜像名 仓库IP:端口/镜像名(一定要打标签)
(5)上传镜像:docker push 仓库IP:端口/镜像名
(6)下载镜像:docker pull 仓库IP:端口/镜像名(使用docker images查看)

建立私有仓库

[root@docker ~]# docker pull registry
[root@docker ~]# vim /etc/docker/daemon.json 
{
  "insecure-registries": ["192.168.7.168:5000"],    //不同参数选项之间使用","相隔
  "registry-mirrors": ["https://syy5204b.mirror.aliyuncs.com"]
}
[root@docker ~]# systemctl restart docker
[root@docker ~]# docker run -d -p 5000:5000 -v /data/registry:/tpm/registry registry
#宿主机中的/data/registry会自动创建挂载容器中的/tpm/registry
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4e3029f01510        registry            "/entrypoint.sh /etc…"   4 seconds ago       Up 4 seconds        0.0.0.0:5000->5000/tcp   eager_northcutt
[root@docker ~]# docker pull nginx 
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              e791337790a6        About an hour ago   127MB
[root@docker ~]# docker tag nginx:latest 192.168.7.168:5000/nginx
[root@docker ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
192.168.7.168:5000/nginx   latest              e791337790a6        2 hours ago         127MB
[root@docker ~]# docker push 192.168.7.168:5000/nginx
#获取私有仓库的列表
curl -XGET http://192.168.7.168:5000/v2/_catalog
{"repositories":["nginx"]}
[root@docker ~]# docker rmi 192.168.7.168:5000/nginx:latest 
[root@docker ~]# docker pull 192.168.7.168:5000/nginx
Using default tag: latest
latest: Pulling from nginx
Digest: sha256:6b3b6c113f98e901a8b1473dee4c268cf37e93d72bc0a01e57c65b4ab99e58ee
Status: Downloaded newer image for 192.168.7.168:5000/nginx:latest
192.168.7.168:5000/nginx:latest
[root@docker ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
192.168.7.168:5000/nginx   latest              e791337790a6        2 hours ago         127MB

Docker数据卷
docker数据卷是通过挂载宿主系统的存储空间

[root@docker ~]# docker pull centos
#宿主机目录/var/www/挂载容器中的/data1
#这些目录不需要自己手动创建,系统会自动创建
[root@docker ~]# docker run -v /var/www:/data1 --name test1 -it centos /bin/bash
[root@3ea73938c9b5 /]# cd /data1/
[root@3ea73938c9b5 data1]# touch test123
#返回宿主机进行查看
[root@docker ~]# ls /var/www/
test123

Docker数据卷容器
挂载容器的存储空间

#创建数据卷容器
[root@docker ~]# docker run --name web100 -v /data1 -v /data2 -it centos /bin/bash
[root@44c8fbe8e01a /]# ls
bin    data2  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
data1  dev    home  lib64  media       opt  root  sbin  sys  usr
#新容器挂载数据卷容器web100
[root@docker ~]# docker run -it --volumes-from web100 --name db1 centos /bin/bash
[root@9b0b7f686192 /]# ls
bin    data2  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
data1  dev    home  lib64  media       opt  root  sbin  sys  usr
#在新容器创建文件,到数据卷容器可以查看到相同的内容

端口映射

#不指定端口
[root@docker ~]# docker run -d -P nginx
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                     NAMES
6dd91cea1325        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 4 seconds                  0.0.0.0:1234->80/tcp      eager_mclean
#指定端口
[root@docker ~]# docker run -d -p 1234:80 nginx
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                     NAMES
fd952499c2c5        nginx               "nginx -g 'daemon of…"   8 seconds ago       Up 7 seconds                  0.0.0.0:32768->80/tcp     infallible_beaver
  • -P:指定随机映射端口
  • -p:在固定的IP地址映射
    容器互联(使用centos镜像)
    #创建并运行容器取名web1,端口号自动映射
    [root@docker ~]# docker run -itd -P --name web1 centos /bin/bash
    #创建并运行容器取名web2,关联web1
    [root@docker ~]# docker run -itd -P --name web2 --link web1:web1 centos /bin/bash
    #在web2可以直接ping通web1
    [root@docker ~]# docker exec -it 3c0d72923de3 /bin/bash
    [root@3c0d72923de3 /]# ping web1
    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.200 ms
    64 bytes from web1 (172.17.0.5): icmp_seq=2 ttl=64 time=0.110 ms