第2章 搭建你的第一個Docker應用棧
QUESTION 1:docker hub pull鏡像失敗
[root@nxl-test nxl]# docker pull ubuntu
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/library/ubuntu/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fubuntu%3Apull&service=registry.docker.io: net/http: TLS handshake timeout
主要原因:
- 解析ip不可用
- dns域名沒有解析
- 訪問docker hub網速不好
Solution 1:多pull幾次;
Solution 2:解析Docker Hub IP,手動修改修改docker.io的域名。
- 下載一個big工具
yum install bind-utils
安裝完畢後,輸入命令``,顯示很多可用ip
[root@nxl-test nxl]# dig @114.114.114.114 registry-1.docker.io
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> @114.114.114.114 registry-1.docker.io
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57701
;; 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. 36 IN A 100.24.246.89
registry-1.docker.io. 36 IN A 54.152.200.115
registry-1.docker.io. 36 IN A 3.221.133.86
registry-1.docker.io. 36 IN A 52.87.94.70
registry-1.docker.io. 36 IN A 3.224.11.4
registry-1.docker.io. 36 IN A 3.210.179.11
registry-1.docker.io. 36 IN A 54.174.39.59
registry-1.docker.io. 36 IN A 54.210.105.17
;; Query time: 30 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: 六 2月 29 15:55:34 CST 2020
;; MSG SIZE rcvd: 177
從中任意選一個ip,這裏我們選擇52.87.94.70,
然後在/etc/hosts
目錄下去修改docker.io的域名解析到其它可用IP
命令如下:
vi /etc/host
34.199.77.19 registry-1.docker.io
保存後重啓docker.
systemctl restart docker
Solution 3 (dial tcp: lookup auth.docker.io: no such host)
通過命令去我們linux的dns配置 文件
vi /etc/resolv.conf
nameserver 8.8.4.4 //新增谷歌的dns域名解析地址:
nameserver localdomain //再配置一個默認解析地址(如果有請忽略)
重啓容器
systemctl restart docker
Solution 4 換成本地源
QUESTION 2:exit退出,同時關閉了正在運行的容器
Description:
- 創建容器,進入容器,
exit
退出,容器關閉; docker attach [containers]
,exit
退出,容器關閉;
[root@nxl-test nxl]# docker run -it --name redis-master redis /bin/bash
root@f1d18470a9fa:/data# exit
[root@nxl-test nxl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b7c91dbfbdf redis "docker-entrypoint.s…" 11 seconds ago Created 6379/tcp redis-slave1
f1d18470a9fa redis "docker-entrypoint.s…" About a minute ago Exited (0) 41 seconds ago redis-master
Solution 1:退出容器時,按住:Ctrl+P+Q
退出容器,容器正常在後臺運行
root@f1d18470a9fa:/data# read escape sequence
[root@nxl-test nxl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b7c91dbfbdf redis "docker-entrypoint.s…" 17 minutes ago Created 6379/tcp redis-slave1
f1d18470a9fa redis "docker-entrypoint.s…" 19 minutes ago Up 8 minutes 6379/tcp redis-master
7dc3c5282b45 portainer/portainer "/portainer" 2 days ago Exited (2) 6 hours ago happy_moore
Solution 2:創建容器時加-d
,容器後臺運行
[root@nxl-test nxl]# docker run -it -d --name redis-master1 redis /bin/bash
1c5d1ab99df3cf9957cd8b62668651d677048db30aa498a700a864571c02aa0d
Solution 3:進入正在運行的容器,用 docker exec [containers]
[root@nxl-test nxl]# docker exec -it redis-master1 /bin/bash
root@1c5d1ab99df3:/data# exit
exit
[root@nxl-test nxl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c5d1ab99df3 redis "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 6379/tcp redis-master1
f1d18470a9fa redis "docker-entrypoint.s…" 32 minutes ago Up 21 minutes 6379/tcp redis-master
7dc3c5282b45 portainer/portainer "/portainer" 2 days ago Exited (2) 7 hours ago
QUESTION 3:docker inspect --format “{{ .Volume }}” f1d1 報錯
Description
[root@nxl-test nxl]# docker inspect --format "{{ .Volumes }}" f1d18470a9fa
Template parsing error: template: :1:3: executing "" at <.volumes>: map has
使用grep
[root@nxl-test ~]# docker inspect f1d18470a9fa | grep Mounts -A 10
"Mounts": [
{
"Type": "volume",
"Name": "ebf32045389654a6fabe6b9c3dd81abd552cac849dffc529738c63a409edf796",
"Source": "/var/lib/docker/volumes/ebf32045389654a6fabe6b9c3dd81abd552cac849dffc529738c63a409edf796/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
QUESTION 4:redis 主從庫數據不一致,主庫輸入,從庫get 返回 nil
Description
redis-master info replication
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0 //沒有slave
redis-slave info replication
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:master
master_port:6379
master_link_status:down //和主節點連接不成功
...
Solution 1:關閉機器防火牆或者在防火牆裏添加端口6379/tcp
[root@nxl-test ~]# firewall-cmd --add-port=6379/tcp
FirewallD is not running //說明防火牆本身是關閉的,排除防火牆問題。
[root@nxl-test ~]# yum install iptables-services //順手安裝iptables 工具
[root@nxl-test ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Solution 2:master節點bind 127.0.0.1
註釋掉,驗證我這邊不行。
Solution 3:master節點改成bind 0.0.0.0
,成功。
將master 節點redis.conf文件中,bind 127.0.0.1
改成 bind 0.0.0.0
, kill -9
原master redis-server進程,重新開啓redis-server redis.conf
成功。
查看進程號:ps -ef | grep redis
// master node
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.18.0.3,port=6379,state=online,offset=112,lag=0
slave1:ip=172.18.0.4,port=6379,state=online,offset=112,lag=0
//slave node
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:master
master_port:6379
master_link_status:up
...
設置爲 0.0.0.0 表示都可以訪問
如果是127.0.0.1 表示localhost訪問
如果不設置從就沒有訪問主的權限
QUESTION 3:yum 安裝時出現:Loading mirror speeds from cached hostfile
Description
[root@nxl-test ~]# yum install -y install iputils-ping
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
沒有可用軟件包 install。
沒有可用軟件包 iputils-ping。
QUESTION 3:Django的BUG:ImportError: cannot import name ‘patterns’
Description
在學習Docker容器與容器雲第2版時出現的BUG:
第二章,搭建第一個Hello World程序,運行:
python manage.py makemigrations
出現
ImportError: cannot import name 'patterns'