[toc]mysql
[root@xaviyunserver ~]# docker create -it centos7 bash 5ab8830ee5d2cb4da2df86912275fc14806cad1f44bb24e0e54ddefa11a60532 [root@xaviyunserver ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5ab8830ee5d2 centos7 "bash" 10 seconds ago Created //建立 goofy_knuth 410e90b34976 centos7 "bash" 2 hours ago Up 2 hours dreamy_chatterjee 83a4c32c03ca centos "/bin/bash" 4 hours ago Up 4 hours nostalgic_lamport
[root@xaviyunserver ~]# docker start 5ab8830ee5d2 5ab8830ee5d2 [root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5ab8830ee5d2 centos7 "bash" 2 minutes ago Up 5 seconds goofy_knuth 410e90b34976 centos7 "bash" 2 hours ago Up 2 hours dreamy_chatterjee 83a4c32c03ca centos "/bin/bash" 4 hours ago Up 4 hours nostalgic_lamport
[root@xaviyunserver ~]# docker run -it centos bash [root@27096b136e00 /]# ls bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@27096b136e00 /]# ifconfig bash: ifconfig: command not found //這裏沒有安裝net-tool工具
好比:linux
[root@xaviyunserver ~]# docker run -itd --name centos7_1 centos7 bash 1a93298ed9db13673049b2d8d82013aa2b8e98caaa36c4d771337fc3f9bc2cea [root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a93298ed9db centos7 "bash" 4 seconds ago Up 4 seconds centos7_1 5ab8830ee5d2 centos7 "bash" 10 minutes ago Up 8 minutes goofy_knuth 410e90b34976 centos7 "bash" 2 hours ago Up 2 hours dreamy_chatterjee 83a4c32c03ca centos "/bin/bash" 4 hours ago Up 4 hours nostalgic_lamport
[root@xaviyunserver ~]# docker exec -it centos7_1 bash [root@1a93298ed9db /]#
[root@xaviyunserver ~]# docker run -itd centos bash -c "echo 123" 5ea2041ac3d24f6e44a6347b8d94328ddff02b26fa7c12f538389bc72ad9d5d4 [root@xaviyunserver ~]# docker logs 5ea2041a 123
當咱們pull一個鏡像,通常都是從官網直接拉取的,通常的都是比較純淨的,可是在公司內通常這種鏡像沒有任何做用,咱們須要的是基礎環境都配置好的,能夠直接在此基礎上面運行服務的系統。nginx
綜上所述,咱們須要搭建一個在咱們服務器內部的一個鏡像倉庫,須要哪一個直接拉取便可!(相似於gitlab),咱們須要在本地建立一個倉庫:git
[root@xaviyunserver ~]# docker pull registry Using default tag: latest
[root@xaviyunserver ~]# docker run -d -p 5000:5000 registry 87517b62936d5ad893c078c790c4bbaebc38056052a85f2cdc14ac921c1b4677 [root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87517b62936d registry "/entrypoint.sh /etc…" 6 seconds ago Up 5 seconds 0.0.0.0:5000->5000/tcp friendly_davinci 1a93298ed9db centos7 "bash" 11 hours ago Up 11 hours centos7_1 5ab8830ee5d2 centos7 "bash" 11 hours ago Up 11 hours goofy_knuth 410e90b34976 centos7 "bash" 13 hours ago Up 13 hours dreamy_chatterjee 83a4c32c03ca centos "/bin/bash" 14 hours ago Up 14 hours nostalgic_lamport
假若有這麼一種狀況,我們的容器須要對外進行提供服務,虛擬機內的IP地址確定不能夠直接做爲訪問地址的,可是咱們ens33的地址能夠配置成對外的公網IP 而後經過端口映射,是否是咱們的docker容器也能夠跑web服務了?web
[root@xaviyunserver ~]# docker exec -it 410e90b34976c bash //任意進入一個容器 [root@410e90b34976 /]# ifconfig bash: ifconfig: command not found [root@410e90b34976 /]# yum install -y net-tools //安裝net-tools [root@410e90b34976 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet) RX packets 4008 bytes 10832918 (10.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3346 bytes 231130 (225.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@410e90b34976 /]# exit exit [root@xaviyunserver ~]# ping 172.17.0.4 //能夠ping通這個容器 PING 172.17.0.4 (172.17.0.4) 56(84) bytes of data. 64 bytes from 172.17.0.4: icmp_seq=1 ttl=64 time=0.054 ms 64 bytes from 172.17.0.4: icmp_seq=2 ttl=64 time=0.058 ms 64 bytes from 172.17.0.4: icmp_seq=3 ttl=64 time=0.055 ms
[root@xaviyunserver ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87517b62936d registry "/entrypoint.sh /etc…" 18 minutes ago Up 18 minutes 0.0.0.0:5000->5000/tcp friendly_davinci 1a93298ed9db centos7 "bash" 11 hours ago Up 11 hours centos7_1 5ab8830ee5d2 centos7 "bash" 11 hours ago Up 11 hours goofy_knuth 410e90b34976 centos7 "bash" 13 hours ago Up 13 hours dreamy_chatterjee 83a4c32c03ca centos "/bin/bash" 15 hours ago Up 15 hours nostalgic_lamport
[root@xaviyunserver ~]# telnet 127.0.0.1 5000 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. [root@xaviyunserver ~]# curl 127.0.0.1:5000/v2/_catalog {"repositories":[]}
[root@xaviyunserver ~]# docker tag centos7 116.62.212.186:5000/centos7 [root@xaviyunserver ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest 1513b3b29ff7 14 hours ago 435MB 116.62.212.186:5000/centos7 latest 1513b3b29ff7 14 hours ago 435MB centos_with_net latest 097367cfbba8 15 hours ago 282MB ubuntu latest 113a43faa138 4 weeks ago 81.2MB xavilinux_centos latest 49f7960eb7e4 4 weeks ago 200MB centos latest 49f7960eb7e4 4 weeks ago 200MB registry latest d1fd7d86a825 5 months ago 33.3MB
[root@xaviyunserver ~]# vi /etc/docker/daemon.json { "insecure-registries": ["116.62.212.186:5000"] } [root@xaviyunserver ~]# systemctl restart docker //重啓服務 [root@xaviyunserver ~]# docker ps -a //查看容器已經關閉,還須要啓動 [root@xaviyunserver ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87517b62936d registry "/entrypoint.sh /etc…" About an hour ago Exited (2) 3 minutes ago friendly_davinci
[root@xaviyunserver ~]# docker start 87517b62936d 87517b62936d [root@xaviyunserver ~]# docker push 116.62.212.186:5000/centos7 The push refers to repository [116.62.212.186:5000/centos7]
排障過程:redis
經過curl進行驗證:curl https://dhq9bx4f.mirror.aliyuncs.com/v2/_catalog 是能夠正常返回結果,說明服務沒有問題sql
經過谷歌×××(百度不行,解決不了這個問題),一共大概有兩種方式docker
一、設置dns,在/etc/resolv.conf,以及/etc/docker/daemon.json中增長dns。例如dns地址:8.8.8.8shell
二、設置docker代理(centos和ubuntu一樣設置)json
參考文獻:
https://blog.csdn.net/xxb249/article/details/79469534
[root@xaviyunserver ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql Digest: sha256:43ed4f8c9d1695e97a39cdfe9475af9096e3723cfb79d820d8da00d61a277a85 Status: Image is up to date for mysql:latest
[root@zhdy01 ~]# curl 127.0.0.1:5000/v2/_catalog {"repositories":["centos7","centvim"]}
總結:今天換了電腦上的虛擬機倆作實驗,一次成功
[root@xavi ~]# docker push 192.168.72.130:5000/centos7 The push refers to repository [192.168.72.130:5000/centos7] 788edba9eaa8: Pushed latest: digest: sha256:cfc19e79d4010d16b86660c37dbf1f1e881e974dd06c3c6ab05477db2a43444a size: 529 [root@xavi ~]# docker run -d -p 2000:2000 registry 9c2b897587c22d9f1d6977b8c2c87b4f33dd98dc0c63003e361b60864520c70b [root@xavi ~]# [root@xavi ~]# docker tag ubuntu 192.168.72.130:5000/ubuntu [root@xavi ~]# docker push 192.168.72.130:5000/ubuntu The push refers to repository [192.168.72.130:5000/ubuntu] b6f13d447e00: Pushed a20a262b87bd: Pushed 904d60939c36: Pushed 3a89e0d8654e: Pushed db9476e6d963: Pushed latest: digest: sha256:e7def0d56013d50204d73bb588d99e0baa7d69ea1bc1157549b898eb67287612 size: 1357 [root@xavi ~]# curl 127.0.0.1:5000/v2_catalog 404 page not found [root@xavi ~]# curl 127.0.0.1:5000/v2/_catalog {"repositories":["centos7","ubuntu"]}
背景:假如咱們的某個容器跑了一個線上的業務,不可抵抗的狀況發生了,數據呢?數據具體在什麼位置呢?他會一併刪除,如何保證數據不刪除呢?
把咱們宿主機的目錄掛載到容器內,讓容器的數據所有寫到我們指定的宿主機目錄。
[root@xavi ~]# docker run -tid -v /data/:/data centos7 bash 43e7d37d1adcf10abca55aeeed565ddb5fcded2777ae4e96193332529029dff5 [root@xavi ~]# ls /data/ ftp mariadb mysql nginx redis wwwroot [root@xavi ~]# docker exec -it 43e7d37d1adcf bash [root@43e7d37d1adc /]# ls -l /data/ total 0 drwxr-xr-x 2 1010 1010 21 Mar 29 22:20 ftp drwx------ 5 1004 root 222 Mar 1 11:56 mariadb drwx------ 9 1004 1005 194 Apr 18 05:37 mysql drwxr-xr-x 4 root root 42 Mar 15 11:47 nginx drwxr-xr-x 2 root root 44 Jun 13 08:38 redis drwxr-xr-x 8 root root 116 May 27 09:04 wwwroot
上述兩處/data/文件下的目錄是相同的
[root@43e7d37d1adc /]# mkdir /data/xavi [root@43e7d37d1adc /]# exit exit [root@xavi ~]# ls /data/ ftp mariadb mysql nginx redis wwwroot xavi // 看來已經自動的存儲到了宿主機的硬盤上!
[root@xavi ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 43e7d37d1adc centos7 "bash" 13 minutes ago Up 13 minutes friendly_visvesvaraya 9c2b897587c2 registry "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:2000->2000/tcp, 5000/tcp epic_saha d0b81c693cc5 registry "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp objective_chebyshev
[root@xavi ~]# docker run -itd --volumes-from friendly_visvesvaraya centos7 bash 97331ffb05d10701d2a9c488a8a4716313d8336cedafe66e5a9c7364d7be4985 [root@xavi ~]# [root@xavi ~]# docker exec -it 97331ffb05d10701 bash [root@97331ffb05d1 /]# ls /data/ ftp mariadb mysql nginx redis wwwroot xavi
這裏很相似於NFS的共享功能?
有時候,咱們須要多個容器之間相互共享數據,相似於linux裏面的NFS,因此就能夠搭建一個專門的數據卷容器,而後其餘容器直接掛載該數據卷。
應用背景:
平常工做中:把容器和本地宿主機作了目錄映射直接存在本地,那咱們只需對本地的硬盤按期備份,但若是沒有作這個映射,沒有作映射狀況下的備份和恢復該如何來處理
mkdir /data/backup docker run --volumes-from testvol -v /data/backup/:/backup centos tar cvf /backup/data.tar /data/
思路: 先新建一個數據卷容器,再建一個新的容器並掛載該數據卷容器,而後再把tar包解包。