官网文档:https://docs.docker.com/engine/install/ubuntu/
-
删除旧版本:
$ sudo apt-get remove docker docker-engine docker.io containerd runc
-
设置仓库:
Update the
apt
package index and install packages to allowapt
to use a repository over HTTPS:$ sudo apt-get update $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release
Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
-
可能还需要执行这个命令,还需确认一下:
echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6fM0YdN3-1624677241842)(/home/quxianglin/.config/Typora/typora-user-images/image-20210624103714889.png)]
-
安装docker引擎:
$ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io
-
添加阿里云仓库:
root@hadoop001:~# sudo mkdir -p /etc/docker root@hadoop001:~# sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://qlncxq9a.mirror.aliyuncs.com"] } EOF root@hadoop001:~# sudo systemctl daemon-reload root@hadoop001:~# sudo systemctl restart docker 效果如下:
-
如果需要添加本地私有仓库,需要先拉取一个叫registry的镜像,然后在镜像仓库中配置(关键词:insecure-registries,里面的是本地仓库的地址,例如我的是:172.17.107.38):
quxianglin@quxianglin-PC:~$ docker pull registry:2.7.1 -------------------------------------------------------------------- root@hadoop001:~# sudo mkdir -p /etc/docker root@hadoop001:~# sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://qlncxq9a.mirror.aliyuncs.com"],"insecure-registries":["172.17.107.38:5000"] } EOF root@hadoop001:~# sudo systemctl daemon-reload root@hadoop001:~# sudo systemctl restart docker
-
启动registry镜像,需要暴露5000端口,方便局域网其他电脑访问:
docker run -d -p 5000:5000 --name myregistry01 registry:2.7.1
-
先备份一个镜像名字,使用tag命令标记本地镜像,将其归入某一仓库:
语法:docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
以tomcat为例,我先下载了tomcat:9.0.46-jdk8
执行命令:docker tag tomcat:9.0.46-jdk8 172.17.107.38:5000/my_tomcat:v1注意,私有仓库镜像名字一定要有IP+端口号,有用户名的还得加上用户名,不然一会推送不上去!
root@hadoop001:~# docker tag tomcat:9.0.46-jdk8 172.17.107.38:5000/my_tomcat:v1 root@hadoop001:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx <none> 4f380adfc10f 2 days ago 133MB 172.17.107.38:5000/my_mysql latest 09361feeb475 3 days ago 447MB 172.17.107.38:5000/my_tomcat v1 7253c7ce0ab9 6 weeks ago 534MB abc/def/my_tomcat v1 7253c7ce0ab9 6 weeks ago 534MB tomcat 9.0.46-jdk8 7253c7ce0ab9 6 weeks ago 534MB registry 2.7.1 1fd8e1b0bb7e 2 months ago 26.2MB
-
推送新镜像到私有仓库:
root@hadoop001:~# docker rmi -f abc/def/my_tomcat:v1 ##将其中的一个测试标记删除 Untagged: abc/def/my_tomcat:v1 root@hadoop001:~# docker push 172.17.107.38:5000/my_tomcat:v1 这就推送到本地仓库了 The push refers to repository [172.17.107.38:5000/my_tomcat] 5b3717f6302e: Pushed fc01544aaa18: Pushed d8a1ef4a64d6: Pushed bd87f5884754: Pushed 4a64f1e3a7a1: Pushed 3fc095fab4a2: Pushed 685934357c89: Pushed ccb9b68523fd: Pushed 00bcea93703b: Pushed 688e187d6c79: Pushed v1: digest: sha256:8a1edca5c98fee77cc3f36a5544940fd5bdbbd183b42e91de1031c91f37bab70 size: 2421 root@hadoop001:~# docker images ## 查询结果 REPOSITORY TAG IMAGE ID CREATED SIZE nginx <none> 4f380adfc10f 2 days ago 133MB 172.17.107.38:5000/my_mysql latest 09361feeb475 3 days ago 447MB 172.17.107.38:5000/my_tomcat v1 7253c7ce0ab9 6 weeks ago 534MB tomcat 9.0.46-jdk8 7253c7ce0ab9 6 weeks ago 534MB
-
拉取私有仓库tomcat镜像,注意查看,这是我从第二台服务器拉取的镜像
root@hadoop002:~# docker pull 172.17.107.38:5000/my_tomcat:v1 v1: Pulling from my_tomcat d960726af2be: Pull complete e8d62473a22d: Pull complete 8962bc0fad55: Pull complete 65d943ee54c1: Pull complete da20b77f10ac: Pull complete fb6a778e6477: Pull complete ae7884f0e61b: Pull complete 5a229cc6592e: Pull complete efb3915175c2: Pull complete 805021669e73: Pull complete Digest: sha256:8a1edca5c98fee77cc3f36a5544940fd5bdbbd183b42e91de1031c91f37bab70 Status: Downloaded newer image for 172.17.107.38:5000/my_tomcat:v1 172.17.107.38:5000/my_tomcat:v1 root@hadoop002:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx <none> 4f380adfc10f 3 days ago 133MB 172.17.107.38:5000/my_mysql latest 09361feeb475 3 days ago 447MB 172.17.107.38:5000/my_tomcat v1 7253c7ce0ab9 6 weeks ago 534MB
-
查询私有仓库的镜像和镜像版本,下面命令来自我的笔记本电脑
quxianglin@quxianglin-PC:~$ curl -XGET http://192.168.1.153:5000/v2/_catalog {"repositories":["elasticsearch","java","mysql","tomcat"]} quxianglin@quxianglin-PC:~$ curl -XGET http://192.168.1.153:5000/v2/elasticsearch/tags/list {"name":"elasticsearch","tags":["7.13.1"]} quxianglin@quxianglin-PC:~$ curl -XGET http://192.168.1.153:5000/v2/mysql/tags/list {"name":"mysql","tags":["5.7.34"]} quxianglin@quxianglin-PC:~$ curl -XGET http://192.168.1.153:5000/v2/tomcat/tags/list {"name":"tomcat","tags":["9.0.46-jdk8"]} 备注: 获取镜像列表接口:http://ip:port/v2/_catalog 获取镜像的标签列表:http://ip:port/v2/image_name/tags/list