一、安装 Docker
1.1、安装docker
# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# mv docker-ce.repo /etc/yum.repos.d/
# yum install -y docker
1.2、启动 docker 守护进程及设置开机启动。
#systemctl start docker
#systemctl stop docker
#systemctl enable docker
1.3、检查docker安装
[root@localhost ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
...
1.4、 删除所有容器
docker rm `docker ps -a -q`
-a标志列出所有容器,-q标志列出容器的ID。
1.5、删除所有镜像
docker rmi `docker images -q`
luna@luna-virtual-machine:~$ docker rmi `docker images -q`
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:fd41f8a687e94b926b45a455c1c75fb29b4d7f206a969b6a16e073efa39d2ce5
Deleted: sha256:94e814e2efa8845d95b2112d54497fbad173e45121ce9255b93401392f538499
Deleted: sha256:e783d8ee44ce099d51cbe699f699a04e43c9af445d85d8576f0172ba92e4e16c
Deleted: sha256:cc7fae10c2d465c5e4b95167987eaa53ae01a13df6894493efc5b28b95c1bba2
Deleted: sha256:99fc3504db138523ca958c0c1887dd5e8b59f8104fbd6fd4eed485c3e25d2446
Deleted: sha256:762d8e1a60542b83df67c13ec0d75517e5104dee84d8aa7fe5401113f89854d9
Untagged: hello-world:latest
Untagged: hello-world@sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
luna@luna-virtual-machine:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
1.6、删除 Docker
/var/lib/docker/ 是系统一个很重要的路径,存放docker的image,container,volume,netowrks等等重要信息。
# yum remove docker-ce
# rm -rf /var/lib/docker
三、使用Docker Registry快速搭建私有镜像仓库
3.1、Docker Registry私有仓库类似maven、gitlab私有仓库,执行docker pull xxx 将从 registry.hub.docker.com(Docker公司为我们提供的公共仓库) 这个地址下载镜像。
3.2、下载registry镜像
[root@localhost ~]# docker pull registry
Using default tag: latest
Trying to pull repository docker.io/library/registry ...
latest: Pulling from docker.io/library/registry
c87736221ed0: Pull complete
1cc8e0bb44df: Pull complete
54d33bcb37f5: Pull complete
e8afc091c171: Pull complete
b4541f6d3db6: Pull complete
Digest: sha256:3b00e5438ebd8835bcfa7bf5246445a6b57b9a50473e89c02ecc8e575be3ebb5
Status: Downloaded newer image for docker.io/registry:latest
3.3、查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/registry latest f32a97de94e1 6 weeks ago 25.8 MB
3.4 运行registry容器
[root@localhost ~]# docker run -i -t -d -v /home/data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest
d798ce4e91deaf249b369fd5db89725bcdefc39ceb00e35c1aee59acb3b8af35
3.5 查看运行的容器及停止容器(可以创建、查看、启动、重启、停止、导入、导出及删除)
[root@localhost data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d798ce4e91de registry:latest "/entrypoint.sh /e..." 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp registry
[root@localhost data]# docker stop d798ce4e91de
d798ce4e91de
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]# docker rm -f d798ce4e91de
d798ce4e91de
参数说明
-i:容器的标准输入保持打开;
-t:分配一个伪终端并绑定到容器的标准输入上;
-d:容器在后台运行;
-v:把宿主机的/home/data/registry目录绑定到容器/var/lib/registry目录(registry容器中存放镜像文件的目录),来实现数据的持久化;
-p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了;
--restart=always:这是重启的策略,容器异常退出会自动重启容器;
--name registry:创建容器命名为registry,可以随便命名;
registry:latest:这个是刚才pull下来的镜像。
3.6 查看镜像仓库中所有的镜像
[root@localhost /]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}
四、测试Registry私有镜像仓库(最好换一台机器进行push、pull镜像)
4.1 下载ubuntu镜像
luna@luna-virtual-machine:~$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
898c46f3b1a1: Pull complete
63366dfa0a50: Pull complete
041d4cd74a92: Pull complete
6e1bee0f8701: Pull complete
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest
luna@luna-virtual-machine:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 94e814e2efa8 5 weeks ago 88.9MB
4.2 为镜像打标签
docker tag ubuntu:latest 192.168.2.210:5000/ubuntu:myubuntu
格式说明:Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
ubuntu:lastest 这是源镜像,也是刚才pull下来的镜像文件;
192.168.2.210:5000/ubuntu:myubuntu:这是目标镜像,也是registry私有镜像服务器的IP地址和端口。
4.3 上传到镜像服务器
luna@luna-virtual-machine:~$ docker push 192.168.2.210:5000/ubuntu:myubuntu
The push refers to repository [192.168.2.210:5000/ubuntu]
Get https://192.168.2.210:5000/v2/: http: server gave HTTP response to HTTPS client
注意了,这是报错了,需要https的方法才能上传,我们可以修改下daemon.json来解决:
luna@luna-virtual-machine:~$ sudo vim /etc/docker/daemon.json
{
"registry-mirrors": [ "https://registry.docker-cn.com"],
"insecure-registries": ["192.168.2.210:5000"]
}
添加私有镜像服务器的地址,注意书写格式为json,有严格的书写要求,然后重启docker服务:
systemctl restart docker
luna@luna-virtual-machine:~$ docker push 192.168.2.210:5000/ubuntu
The push refers to repository [192.168.2.210:5000/ubuntu]
b57c79f4a9f3: Retrying in 1 second
d60e01b37e74: Retrying in 1 second
e45cfbc98a50: Retrying in 1 second
762d8e1a6054: Retrying in 1 second
received unexpected HTTP status: 500 Internal Server Error
如果有上面的报错了,可以先禁用registry私有镜像服务器selinux
# setenforce 0
# getenforce
也可以先删除registry,加上--privileged=true再运行
[root@localhost /]# docker run -i -t -d -v /home/data/registry:/var/lib/registry -p 5000:5000 --restart=always --privileged=true --name registry registry:latest
然后再上传
luna@luna-virtual-machine:~$ docker push 192.168.2.210:5000/ubuntu
The push refers to repository [192.168.2.210:5000/ubuntu]
b57c79f4a9f3: Pushed
d60e01b37e74: Pushed
e45cfbc98a50: Pushed
762d8e1a6054: Pushed
myubuntu: digest: sha256:f2557f94cac1cc4509d0483cb6e302da841ecd6f82eb2e91dc7ba6cfd0c580ab size: 1150
luna@luna-virtual-machine:~$ curl http://192.168.2.210:5000/v2/_catalog
{"repositories":["ubuntu"]}
4.4 测试下载镜像
拉取registry服务器上下载刚才上传的ubuntu镜像
luna@luna-virtual-machine:~$ docker pull 192.168.2.210:5000/ubuntu:myubuntu
myubuntu: Pulling from ubuntu
Digest: sha256:f2557f94cac1cc4509d0483cb6e302da841ecd6f82eb2e91dc7ba6cfd0c580ab
Status: Image is up to date for 192.168.2.210:5000/ubuntu:myubuntu
luna@luna-virtual-machine:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.2.210:5000/ubuntu myubuntu 94e814e2efa8 5 weeks ago 88.9MB
ubuntu latest 94e814e2efa8 5 weeks ago 88.9MB
搭建企业级的镜像私有仓库可以参考:
https://blog.csdn.net/qq_32523587/article/details/82879015