docker镜像
Docker镜像
1.Docker镜像介绍
在安装完docker-ce之后,默认相当于装好了docker-cli和docker-ce进程,启动docker-ce进程就算是运行起了 docker,但是此时我们并没有可以直接使用的docker私有仓库。镜像其实是装好了特定应用的一个东西,镜像是静态的,容器是动态的。镜像运行起来就是容器。这个概念类似于程序和进程,程序是静态的,进程是动态的,进程就是运行的程序。镜像只会占用磁盘空间,但是容器会占用磁盘空间的同时也会占用cpu和内存。
镜像一般是存放在镜像仓库中的,然后通过docker-cli提供的命令从仓库拉取镜像,然后将镜像保存在本地,以便后续使用。值得一提的是镜像仓库中的“镜像”都是压缩过的,压缩的目的在于减少镜像的大小,这样在进行网络传输的时候(镜像拉取)可以尽量的减少带宽的压力。然后docker的客户端拉取了镜像之后,会对镜像进行解压。
默认情况docker是需要配置镜像仓库的,一般在生产环境中会配置一个私有的镜像仓库,这样就可以保证镜像传输的安全和速度。
在Internet上有很多开源厂商给我们提供了镜像仓库(安全度需要你自己来评估),可以直接连接这些镜像仓库拉取(pull)镜像。
2.Dockerhub介绍和使用
Docker公司为了实现Docker容器技术良好的生态圈,推出了和github类似的东西,叫做Dockerhub,Dockerhub是Docker公司在Internet上推出的存放Docker镜像的公有仓库,任何人都可以在Dockerhub上创建自己的仓库,然后上传自己的镜像(早期的时候),Dockerhub目前已经推出了收费服务。
如果你想访问Dockerhub,只需要访问https://hub.docker.com/,此地址访问不需要科学上网。
安装完Docker之后默认拉取镜像的地址就是Dockerhub,如果你想拉取镜像的话就必须要科学上网,或者通过其他的方式。
3.配置Dockerhub的镜像加速器
使用阿里云提供的镜像加速方法,首先访问阿里云。直接在浏览器上访问aliyun.com,然后有账号的就直接用支付宝扫描登陆就可以了。如果没有账号就自己注册一个。
登陆成功之后,需要点击用户的控制台。
点击完后控制台之后搜索容器镜像
在容器镜像服务点进去之后,点击左侧的镜像加速器
点击完镜像加速器之后,就会告诉你如何配置不同操作系统的镜像加速方法
默认的配置镜像加速器的方法是ubuntu的,我们选择到Centos的
配置镜像加速器之前,拉取的情况(大约有10分钟了,还在1.612那个位置挣扎,让人难以忍受)
在CentOS上配置镜像加速,复制阿里云给我们提供的方法,然后直接粘贴就好了。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://5ufvppm7.mirror.aliyuncs.com”]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
4.配置镜像加速器之后拉取镜像
#再次拉取centos的镜像
[root@localhost ~]# docker pull centos
速度非常快,大约30秒左右就拉取完成了。其实从任何镜像仓库拉取镜像都分为主要的两个阶段,第一个阶段是拉取压 缩的镜像,第二个阶段是解压压缩的镜像。
[root@localhost ~]# docker images
#下载镜像过程
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Downloading [==========================================> ]
64.57MB/75.18MB
#解压镜像过程
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Extracting [=================================> ]
50.14MB/75.18MB
#拉取完镜像之后,解压之后,会对docker镜像进行校验
[root@localhost ~]# docker pull centos
#如果拉取镜像的时候没有指定tag,默认就会是latest的tag
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 6 weeks ago 209MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
5.常见的国内外镜像仓库
DockerHub镜像仓库:
google镜像仓库:
https://gcr.io/google-containers/
https://gcr.io/kubernetes-helm/
https://gcr.io/google-containers/pause
quay.io镜像仓库:
elastic镜像仓库:
https://www.docker.elastic.co/
RedHat镜像仓库:
https://access.redhat.com/containers
阿里云镜像仓库:
华为云镜像仓库:
https://console.huaweicloud.com/swr
腾讯云镜像仓库:
https://console.cloud.tencent.com/tcr
6.
配置docker的代理
有些情况下我们可能无法直接的访问到某些镜像仓库,但是有一个代理服务器可以访问到,那么如果我们想从无法访问到的镜像仓库中拉取镜像,就需要配置docker的代理来访问到对应的镜像仓库来拉取镜像。
老庚家里的环境,容器主机的地址是10.163.1.110,老庚家里有一个代理服务器地址和端口分别是192.168.199.11:10808,但是注意,我这个代理服务器的协议是socks5。docker是没办法直接使用socks5代理,所以我们需要将socks5代理转成http或者https,一般就是转换成http。
1.将socks5的代理转换成http的代理
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install privoxy
[root@localhost ~]# vim /etc/privoxy/config
[root@localhost ~]# egrep ‘^listen-address’ /etc/privoxy/config -A 1
listen-address 0.0.0.0:8118
forward-socks5t / 192.168.199.11:10808 .
[root@localhost ~]# systemctl enable privoxy --now
[root@localhost ~]# netstat -tunlp | grep 8118
tcp 0 0 0.0.0.0:8118 0.0.0.0:* LISTEN
6172/privoxy
2.配置docker使用http的代理
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/proxy.conf
sudo chmod 777 /etc/systemd/system/docker.service.d/proxy.conf
sudo echo ’
[Service]
Environment=“HTTP_PROXY=http://10.163.1.110:8118”
Environment=“HTTPS_PROXY=http://10.163.1.110:8118”
’ >> /etc/systemd/system/docker.service.d/proxy.conf
sudo systemctl daemon-reload
sudo systemctl restart docker
3.拉取镜像
关闭镜像加速器
[root@localhost ~]# mv /etc/docker/daemon.json /etc/docker/daemon.json.bak
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull gcr.io/google-containers/ubuntu
Using default tag: latest
Error response from daemon: manifest for gcr.io/google-containers/ubuntu:latest not
found: manifest unknown: Failed to fetch “latest” from request "/v2/google-
containers/ubuntu/manifests/latest".
[root@localhost ~]# docker pull gcr.io/google-containers/ubuntu:14.04
14.04: Pulling from google-containers/ubuntu
a3ed95caeb02: Pull complete
7059585c469e: Pull complete
782c76bb9e67: Pull complete
706514fbad74: Pull complete
Digest: sha256:5746b3b4974d1bd3d4ddbac0373fb71b425f13583797414ffd9d8b547d241f75
Status: Downloaded newer image for gcr.io/google-containers/ubuntu:14.04
gcr.io/google-containers/ubuntu:14.04
[root@localhost ~]# docker pull ubuntu:xenial-20210114
xenial-20210114: Pulling from library/ubuntu
4007a89234b4: Pull complete
5dfa26c6b9c9: Pull complete
0ba7bf18aa40: Pull complete
4c6ec688ebe3: Pull complete
Digest: sha256:e74994b7a9ec8e2129cfc6a871f3236940006ed31091de355578492ed140a39c
Status: Downloaded newer image for ubuntu:xenial-20210114
docker.io/library/ubuntu:xenial-20210114
[root@localhost ~]# docker pull docker.io/library/ubuntu:latest
latest: Pulling from library/ubuntu
83ee3a23efb7: Pull complete
db98fc6f11f0: Pull complete
f611acd52c6c: Pull complete
Digest: sha256:703218c0465075f4425e58fac086e09e1de5c340b12976ab9eb8ad26615c3715
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
7.Docker镜像管理
1.Docker镜像拉取
docker pull命令就是默认拉取镜像的命令,默认情况下,如果不指定docker的仓库,默认的仓库就是从dockerhub上拉取。如果指定了对应的docker仓库就从指定的仓库上拉取镜像。前提是没有配置镜像仓库的配置文件。
从dockerhub上拉取镜像
[root@localhost ~]# docker pull ubuntu:xenial-20210114
从gcr(google container registry)上拉取镜像
[root@localhost ~]# docker pull gcr.io/google-containers/ubuntu:14.04
2.Docker镜像的查看
[root@localhost ~]# docker images
3.Docker镜像删除
强烈不建议通过IMAGE ID删除镜像,有可能会出现误删除的情况,因为Docker存在repository和tag的概念,有可
能出现不同repository不同tag但是IMAGE ID是相同的情况,这样如果通过docker image rm命令删除IMAGE
ID,如果存在两个IMAGE ID相同,但是repository和tag不同的镜像就会出现一起删除的情况。
#如果不指定tag,默认就会删除latest的
[root@localhost ~]# docker image rm REPOSITORY:TAG
[root@localhost ~]# docker image rm ubuntu:xenial-20210114
[root@localhost ~]# docker images
#如果已经有一个容器使用了你想要删除的镜像,那么你需要先删除这个容器,才能删除这个镜像
[root@localhost ~]# docker ps -a
[root@localhost ~]# docker container rm stupefied_hypatia
[root@localhost ~]# docker image rm hello-world
[root@localhost ~]# docker images
#在centos-stream上安装docker
sed -i ‘s/SELINUX=enforcing/SELINUX=permissive/’ /etc/selinux/config
setenforce 0
systemctl disable firewalld --now
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-
ce.repo
yum -y install epel-release docker-ce docker-ce-cli containerd.io --allowerasing
systemctl enable docker --now
4.Docker镜像导出
docker save
#-o参数表示输出为指定的文件
[root@localhost ~]# docker save ubuntu:latest -o ubuntu-latest.tar
[root@localhost ~]# ls -lh
total 72M
- rw-------. 1 root root 1.6K Nov 28 2019 anaconda-ks.cfg
- rw-------. 1 root root 72M Jan 24 01:55 ubuntu-latest.tar
5.Docker镜像导入
docker load
#-i参数表示导入指定的文件
[root@localhost ~]# scp ubuntu-latest.tar 10.163.1.100:~/ubuntu-latest.tar
[root@stream ~]# docker load -i ubuntu-latest.tar
[root@stream ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f63181f19b2f 2 days ago 72.9MB
8.使用Dockerhub上自己创建的仓库
#首先,我们需要先理解docker的镜像仓库,docker镜像仓库的结构分为仓库本身,仓库里面的小仓库,还有tag。
我们来分析一下dockerhub的仓库构成。dockerhub的镜像仓库名称为docker.io,当我们想要从dockerhub上拉取ubuntu镜像的时候,我们就从docker.io的大仓库中找到ubuntu这个小仓库,找到ubuntu这个小仓库之后,我们需要指定我们要从ubuntu这个小仓库中拉取哪个tag(标记)个ubuntu镜像。
docker pull docker.io/library/ubuntu:latest
[root@localhost ~]# docker pull centos:centos7
centos7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:centos7
docker.io/library/centos:centos7
#一般来说我们习惯将大仓库叫做docker registry,小仓库的名字叫做repository
1.在dockerhub上创建账号
2.在dockerhub登陆自己的账号并创建小仓库(repository)
在dockerhub上不花钱的情况下,只能创建一个私有仓库,但是公有仓库的数量则不作限制(老庚没有实测过到底有没有限制),如果你想测限制,你自己测。公有仓库意味着你在这个仓库中的镜像都会被别人看到而且可以拉取。你在私有仓库中上传的镜像不会被除了你之外的人看到。这里指的仓库是repository,并不是registry。
3.修改镜像的tag
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f63181f19b2f 2 days ago 72.9MB
centos latest 300e315adb2f 6 weeks ago 209MB
centos centos7 8652b9f0cb4c 2 months ago 204MB
gcr.io/google-containers/ubuntu 14.04 fbb59e4c2529 5 years ago 188MB
[root@localhost ~]# docker tag ubuntu:latest a390133368/gls-pub:ubuntu-latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
a390133368/gls-pub ubuntu-latest f63181f19b2f 2 days ago 72.9MB
ubuntu latest f63181f19b2f 2 days ago 72.9MB
centos latest 300e315adb2f 6 weeks ago 209MB
centos centos7 8652b9f0cb4c 2 months ago 204MB
gcr.io/google-containers/ubuntu 14.04 fbb59e4c2529 5 years ago 188MB
4.使用自己的账号登陆到dockerhub并上传镜像到自己的小仓库
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don’t have a Docker ID, head over to https://hub.docker.com to create one.
Username: a390133368
Password: your-password
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store#
Login Succeeded
[root@localhost ~]# docker push a390133368/gls-pub:ubuntu-latest
The push refers to repository [docker.io/a390133368/gls-pub]
02473afd360b: Mounted from library/ubuntu
dbf2c0f42a39: Mounted from library/ubuntu
9f32931c9d28: Mounted from library/ubuntu
ubuntu-latest: digest:
sha256:3093096ee188f8ff4531949b8f6115af4747ec1c58858c091c8cb4579c39cc4e size: 943
#查看已经push的镜像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KOAl3YqY-1681109274657)(docker%E9%95%9C%E5%83%8F%2051ea0eeb0362450797a35135a00d193f/Untitled%2010.png)]
查看想要在自己的公有仓库上下载镜像应该如何操作
[root@localhost ~]# docker pull a390133368/gls-pub:ubuntu-latest
ubuntu-latest: Pulling from a390133368/gls-pub
Digest: sha256:3093096ee188f8ff4531949b8f6115af4747ec1c58858c091c8cb4579c39cc4e
Status: Downloaded newer image for a390133368/gls-pub:ubuntu-latest
docker.io/a390133368/gls-pub:ubuntu-latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f63181f19b2f 2 days ago 72.9MB
a390133368/gls-pub ubuntu-latest f63181f19b2f 2 days ago 72.9MB
centos latest 300e315adb2f 6 weeks ago 209MB
centos centos7 8652b9f0cb4c 2 months ago 204MB
gcr.io/google-containers/ubuntu 14.04 fbb59e4c2529 5 years ago 188MB
[root@localhost ~]# docker image rm a390133368/gls-pub:ubuntu-latest
Untagged: a390133368/gls-pub:ubuntu-latest
Untagged: a390133368/gls-
pub@sha256:3093096ee188f8ff4531949b8f6115af4747ec1c58858c091c8cb4579c39cc4e
[root@localhost ~]# docker image rm ubuntu:latest
Untagged: ubuntu:latest
Untagged:
ubuntu@sha256:703218c0465075f4425e58fac086e09e1de5c340b12976ab9eb8ad26615c3715
Deleted: sha256:f63181f19b2fe819156dcb068b3b5bc036820bec7014c5f77277cfa341d4cb5e
Deleted: sha256:0770b7f116f8627ec336a62e65a1f79e344df7ae721eb3e06e11edca85d3d1e7
Deleted: sha256:476e931831a5b24b95ff7587cca09bde9d1d7c0329fbc44ac64793b28fb809d0
Deleted: sha256:9f32931c9d28f10104a8eb1330954ba90e76d92b02c5256521ba864feec14009
[root@localhost ~]# docker pull a390133368/gls-pub:ubuntu-latest
ubuntu-latest: Pulling from a390133368/gls-pub
83ee3a23efb7: Pull complete
db98fc6f11f0: Pull complete
f611acd52c6c: Pull complete
Digest: sha256:3093096ee188f8ff4531949b8f6115af4747ec1c58858c091c8cb4579c39cc4e
Status: Downloaded newer image for a390133368/gls-pub:ubuntu-latest
docker.io/a390133368/gls-pub:ubuntu-latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
a390133368/gls-pub ubuntu-latest f63181f19b2f 2 days ago 72.9MB
centos latest 300e315adb2f 6 weeks ago 209MB
centos centos7 8652b9f0cb4c 2 months ago 204MB
gcr.io/google-containers/ubuntu 14.04 fbb59e4c2529 5 years ago 188MB
命令
docker ps是查看所有存活的容器
docker ps -a 是查看所有的容器,包括已经停止的容器
docker container rm 容器ID 就是删除停止的容器
docker image rm 镜像ID 就是删除镜像