配置docker镜像加速器
docker的镜像基本都在国外的服务器上,拉取镜像时很容易因为网络不稳定导致拉取速度很慢甚至失败。这里我配置下国内的阿里云镜像加速器,可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器。登录阿里云获取专属的镜像加速器地址,daemon.json文件一般再在/etc/docker目录下,如果没有则新建即可。
拉取hello-world镜像
docker提供了hello-world示例镜像,我们可以通过docker search hello-world来查看。配置docker镜像加速器后我们来拉取一个hello-world镜像,通过命令:docker pull hello-world来拉取即可。
Docker拉取错误
这个是请求头超时异常,docker请求不到docker官网下载镜像。这个是DNS域名解析异常,我们需要配置下DNS来解决这个问题。Cenots通过安装bind-utils可以获取Dig工具, Dig是用来查询DNS包括NS记录,A记录,MX记录等相关信息的工具。这里我们通过命令:yum install bind-utils来安装bind-utils,然后通过dig来获取docker的DNS信息,在执行结果中选择一组解析放到本机的/etc/hosts文件里做映射。
如果配置了hosts映射还是无效,我们可以再配置下/etc/resolv.conf文件。/etc/resolv.conf是DNS的配置文件,用于设置DNS服务器的IP地址及DNS域名。用vim打开文件/etc/resolv.conf,新增谷歌的dns域名解析地址:nameserver 8.8.4.4。保存之后重启下docker,再试试docker pull hello-world拉取。
查看docker镜像
docker镜像是一个文件系统(它没有状态也不会被改变),镜像可以启动复杂的软件供用户使用。 通过命令:docker images或者docker image ls可以列出本机已有的镜像,REPOSITORY资源仓库名,TAG指镜像的标签信息,IMAGE ID:是镜像的 ID(唯一的),CREATED:指镜像最后的更新时间,SIZE:指镜像的大小。
测试HelloWorld
拉取完hello-world镜像之后我们就可以来试运行下hello-world,执行命令:docker run hello-world后Docker会先检查本地是否存在名字叫hello-world的镜像,若不存在则去docker hub上下载该镜像,如果存在则将其挂载进容器并运行该容器实例。当看到Hello from Docker!则说明hello-world运行成功,执行docker ps -a命令后可以看到控制台显示了hello-world镜像的容器信息。
总结:
这里需要注意的就是docker官方镜像容易下载失败,所以最好配置下国内的镜像源。以上内容是小编给大家分享的【Docker实战003:运行hello Word并解决报错】,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。
为了方便学习,下面附上本文用到的源码:
# mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]
//"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com"]
}
EOF
# systemctl daemon-reload
# systemctl restart docker
# docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 1303 [OK]
//*******省略********
# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
# docker pull hello-world
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
// 安装dig工具获取DNS
# yum install bind-utils
# dig @114.114.114.114 registry-1.docker.io
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> @114.114.114.114 registry-1.docker.io
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45167
;; flags: qr rd ra; QUERY: 1, ANSWER: 8, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;registry-1.docker.io. IN A
;; ANSWER SECTION:
registry-1.docker.io. 41 IN A 35.174.73.84
registry-1.docker.io. 41 IN A 52.4.20.24
registry-1.docker.io. 41 IN A 52.72.232.213
registry-1.docker.io. 41 IN A 3.218.162.19
registry-1.docker.io. 41 IN A 54.236.131.166
registry-1.docker.io. 41 IN A 52.54.232.21
registry-1.docker.io. 41 IN A 3.211.199.249
registry-1.docker.io. 41 IN A 52.5.11.128
;; Query time: 53 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: 四 10月 01 21:59:03 CST 2020
;; MSG SIZE rcvd: 177
# vi /etc/hosts
35.174.73.84 registry-1.docker.io
# vim /etc/resolv.conf
-------------------------------------------
# Generated by NetworkManager
search localdomain
nameserver 192.168.1.2
nameserver 8.8.4.4
nameserver localdomain
-------------------------------------------
# systemctl restart docker
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 9 months ago 13.3kB
# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 9 months ago 13.3kB
# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/