私有仓库简易版registry建立
仓库镜像
[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
ddad3d7c1e96: Pull complete
6eda6749503f: Pull complete
363ab70c2143: Pull complete
5b94580856e6: Pull complete
12008541203a: Pull complete
Digest: sha256:bac2d7050dc4826516650267fe7dc6627e9e11ad653daca0641437abdf18df27
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
查看下载的镜像
指定私有仓库的位置
[root@localhost ~]# vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.80.10:5000"],
"registry-mirrors": ["https://i657xnlq.mirror.aliyuncs.com"]
}
创建私有仓库容器
[root@localhost ~]# docker create -it registry /bin/bash
6c38f03cf0ee7854d77e48fd66d22e4aae90ab934984523ecdd52f95eba19742
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c38f03cf0ee registry "/entrypoint.sh /bin…" 4 seconds ago Created gracious_jemison
启动registry容器
[root@localhost ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
6cbbade6496071273c715b70e35be1026db6dd8eab88ff3e8ccf0a6ee8766679
- -d 守护进程
- -p 端口 5000:5000 前者为对外提供的端口,后者为内部端口
- -v 指卷轴的挂载 ,挂在信息为 宿主机上/data/registry目录共给registry容器 的/tmp/registry 目录使用
- 构建容器的时候使用-v 挂载 外部的路径是会自动生成的。不需要先行创建
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cbbade64960 registry "/entrypoint.sh /etc…" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp affectionate_bhaskara
6c38f03cf0ee registry "/entrypoint.sh /bin…" 4 minutes ago Created gracious_jemison
下载nginx、定义标签(必须修改)
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
Status: Image is up to date for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker tag nginx:latest 192.168.80.10:5000/nginx
192.168.80.10为私有仓库所在服务器的IP ,5000为对外提供的端口 nginx为镜像名称
上传镜像
[root@localhost ~]# docker push 192.168.80.10:5000/nginx
Using default tag: latest
The push refers to repository [192.168.80.10:5000/nginx]
075508cf8f04: Pushed
5c865c78bc96: Pushed
134e19b2fac5: Pushed
83634f76e732: Pushed
766fe2c3fc08: Pushed
02c055ef67f5: Pushed
latest: digest: sha256:61191087790c31e43eb37caa10de1135b002f10c09fdda7fa8a5989db74033aa size: 1570
查看私有仓库信息
[root@localhost ~]# curl -XGET http://192.168.80.10:5000/v2/_catalog
{"repositories":["nginx"]}
删除原有的nginx镜像,尝试从私有仓库中下载
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat new 60c44f97a1b8 24 hours ago 964MB
registry latest 1fd8e1b0bb7e 7 weeks ago 26.2MB
centos 7 8652b9f0cb4c 6 months ago 204MB
centos 7.4.1708 9f266d35e02c 2 years ago 197MB
从私有仓库中下载
[root@localhost ~]# docker pull 192.168.80.10:5000/nginx
Using default tag: latest
latest: Pulling from nginx
69692152171a: Pull complete
30afc0b18f67: Pull complete
596b1d696923: Pull complete
febe5bd23e98: Pull complete
8283eee92e2f: Pull complete
351ad75a6cfa: Pull complete
Digest: sha256:61191087790c31e43eb37caa10de1135b002f10c09fdda7fa8a5989db74033aa
Status: Downloaded newer image for 192.168.80.10:5000/nginx:latest
192.168.80.10:5000/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat new 60c44f97a1b8 24 hours ago 964MB
192.168.80.10:5000/nginx latest d1a364dc548d 8 days ago 133MB
registry latest 1fd8e1b0bb7e 7 weeks ago 26.2MB
centos 7 8652b9f0cb4c 6 months ago 204MB
centos 7.4.1708 9f266d35e02c 2 years ago 197MB
Dockers数据卷和数据卷容器
- 数据卷:挂载宿主系统的存储空间
- 数据卷容器:挂载容器的存储空间(也可以实现容器之间文件的共享)
数据卷创建
格式:docker run -v 宿主机挂载点 -name 容器名 -it 镜像名
[root@localhost ~]# docker run -v /var/www:/data1 --name web11 -it centos:7 /bin/bash
[root@95e5537331bc /]# ls
anaconda-post.log data1 etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@95e5537331bc data1]# touch test1
[root@95e5537331bc data1]# ls
test1
启动另一个终端查看是否有/var/www/test1文件
[root@localhost ~]# cd /var/www/
[root@localhost /var/www]# ls
test1
数据卷容器创建
先创建可以被挂载的容器
[root@localhost ~]# docker run --name web100 -v /data1 -v /data2 -it centos:7 /bin/bash
[root@9fd8780c6ca2 /]# ls
anaconda-post.log data1 dev home lib64 mnt proc run srv tmp var
bin data2 etc lib media opt root sbin sys usr
另起一个终端,创建新的容器,指定挂载的web100容器
并在容器的/data1和/data2各新建一个文件
[root@localhost ~]# docker run -it --volumes-from web100 --name db1 centos:7 /bin/bash
[root@8b3f8e83994f /]# cd data1
[root@8b3f8e83994f data1]# touch demo01
[root@8b3f8e83994f data1]# cd ../data2
[root@8b3f8e83994f data2]# touch demo02
[root@8b3f8e83994f data2]#
在钱一个终端中查看
[root@9fd8780c6ca2 /]# cd data1
[root@9fd8780c6ca2 data1]# ls
demo01
[root@9fd8780c6ca2 data1]# cd ../data2
[root@9fd8780c6ca2 data2]# ls
demo02
端口映射
-P随机生成端口号
[root@localhost ~]# docker run -d -P httpd:latest
58dffe19aad7f2a9ffa4e9f22982d21ff06c4b2c28306716fe56146d456ed676
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58dffe19aad7 httpd:latest "httpd-foreground" 6 seconds ago Up 5 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp gallant_payne
-p指定端口号
[root@localhost ~]# docker run -d -p 333:80 httpd:latest
ad7d9969e1dc6d414c858fd34332222e4562fb92958781af828fd7114d75a4cb
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad7d9969e1dc httpd:latest "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:333->80/tcp, :::333->80/tcp admiring_lalande
容器互联
创建并运行web1容器,端口号自动映射
[root@localhost ~]# docker run -itd -P --name web1 centos:7 /bin/bash
eae9a331a7366840a58312c985f5101edd34c34bb7862e39bfe640cbf6560729
[root@localhost ~]# docker run -itd -P --name web2 --link web1:web1 centos:7 /bin/bash
c265541f2b822431d5e8ea3bd81145451104951ef374acff4acd2fedef4bbea3
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c265541f2b82 centos:7 "/bin/bash" 5 seconds ago Up 5 seconds web2
eae9a331a736 centos:7 "/bin/bash" 34 seconds ago Up 33 seconds web1
进入web2容器 ping web1
[root@localhost ~]# docker exec -it c265541f2b82 /bin/bash
[root@c265541f2b82 /]# 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.330 ms
64 bytes from web1 (172.17.0.5): icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from web1 (172.17.0.5): icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from web1 (172.17.0.5): icmp_seq=4 ttl=64 time=0.056 ms
64 bytes from web1 (172.17.0.5): icmp_seq=5 ttl=64 time=0.071 ms