Docker入门二
容器管理
1.docker create創(chuàng)建一個(gè)容器,但容器并沒(méi)啟動(dòng),就和我們創(chuàng)建虛擬機(jī)一樣,創(chuàng)建了虛擬機(jī)后沒(méi)啟動(dòng)
[root@centos-02 ~]# docker create -it centos6 bash WARNING: IPv4 forwarding is disabled. Networking will not work. 558e31f7b0fb941ca4ee8c1c2b42553b06ac79c0613984b7ad8b9b4ba97f61fd [root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 558e31f7b0fb centos6 "bash" 9 seconds ago Created hopeful_murdock 94b9eab05296 centos6 "bash" About an hour ago Up About an hour elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
2.啟動(dòng)容器
[root@centos-02 ~]# docker start 558e31f7b0fb 558e31f7b0fb [root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 558e31f7b0fb centos6 "bash" 2 minutes ago Up 8 seconds hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up About an hour elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
3.不加-d運(yùn)行容器
[root@centos-02 ~]# docker run -it centos bash WARNING: IPv4 forwarding is disabled. Networking will not work. [root@f4eba170402c /]# ls bin etc lib media opt root sbin sys usr dev home lib64 mnt proc run srv tmp var
4.我們用ctrl+d退出,然后查看容器發(fā)現(xiàn)沒(méi)有f4eba170402c,因?yàn)槲覀兺顺隽酥暗腷ash,這是因?yàn)槲覀兺顺隽藳](méi)有加-d,加上-a參數(shù)查看狀態(tài)為Exited,
[root@f4eba170402c /]# exit [root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 558e31f7b0fb centos6 "bash" 10 minutes ago Up 7 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
[root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4eba170402c centos "bash" 3 minutes ago Exited (0) 2 minutes ago cocky_bell 558e31f7b0fb centos6 "bash" 12 minutes ago Up 9 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
5.給容器起個(gè)名字
[root@centos-02 ~]# docker run -itd --name centos6_1 centos6 bash WARNING: IPv4 forwarding is disabled. Networking will not work. 5e32153dbbd6dea32b62291f9aa484b1d82c84d5edf9899ab72a8335f61e16ac [root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e32153dbbd6 centos6 "bash" 15 seconds ago Up 11 seconds centos6_1 558e31f7b0fb centos6 "bash" 17 minutes ago Up 14 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
6.可以直接用名字進(jìn)入容器
[root@centos-02 ~]# docker exec -it centos6_1 bash [root@5e32153dbbd6 /]#
7.容器執(zhí)行完直接刪除、命令執(zhí)行完容器就退出,執(zhí)行完不留任何痕跡。
[root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e32153dbbd6 centos6 "bash" 5 minutes ago Up 5 minutes centos6_1 558e31f7b0fb centos6 "bash" 22 minutes ago Up 19 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]# docker run --rm -it centos bash -c "sleep 10" WARNING: IPv4 forwarding is disabled. Networking will not work. [root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e32153dbbd6 centos6 "bash" 7 minutes ago Up 6 minutes centos6_1 f4eba170402c centos "bash" 15 minutes ago Exited (0) 13 minutes ago cocky_bell 558e31f7b0fb centos6 "bash" 23 minutes ago Up 21 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
1.docker logs查看容器運(yùn)行歷史信息
[root@centos-02 ~]# docker run -itd centos bash -c "echo 123" WARNING: IPv4 forwarding is disabled. Networking will not work. 59e0b06bcfb4e407ba29719e0bd805c8f52948429ecfbf94c8616ea5090dcd37 [root@centos-02 ~]# docker logs 59e0b0 123 [root@centos-02 ~]#
2.刪除容器 rm,刪除一個(gè)啟動(dòng)的容器加-f參數(shù)
[root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 59e0b06bcfb4 centos "bash -c 'echo 123'" 2 minutes ago Exited (0) 2 minutes ago xenodochial_hoover 5e32153dbbd6 centos6 "bash" 18 minutes ago Up 18 minutes centos6_1 f4eba170402c centos "bash" 26 minutes ago Exited (0) 24 minutes ago cocky_bell 558e31f7b0fb centos6 "bash" 34 minutes ago Up 32 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]# docker rm 59e0b06bcfb4 59e0b06bcfb4 [root@centos-02 ~]# docker rm f4eba170402c f4eba170402c [root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e32153dbbd6 centos6 "bash" 20 minutes ago Up 20 minutes centos6_1 558e31f7b0fb centos6 "bash" 37 minutes ago Up 34 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
[root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e32153dbbd6 centos6 "bash" 21 minutes ago Up 21 minutes centos6_1 558e31f7b0fb centos6 "bash" 38 minutes ago Up 35 minutes hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]# docker rm 558e31f7b0fb Error response from daemon: You cannot remove a running container 558e31f7b0fb941ca4ee8c1c2b42553b06ac79c0613984b7ad8b9b4ba97f61fd. Stop the container before attempting removal or
force remove [root@centos-02 ~]# docker rm -f 558e31f7b0fb 558e31f7b0fb [root@centos-02 ~]#
[root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e32153dbbd6 centos6 "bash" 22 minutes ago Up 22 minutes centos6_1 94b9eab05296 centos6 "bash" 2 hours ago Up 2 hours elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#
倉(cāng)庫(kù)管理
1.用registry鏡像搭建私有倉(cāng)庫(kù),我們可以用這個(gè)鏡像來(lái)運(yùn)行一個(gè)容器,這個(gè)容器就是我們的私有倉(cāng)庫(kù)。
[root@centos-02 ~]# docker pull registry Using default tag: latest latest: Pulling from library/registry 81033e7c1d6a: Pull complete b235084c2315: Pull complete c692f3a6894b: Pull complete ba2177f3a70e: Pull complete a8d793620947: Pull complete Digest: sha256:672d519d7fd7bbc7a448d17956ebeefe225d5eb27509d8dc5ce67ecb4a0bce54 Status: Downloaded newer image for registry:latest
2.啟動(dòng)鏡像
[root@centos-02 ~]# docker run -d -p 5000:5000 registry
WARNING: IPv4 forwarding is disabled. Networking will not work.
c9d3872e7057bf844d7d6efed193ce18d0d8be13ea644fb3a3d3437119e9b62d
[root@centos-02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9d3872e7057 registry "/entrypoint.sh /e..." 22 seconds ago Up 6 seconds 0.0.0.0:5000->5000/tcp cocky_kalam
5e32153dbbd6 centos6 "bash" 10 hours ago Up 10 hours centos6_1
94b9eab05296 centos6 "bash" 12 hours ago Up 12 hours elegant_minsky
815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira
[root@centos-02 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}
[root@centos-02 ~]#
3.我們給centos6加一個(gè)標(biāo)簽
[root@centos-02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6 latest 9aae4b974d36 12 hours ago 512MB centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos latest 49f7960eb7e4 2 weeks ago 200MB registry latest d1fd7d86a825 5 months ago 33.3MB [root@centos-02 ~]# docker tag centos6 192.168.133.88:5000/centos6 [root@centos-02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.133.88:5000/centos6 latest 9aae4b974d36 12 hours ago 512MB centos6 latest 9aae4b974d36 12 hours ago 512MB centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos latest 49f7960eb7e4 2 weeks ago 200MB registry latest d1fd7d86a825 5 months ago 33.3MB [root@centos-02 ~]#
1.指定私有倉(cāng)庫(kù)的地址
[root@centos-02 ~]# vi /etc/docker/daemon.json
[root@centos-02 ~]# cat /etc/docker/daemon.json
{
"insecure-registries":["192.168.133.88:5000"]
}
[root@centos-02 ~]#
2.重啟docker
[root@centos-02 ~]# systemctl restart docker [root@centos-02 ~]# vi /etc/docker/daemon.json [root@centos-02 ~]# systemctl restart docker [root@centos-02 ~]# docker push 192.168.133.88:5000/centos6 The push refers to a repository [192.168.133.88:5000/centos6] Get http://192.168.133.88:5000/v2/: dial tcp 192.168.133.88:5000: getsockopt: connection refused [root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9d3872e7057 registry "/entrypoint.sh /e..." 19 minutes ago Exited (2) 3 minutes ago cocky_kalam 5e32153dbbd6 centos6 "bash" 11 hours ago Exited (137) 3 minutes ago centos6_1 94b9eab05296 centos6 "bash" 12 hours ago Exited (137) 2 minutes ago elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Exited (137) 3 minutes ago distracted_mahavira [root@centos-02 ~]# docker start c9d3872e7057 c9d3872e7057 [root@centos-02 ~]# docker push 192.168.133.88:5000/centos6 The push refers to a repository [192.168.133.88:5000/centos6] 0a2f11f7b1ef: Pushed latest: digest: sha256:447a292bca0c97689818f9dd3003fa92c2c1489e4922b57f75784005d042f317 size: 529 [root@centos-02 ~]#
3.查看私有鏡像
[root@centos-02 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos6"]}
[root@centos-02 ~]#
4.下面我們把centos_with_net放到私有鏡像里
[root@centos-02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.133.88:5000/centos6 latest 9aae4b974d36 13 hours ago 512MB centos6 latest 9aae4b974d36 13 hours ago 512MB centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos latest 49f7960eb7e4 2 weeks ago 200MB registry latest d1fd7d86a825 5 months ago 33.3MB [root@centos-02 ~]# docker tag centos_with_net 192.168.133.88:5000/centos_with_net [root@centos-02 ~]# docker push 192.168.133.88:5000/centos_with_net The push refers to a repository [192.168.133.88:5000/centos_with_net] 7ba39064c8e1: Pushed bcc97fbfc9e1: Pushed latest: digest: sha256:022fd29aae96ae544f44c1e97b9515c3809eb9c8d4b44eaa9897de695718f872 size: 741 [root@centos-02 ~]#
[root@centos-02 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos6","centos_with_net"]}
[root@centos-02 ~]#
5.從私有倉(cāng)庫(kù)下載鏡像
[root@centos-02 ~]# docker pull 192.168.133.88:5000/centos6 Using default tag: latest latest: Pulling from centos6 Digest: sha256:447a292bca0c97689818f9dd3003fa92c2c1489e4922b57f75784005d042f317 Status: Image is up to date for 192.168.133.88:5000/centos6:latest [root@centos-02 ~]#
數(shù)據(jù)管理
https://blog.csdn.net/u010846177/article/details/54356670
1.掛載本地的目錄到容器里
[root@centos-02 ~]# docker run -tid -v /data/:/data centos_with_net bash (左邊的data是宿主機(jī)目錄,右邊的data是容器目錄)(這個(gè)叫數(shù)據(jù)卷容器類似于nfs) 2800b8b42c0522f2963189baff7fbdcb20459b022d655edbaf773832d866672a [root@centos-02 ~]# ls /data/ gitroot mongodb redis redis2 redis_data [root@centos-02 ~]# docker exec -it 2800b8 bash [root@2800b8b42c05 /]# ls -l /data/ total 0 drwxr-xr-x 3 root root 24 May 8 14:50 gitroot drwxr-xr-x 7 root root 76 Apr 9 13:21 mongodb drwxr-xr-x 2 root root 44 Mar 31 06:41 redis drwxr-xr-x 2 root root 44 Mar 31 06:41 redis2 drwxr-xr-x 6 root root 54 Mar 31 17:31 redis_data [root@2800b8b42c05 /]#
2.我們?cè)谌萜骼飫?chuàng)建一個(gè)123目錄,也會(huì)在宿主機(jī)上自動(dòng)建一個(gè)123目錄
[root@centos-02 ~]# docker exec -it 2800b8 bash [root@2800b8b42c05 /]# mkdir /data/123 [root@2800b8b42c05 /]# exit [root@centos-02 ~]# ls /data/ 123 gitroot mongodb redis redis2 redis_data [root@centos-02 ~]#
1.創(chuàng)建一個(gè)新的容器,然后把centos_with_net作為一個(gè)數(shù)據(jù)卷
[root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2800b8b42c05 centos_with_net "bash" 19 minutes ago Up 19 minutes zen_nightingale c9d3872e7057 registry "/entrypoint.sh /e..." About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp cocky_kalam [root@centos-02 ~]# docker run -itd --volumes-from zen_nightingale centos6 bash bd6eb972dbdf398278f68602632cb037dc940801b4ab961e385dbc436beca808 [root@centos-02 ~]# docker exec -it bd6eb9 bash [root@bd6eb972dbdf /]# ls /data/ 123 gitroot mongodb redis redis2 redis_data [root@bd6eb972dbdf /]#
數(shù)據(jù)卷備份恢復(fù)
1.上圖說(shuō)明:我們的數(shù)據(jù)卷容器共享的目錄叫data,宿主機(jī)有一個(gè)叫data/backup目錄,data/backup目錄和新建容器里面的backup目錄是映射的,也就是說(shuō)兩個(gè)目錄的文件是相同的,現(xiàn)在我們新建容器掛載了數(shù)據(jù)卷容器,意味著數(shù)據(jù)卷容器的data和新建容器的data下面的文件是一樣的,現(xiàn)在我們把新建容器里面的data下面的數(shù)據(jù)拷貝到新建容器里面的backup目錄下,也就是把數(shù)據(jù)卷容器下面data的數(shù)據(jù)拷貝到了新建容器data/backup下面
Docker網(wǎng)絡(luò)模式
1.虛擬機(jī)網(wǎng)絡(luò)模式:
橋接:網(wǎng)絡(luò)和宿主機(jī)網(wǎng)絡(luò)是同等關(guān)系,他們鏈接了同一個(gè)路由器,鏈接了同一個(gè)交換機(jī),網(wǎng)段也是一樣的。
NAT:它把宿主機(jī)的網(wǎng)絡(luò)做成了一個(gè)和路由器設(shè)備一樣,它實(shí)現(xiàn)了網(wǎng)絡(luò)地址轉(zhuǎn)換,只要宿主機(jī)能聯(lián)網(wǎng)它就能聯(lián)網(wǎng),他們的網(wǎng)段是不一樣的。
僅主機(jī):就是主機(jī)和宿主機(jī)做一個(gè)鏈接,它們直接連了一根網(wǎng)線
2.docker網(wǎng)絡(luò)模式:
host模式:
container模式:
none模式:
bridge模式:
1.如何讓外部訪問(wèn)容器呢?做端口映射,操作過(guò)程:首先進(jìn)入一個(gè)容器里,在容器里安裝一個(gè)web服務(wù)nginx,把這個(gè)帶nginx的容器做一個(gè)打包或者說(shuō)導(dǎo)成一個(gè)鏡像,鏡像完成之后我們?cè)賳?dòng)一個(gè)容器,在啟動(dòng)容器的時(shí)候我們加一個(gè)端口映射
[root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd6eb972dbdf centos6 "bash" 13 hours ago Up 13 hours focused_carson 2800b8b42c05 centos_with_net "bash" 13 hours ago Up 13 hours zen_nightingale c9d3872e7057 registry "/entrypoint.sh /e..." 14 hours ago Up 14 hours 0.0.0.0:5000->5000/tcp cocky_kalam [root@centos-02 ~]# docker exec -it 2800b8b42c05 bash [root@2800b8b42c05 /]#
[root@centos-02 ~]# docker exec -it 2800b8b42c05 bash [root@2800b8b42c05 /]# yum install -y epel-release Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.nwsuaf.edu.cn * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn base | 3.6 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:7-11 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: epel-release noarch 7-11 extras 15 k Transaction Summary ================================================================================ Install 1 Package Total download size: 15 k Installed size: 24 k Downloading packages: epel-release-7-11.noarch.rpm | 15 kB 00:11 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : epel-release-7-11.noarch 1/1 Verifying : epel-release-7-11.noarch 1/1 Installed: epel-release.noarch 0:7-11 Complete! [root@2800b8b42c05 /]#
[root@2800b8b42c05 /]# yum install -y nginx
[root@2800b8b42c05 /]# rpm -qa nginx nginx-1.12.2-2.el7.x86_64
2.下面我們把容器導(dǎo)成鏡像
[root@2800b8b42c05 /]# exit [root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2800b8b42c05 centos_with_net "bash" 15 hours ago Up About an hour zen_nightingale [root@centos-02 ~]# docker commit -m "install nginx" -a "linux_02" 2800b8b42c05 centos_with_nginx sha256:aa85a9db695dcef620db740a056d33f9ff13043740944bf27247646a60e92f39 [root@centos-02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_with_nginx latest aa85a9db695d 17 seconds ago 412MB 192.168.133.88:5000/centos6 latest 9aae4b974d36 28 hours ago 512MB centos6 latest 9aae4b974d36 28 hours ago 512MB 192.168.133.88:5000/centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos latest 49f7960eb7e4 2 weeks ago 200MB registry latest d1fd7d86a825 5 months ago 33.3MB [root@centos-02 ~]# docker run -itd -p 8088:80 centos_with_nginx bash 2a18e14f9262e43debf9a8979013f0a34397137b2da63105c47640d415229948 [root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a18e14f9262 centos_with_nginx "bash" 10 seconds ago Up 7 seconds 0.0.0.0:8088->80/tcp stupefied_haibt 2800b8b42c05 centos_with_net "bash" 15 hours ago Up About an hour zen_nightingale [root@centos-02 ~]#
operation not permitted
1.新建的容器啟動(dòng)nginx的時(shí)候會(huì)報(bào)錯(cuò),這是因?yàn)閐bus-daemon服務(wù)沒(méi)有起來(lái)
[root@centos-02 ~]# docker exec -it 2a18e14f9262 bash [root@2a18e14f9262 /]# systemctl start nginx Failed to get D-Bus connection: Operation not permitted [root@2a18e14f9262 /]#
[root@2a18e14f9262 /]# exit [root@centos-02 ~]# docker run -itd --privileged -e "container=docker" -p 8088:80 centos_with_nginx /usr/sbin/init bbe891100f8dde42b31c08bd022598ce8807e99ae50d6a606a367431f1e68a81 docker: Error response from daemon: driver failed programming external connectivity on endpoint unruffled_agnesi (44961c284cc83df3eca7b4b8ae04c484d6635b5df06686b6924a321524c7a086):
Bind for 0.0.0.0:8088 failed: port is already allocated. [root@centos-02 ~]# docker rm -f 2a18e14f9262 2a18e14f9262 [root@centos-02 ~]# docker run -itd --privileged -e "container=docker" -p 8088:80 centos_with_nginx /usr/sbin/init 8c427376403f1a8d1bc7f7681058aea714d0b4da950dfe6b5671f6ffdde39403 [root@centos-02 ~]#
2.牛逼啟動(dòng)成功
[root@centos-02 ~]# docker exec -it 8c4273 bash [root@8c427376403f /]# systemctl start nginx [root@8c427376403f /]# ps aux|grep nginx root 99 0.3 0.2 120812 2088 ? Ss 05:23 0:00 nginx: master process /usr/sbin/nginx nginx 100 0.1 0.3 121276 3112 ? S 05:23 0:00 nginx: worker process root 102 0.0 0.0 9092 664 pts/1 S+ 05:23 0:00 grep --color=auto nginx [root@8c427376403f /]#
3.下面我們退出用8088端口訪問(wèn)(配置nginx一定不能清空iptables是,清空之后瀏覽器就不能訪問(wèn)nginx容器了)
[root@8c427376403f /]# exit
[root@centos-02 ~]# curl localhost:8088
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Page for the Nginx HTTP Server on Fedora</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
/*<![CDATA[*/
body {
background-color: #fff;
color: #000;
font-size: 0.9em;
font-family: sans-serif,helvetica;
margin: 0;
padding: 0;
}
:link {
color: #c00;
}
:visited {
color: #c00;
}
a:hover {
color: #f50;
}
h1 {
text-align: center;
margin: 0;
padding: 0.6em 2em 0.4em;
background-color: #294172;
color: #fff;
font-weight: normal;
font-size: 1.75em;
border-bottom: 2px solid #000;
}
h1 strong {
font-weight: bold;
font-size: 1.5em;
}
h2 {
text-align: center;
background-color: #3C6EB4;
font-size: 1.1em;
font-weight: bold;
color: #fff;
margin: 0;
padding: 0.5em;
border-bottom: 2px solid #294172;
}
hr {
display: none;
}
.content {
padding: 1em 5em;
}
.alert {
border: 2px solid #000;
}
img {
border: 2px solid #fff;
padding: 2px;
margin: 2px;
}
a:hover img {
border: 2px solid #294172;
}
.logos {
margin: 1em;
text-align: center;
}
/*]]>*/
</style>
</head>
<body>
<h1>Welcome to <strong>nginx</strong> on Fedora!</h1>
<div class="content">
<p>This page is used to test the proper operation of the
<strong>nginx</strong> HTTP server after it has been
installed. If you can read this page, it means that the
web server installed at this site is working
properly.</p>
<div class="alert">
<h2>Website Administrator</h2>
<div class="content">
<p>This is the default <tt>index.html</tt> page that
is distributed with <strong>nginx</strong> on
Fedora. It is located in
<tt>/usr/share/nginx/html</tt>.</p>
<p>You should now put your content in a location of
your choice and edit the <tt>root</tt> configuration
directive in the <strong>nginx</strong>
configuration file
<tt>/etc/nginx/nginx.conf</tt>.</p>
</div>
</div>
<div class="logos">
<a ><img
src="nginx-logo.png"
alt="[ Powered by nginx ]"
width="121" height="32" /></a>
<a ><img
src="poweredby.png"
alt="[ Powered by Fedora ]"
width="88" height="31" /></a>
</div>
</div>
</body>
</html>
[root@centos-02 ~]#
配置橋接網(wǎng)絡(luò)
1.pipework網(wǎng)絡(luò)的一種額外模式,能夠?qū)崿F(xiàn)讓docker容器和宿主機(jī)使用同一個(gè)交換機(jī),他們?cè)谕粋€(gè)網(wǎng)段下,這樣就可以直接和外面的網(wǎng)絡(luò)通信,這樣我們就可以把docker容器看做成一臺(tái)獨(dú)立的服務(wù)器,我們要操作的網(wǎng)卡是ens33
[root@centos-02 ~]# cd /etc/sysconfig/network-scripts/
[root@centos-02 network-scripts]# ls
ifcfg-ens33 ifdown-isdn ifup ifup-plip ifup-tunnel
ifcfg-lo ifdown-post ifup-aliases ifup-plusb ifup-wireless
ifdown ifdown-ppp ifup-bnep ifup-post init.ipv6-global
ifdown-bnep ifdown-routes ifup-eth ifup-ppp network-functions
ifdown-eth ifdown-sit ifup-ib ifup-routes network-functions-ipv6
ifdown-ib ifdown-Team ifup-ippp ifup-sit
ifdown-ippp ifdown-TeamPort ifup-ipv6 ifup-Team
ifdown-ipv6 ifdown-tunnel ifup-isdn ifup-TeamPort
[root@centos-02 network-scripts]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:eeff:fedc:8689 prefixlen 64 scopeid 0x20<link>
ether 02:42:ee:dc:86:89 txqueuelen 0 (Ethernet)
RX packets 34331 bytes 1776206 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 39913 bytes 355961005 (339.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.133.88 netmask 255.255.255.0 broadcast 192.168.133.255
inet6 fe80::b646:159d:d0ac:4cbe prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:33:1b:3e txqueuelen 1000 (Ethernet)
RX packets 602491 bytes 453368450 (432.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 439996 bytes 71337069 (68.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 896436 bytes 253197350 (241.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 896436 bytes 253197350 (241.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vethe16fc6d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::3097:1eff:fe0e:2a68 prefixlen 64 scopeid 0x20<link>
ether 32:97:1e:0e:2a:68 txqueuelen 0 (Ethernet)
RX packets 25 bytes 18424 (17.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3780 (3.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vethe354dc7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::90a7:97ff:fed0:344d prefixlen 64 scopeid 0x20<link>
ether 92:a7:97:d0:34:4d txqueuelen 0 (Ethernet)
RX packets 3901 bytes 224117 (218.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5214 bytes 24234533 (23.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos-02 network-scripts]#
2.和ens33網(wǎng)卡做一個(gè)橋接,拷貝ens33網(wǎng)卡為br0,將ens33的ip配置到br0上
[root@centos-02 network-scripts]# cp ifcfg-ens33 ifcfg-br0 [root@centos-02 network-scripts]#
[root@centos-02 network-scripts]# vim ifcfg-br0 [root@centos-02 network-scripts]# cat ifcfg-br0 TYPE=Bridge BOOTPROTO=static DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=br0 UUID=63d602d6-c8ae-4350-b149-aad17fc44e98 DEVICE=br0 ONBOOT=yes IPADDR=192.168.133.88 NETWORK=255.255.255.0 GATEWAY=192.168.133.2 DNS1=119.29.29.29 [root@centos-02 network-scripts]# vim ifcfg-ens33 [root@centos-02 network-scripts]# cat ifcfg-ens33 TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 #UUID=63d602d6-c8ae-4350-b149-aad17fc44e98 DEVICE=ens33 ONBOOT=yes #IPADDR=192.168.133.88 #NETWORK=255.255.255.0 #GATEWAY=192.168.133.2 #DNS1=119.29.29.29 BRIDGE=br0 [root@centos-02 network-scripts]#
3.成功ens33上是沒(méi)有ip的,br0上有ip
[root@centos-02 network-scripts]# systemctl restart network
[root@centos-02 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.133.88 netmask 255.255.255.0 broadcast 192.168.133.255
inet6 fe80::3433:6db9:27f8:29b0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:33:1b:3e txqueuelen 1000 (Ethernet)
RX packets 303 bytes 43156 (42.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 312 bytes 50106 (48.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:eeff:fedc:8689 prefixlen 64 scopeid 0x20<link>
ether 02:42:ee:dc:86:89 txqueuelen 0 (Ethernet)
RX packets 34331 bytes 1776206 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 39913 bytes 355961005 (339.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:33:1b:3e txqueuelen 1000 (Ethernet)
RX packets 608727 bytes 454406538 (433.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 446255 bytes 72375550 (69.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 908632 bytes 256415982 (244.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 908632 bytes 256415982 (244.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vethe16fc6d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::3097:1eff:fe0e:2a68 prefixlen 64 scopeid 0x20<link>
ether 32:97:1e:0e:2a:68 txqueuelen 0 (Ethernet)
RX packets 25 bytes 18424 (17.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3780 (3.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vethe354dc7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::90a7:97ff:fed0:344d prefixlen 64 scopeid 0x20<link>
ether 92:a7:97:d0:34:4d txqueuelen 0 (Ethernet)
RX packets 3901 bytes 224117 (218.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5214 bytes 24234533 (23.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos-02 network-scripts]#
4.下面我們安裝pipework
[root@centos-02 network-scripts]# cd [root@centos-02 ~]# git clone https://github.com/jpetazzo/pipework Cloning into 'pipework'... remote: Counting objects: 501, done. remote: Total 501 (delta 0), reused 0 (delta 0), pack-reused 501 Receiving objects: 100% (501/501), 172.97 KiB | 138.00 KiB/s, done. Resolving deltas: 100% (264/264), done. [root@centos-02 ~]# cd pipework/ [root@centos-02 pipework]# ls docker-compose.yml doctoc LICENSE pipework pipework.spec README.md [root@centos-02 pipework]# cp pipework /usr/local/bin/
5.下面我們開啟一個(gè)容器
[root@centos-02 pipework]# cd
[root@centos-02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c427376403f centos_with_nginx "/usr/sbin/init" 45 minutes ago Up 45 minutes 0.0.0.0:8088->80/tcp festive_leavitt
2800b8b42c05 centos_with_net "bash" 16 hours ago Up 2 hours zen_nightingale
[root@centos-02 ~]# docker run -itd --net=none centos_with_nginx bash
8df53477e0274d3834fc7a6d4ad61c954e391810cf6aa3343c830bc20b3c39e0
[root@centos-02 ~]# docker exec -it 8df534 bash
[root@8df53477e027 /]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@8df53477e027 /]#
6.給容器添加一個(gè)ip,添加成功
[root@8df53477e027 /]# exit
[root@centos-02 ~]# pipework br0 8df534 192.168.133.135/24@192.168.133.2
[root@centos-02 ~]# docker exec -it 8df534 bash
[root@8df53477e027 /]# ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.133.135 netmask 255.255.255.0 broadcast 192.168.133.255
ether 22:1a:2a:85:3e:2d txqueuelen 1000 (Ethernet)
RX packets 77 bytes 4140 (4.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1 bytes 42 (42.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@8df53477e027 /]#
7.測(cè)試
[root@centos-03 ~]# ping 192.168.133.135 PING 192.168.133.135 (192.168.133.135) 56(84) bytes of data. 64 bytes from 192.168.133.135: icmp_seq=1 ttl=64 time=15.0 ms 64 bytes from 192.168.133.135: icmp_seq=2 ttl=64 time=0.658 ms 64 bytes from 192.168.133.135: icmp_seq=3 ttl=64 time=0.523 ms 64 bytes from 192.168.133.135: icmp_seq=4 ttl=64 time=0.667 ms ^C --- 192.168.133.135 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 0.523/4.226/15.056/6.252 ms [root@centos-03 ~]# [root@8df53477e027 /]# ping www.qq.com PING news.qq.com (125.39.52.26) 56(84) bytes of data. 64 bytes from no-data (125.39.52.26): icmp_seq=1 ttl=128 time=72.3 ms ^C --- news.qq.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 6ms rtt min/avg/max/mdev = 72.361/72.361/72.361/0.000 ms [root@8df53477e027 /]#
Dockerfile(上)
1.創(chuàng)建鏡像的方法:可以用現(xiàn)有的鏡像搞成容器,我們?cè)谌萜骼镒鲆恍┎僮靼阉鼘?dǎo)出成鏡像。還有一種方式去openvz的官網(wǎng)下載一個(gè)模板,現(xiàn)在我們通過(guò)dockerfile創(chuàng)建鏡像
2.from:指定基于那個(gè)鏡像
Dockerfile格式(下)
Dockerfile示例(安裝nginx上)
1.編輯Dockerfile文件,執(zhí)行提示我們網(wǎng)絡(luò)不能工作,網(wǎng)絡(luò)不同,這和我們之前配置的pipework有關(guān),我們需要重啟docker。
[root@8df53477e027 /]# exit
[root@centos-02 ~]#
[root@centos-02 ~]# vim Dockerfile
[root@centos-02 ~]# cat Dockerfile
## Set the base image to CentOS
FROM centos
# File Author / Maintainer
MAINTAINER aming aming@aminglinux.com
# Install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
# Install Nginx
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar zxvf nginx-1.8.0.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -fv /usr/local/nginx/conf/nginx.conf
ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf #COPY .nginx_conf /usr/local/nginx/conf/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute when creating a new container
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd
[root@centos-02 ~]# docker build -t centos_nginx .
Sending build context to Docker daemon 823.7MB
Step 1/11 : FROM centos
---> 49f7960eb7e4
Step 2/11 : MAINTAINER aming aming@aminglinux.com
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 4e8cb4705aed
---> 25a43104b71d
Removing intermediate container 4e8cb4705aed
Step 3/11 : RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in ca5c343e847d
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo=<repoid> ...
4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable <repoid>
or
subscription-manager repos --disable=<repoid>
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
The command '/bin/sh -c yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel' returned a non-zero code: 1
[root@centos-02 ~]#
2.重啟docker讓它自動(dòng)綁定之前的br0網(wǎng)絡(luò)
[root@centos-02 ~]# systemctl restart docker [root@centos-02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca5c343e847d 25a43104b71d "/bin/sh -c 'yum i..." 5 minutes ago Exited (1) 5 minutes ago loving_ptolemy 8df53477e027 centos_with_nginx "bash" 3 hours ago Exited (137) About a minute ago gifted_jennings 8c427376403f centos_with_nginx "/usr/sbin/init" 3 hours ago Exited (137) About a minute ago 0.0.0.0:8088->80/tcp festive_leavitt bbe891100f8d centos_with_nginx "/usr/sbin/init" 3 hours ago Created unruffled_agnesi 71453796c2b5 centos_with_nginx "base" 4 hours ago Created 0.0.0.0:8088->80/tcp optimistic_shockley bd6eb972dbdf centos6 "bash" 18 hours ago Exited (137) 5 hours ago focused_carson 2800b8b42c05 centos_with_net "bash" 18 hours ago Exited (137) About a minute ago zen_nightingale c9d3872e7057 registry "/entrypoint.sh /e..." 19 hours ago Exited (255) 5 hours ago 0.0.0.0:5000->5000/tcp cocky_kalam 5e32153dbbd6 centos6 "bash" 30 hours ago Exited (137) 19 hours ago centos6_1 94b9eab05296 centos6 "bash" 31 hours ago Exited (137) 19 hours ago elegant_minsky 815adfd9da61 centos "/bin/bash" 4 days ago Exited (137) 19 hours ago distracted_mahavira [root@centos-02 ~]# docker start 2800b8b42c05 2800b8b42c05 [root@centos-02 ~]#
3.查看是否能聯(lián)網(wǎng)
[root@centos-02 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS 2800b8b42c05 centos_with_net "bash" 19 hours ago Up 2 mile [root@centos-02 ~]# docker exec -it 2800b8b42c05 bash [root@2800b8b42c05 /]# ping www.qq.com PING news.qq.com (125.39.52.26) 56(84) bytes of data. 64 bytes from no-data (125.39.52.26): icmp_seq=1 ttl=127 time=49.5 ms ^C --- news.qq.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 8ms rtt min/avg/max/mdev = 49.520/49.520/49.520/0.000 ms [root@2800b8b42c05 /]#
4.重新build
[root@2800b8b42c05 /]# exit [root@centos-02 ~]# docker build -t centos_nginx .
Dockerfile示例(安裝nginx下)
1.牛逼成功了,下面我們測(cè)試下。
Successfully built 1a27fe560703 Successfully tagged centos_nginx:latest [root@centos-02 ~]#
[root@centos-02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx latest 1a27fe560703 3 minutes ago 346MB centos_with_nginx latest aa85a9db695d 6 hours ago 412MB 192.168.133.88:5000/centos6 latest 9aae4b974d36 33 hours ago 512MB centos6 latest 9aae4b974d36 33 hours ago 512MB 192.168.133.88:5000/centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos_with_net latest f6e3f4365ee8 3 days ago 276MB centos latest 49f7960eb7e4 2 weeks ago 200MB registry latest d1fd7d86a825 5 months ago 33.3MB [root@centos-02 ~]#
[root@centos-02 ~]# docker run -itd -p 81:80 centos_nginx bash bbd4ec8b47df1c948c00ced5048c2cefcd5fb0d0d69ae12aa002fd57e21f3a9a [root@centos-02 ~]# docker exec -it bbd4ec bash [root@bbd4ec8b47df /]# ps aux|grep nginx root 1 0.5 0.1 11684 1304 pts/0 Ss+ 10:49 0:00 /bin/sh -c /usr/local/nginx/sbin/nginx && tail -f /etc/passwd bash root 6 0.0 0.0 24884 788 ? Ss 10:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 8 0.1 0.3 27328 3352 ? S 10:49 0:00 nginx: worker process nobody 9 0.0 0.3 27328 3352 ? S 10:49 0:00 nginx: worker process root 23 0.0 0.0 9092 664 pts/1 S+ 10:50 0:00 grep --color=auto nginx [root@bbd4ec8b47df /]#
用docker compose部署服務(wù)
1.下載安裝docker-compose
[root@centos-02 ~]# curl -L https://github.com/docker/compose/releases/download/1.22.0-rc1/docker-compose-Linux-x86_64 > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 127 0 --:--:-- 0:00:04 --:--:-- 128
100 11.2M 100 11.2M 0 0 75806 0 0:02:34 0:02:34 --:--:-- 70573
[root@centos-02 ~]# du -sh !$
du -sh /usr/local/bin/docker-compose
12M /usr/local/bin/docker-compose
[root@centos-02 ~]# chmod 755 !$
chmod 755 /usr/local/bin/docker-compose
[root@centos-02 ~]#
2.查看compose版本
[root@centos-02 ~]# docker-compose version docker-compose version 1.22.0-rc1, build e7de1bc3 docker-py version: 3.4.0 CPython version: 3.6.5 OpenSSL version: OpenSSL 1.1.0f 25 May 2017 [root@centos-02 ~]#
docker compose示例
1.編輯compose文件并啟動(dòng)centos_nginx、centos_with_net
[root@centos-02 ~]# vim docker-compose.yml
[root@centos-02 ~]# cat docker-compose.yml
version: "2"
services:
app1:
image: centos_nginx
ports:
- "8080:80"
networks:
- "net1"
volumes:
- /data/:/data
app2:
image: centos_with_net
networks:
- "net2"
volumes:
- /data/:/data1
entrypoint: tail -f /etc/passwd
networks:
net1:
driver: bridge
net2:
driver: bridge
[root@centos-02 ~]# docker-compose up -d
Creating network "root_net1" with driver "bridge"
Creating network "root_net2" with driver "bridge"
Creating root_app2_1 ... done
Creating root_app1_1 ... done
[root@centos-02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b53b09b5d10d centos_nginx "/bin/sh -c '/usr/..." 55 seconds ago Up 23 seconds 0.0.0.0:8080->80/tcp root_app1_1
877c4f15ca56 centos_with_net "tail -f /etc/passwd" 55 seconds ago Up 23 seconds root_app2_1
[root@centos-02 ~]#
[root@centos-02 ~]# docker --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
config Manage Docker configs
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
[root@centos-02 ~]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------
root_app1_1 /bin/sh -c /usr/local/ngin ... Up 0.0.0.0:8080->80/tcp
root_app2_1 tail -f /etc/passwd Up
[root@centos-02 ~]#
2.停止命令
[root@centos-02 ~]# docker-compose stop Stopping root_app1_1 ... done Stopping root_app2_1 ... done
3.刪除停止的容器
[root@centos-02 ~]# docker-compose rm -f Going to remove root_app1_1, root_app2_1 Removing root_app1_1 ... done Removing root_app2_1 ... done [root@centos-02 ~]#
4.重啟
[root@centos-02 ~]# docker-compose ps Name Command State Ports ------------------------------ [root@centos-02 ~]# docker-compose up -d Creating root_app2_1 ... done Creating root_app1_1 ... done [root@centos-02 ~]# docker-compose ps Name Command State Ports --------------------------------------------------------------------------- root_app1_1 /bin/sh -c /usr/local/ngin ... Up 0.0.0.0:8080->80/tcp root_app2_1 tail -f /etc/passwd Up [root@centos-02 ~]#
docker搭建lnmp
https://blog.csdn.net/xy752068432/article/details/75975065
1.docker安裝mysql
docker pull mysql:5.6
2.然后我們可以通過(guò)命令 docker images 查看我們剛剛拉下來(lái)的mysql的鏡像
[root@centos-04 /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mysql 5.6 a876cc5d29e4 4 weeks ago 256 MB [root@centos-04 /]#
3.運(yùn)行并啟動(dòng)一個(gè)容器,通過(guò)以下命令
參數(shù)說(shuō)明
-d 讓容器在后臺(tái)運(yùn)行
-p 添加主機(jī)到容器的端口映射
-e 設(shè)置環(huán)境變量,這里是設(shè)置mysql的root用戶的初始密碼,這個(gè)必須設(shè)置
–name 容器的名字,隨便取,但是必須唯一
[root@centos-04 /]# docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.6
ps:其實(shí)我們可以僅僅使用docker run命令就行了。docker run會(huì)先去pull,然后再create。個(gè)人習(xí)慣先把鏡像pull下來(lái),在run的時(shí)候會(huì)很快。
4.接下來(lái)我們就可以通過(guò)命令docker ps -a 查看我們剛剛創(chuàng)建的容器
[root@centos-04 /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f42d98924858 mysql:5.6 "docker-entrypoint..." 4 minutes ago Up 4 minutes 0.0.0.0:3307->3306/tcp mysql [root@centos-04 /]#
這里我們可以看到我的容器狀態(tài)的Up狀態(tài),表示容器正在運(yùn)行,并且把可以看到主機(jī)和容器的端口映射關(guān)系。
5.接下來(lái),我們就可以進(jìn)入到我們剛剛創(chuàng)建的容器中,輸入命令
參數(shù)說(shuō)明
-t 在容器里生產(chǎn)一個(gè)偽終端
-i 對(duì)容器內(nèi)的標(biāo)準(zhǔn)輸入 (STDIN) 進(jìn)行交互
[root@centos-04 /]# docker exec -it mysql bash root@f42d98924858:/#
容器中默認(rèn)是沒(méi)有vim的,所以我們首先要安裝vim,需要注意的是安裝前記得先執(zhí)行apt update命令,不然安裝會(huì)出現(xiàn)問(wèn)題。
進(jìn)入到mysql容器后,我們通過(guò)創(chuàng)建一個(gè)遠(yuǎn)程可以訪問(wèn)的用戶,這樣我們就能從別的主機(jī)訪問(wèn)到我們的數(shù)據(jù)庫(kù)了。
docker安裝php-fpm
1.同樣首先我們拉取php-fpm的鏡像
[root@centos-04 /]# docker pull php:7.0-fpm
2.再創(chuàng)建一個(gè)phpfpm容器
[root@centos-04 /]# docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link mysql:mysql --name phpfpm php:7.0-fpm c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306 [root@centos-04 /]#
參數(shù)說(shuō)明
-d 讓容器在后臺(tái)運(yùn)行
-p 添加主機(jī)到容器的端口映射
-v 添加目錄映射,即主機(jī)上的/var/nginx/www/html和容器中/var/www/html目錄是同步的
–name 容器的名字
–link 與另外一個(gè)容器建立起聯(lián)系,這樣我們就可以在當(dāng)前容器中去使用另一個(gè)容器里的服務(wù)。
這里如果不指定–link參數(shù)其實(shí)也是可以得,因?yàn)槿菀妆旧硪彩怯衖p的且唯一,所以我們也可以直接利用ip去訪問(wèn)容器。
3.然后進(jìn)入到我們的容器,然后我們?cè)?var/www/html目錄下新建一個(gè)index.php文件
[root@centos-04 /]# docker exec -it phpfpm bash root@c58be577ba9f:/var/www/html# touch index.php root@c58be577ba9f:/var/www/html# exit exit [root@centos-04 /]#
4.我們可以看到該目錄下新建了一個(gè)php文件
接下來(lái)我們回到我們的主機(jī)上面,訪問(wèn)一下我們主機(jī)上/var/nginx/www/html
[root@centos-04 /]# ls /var/nginx/www/html index.php [root@centos-04 /]#
我們發(fā)現(xiàn)我們?cè)谌萜骼锏?var/www/html目錄中新建的文件也在主機(jī)的/var/nginx/www/html目錄中,因?yàn)樵趧?chuàng)建容器的時(shí)候,我們已經(jīng)把主機(jī)中的目錄掛載到了容器中去了。
5.因?yàn)楹竺嫖乙褂胮do模塊進(jìn)行測(cè)試,所以我需要自己安裝pdo_mysql模塊,在docker容器中可以這樣來(lái)安裝
[root@centos-04 /]# docker exec -it phpfpm bash root@c58be577ba9f:/var/www/html# docker-php-ext-install pdo_mysql
6.然后我們可以通過(guò)命令php -m查看我們的php的所有擴(kuò)展模塊,我們可以去看到我們剛剛安裝的pdo_mysql擴(kuò)展也在里面
root@c58be577ba9f:/var/www/html# php -m [PHP Modules] Core ctype curl date dom fileinfo filter ftp hash iconv json libxml mbstring mysqlnd openssl pcre PDO pdo_mysql pdo_sqlite Phar posix readline Reflection session SimpleXML SPL sqlite3 standard tokenizer xml xmlreader xmlwriter zlib [Zend Modules] root@c58be577ba9f:/var/www/html#
7.重啟phpfmp容器
[root@centos-04 /]# docker restart c58be577ba9f
docker安裝nginx
1.我們從倉(cāng)庫(kù)里去拉取一個(gè)nginx鏡像
root@c58be577ba9f:/var/www/html# exit; exit [root@centos-04 /]# docker pull nginx:1.10.3
https://blog.csdn.net/qq_26641781/article/details/80883192
docker run --name my_nginx -d -p 80:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx
2.接下來(lái)運(yùn)行nginx容器(如果想要把nginx的配置文件掛載到宿主機(jī)需要先目錄和文件并且配置文件不能有錯(cuò),因?yàn)槭侵苯油降絥ginx容器中的對(duì)應(yīng)配置,啟動(dòng)容器的時(shí)候nginx直接用的,如果有錯(cuò)誤nginx啟動(dòng)不成功)
[root@centos-04 /]# docker run -d -p 80:80 --name nginx -v /var/nginx/www/html:/var/www/html --link phpfpm:phpfpm --name nginx nginx:1.10.3 c00e5859f876b15180d6aa2e69dbf87d34d2ee7bf00d3642a8d84d922b2fc22c [root@centos-04 /]#
參數(shù)說(shuō)明:
-d 讓容器在后臺(tái)運(yùn)行
-p 添加主機(jī)到容器的端口映射
-v 添加目錄映射,這里最好nginx容器的根目錄最好寫成和php容器中根目錄一樣。但是不一點(diǎn)非要一模一樣,如果不一樣在配置nginx的時(shí)候需要注意
–name 容器的名字
–link 與另外一個(gè)容器建立起聯(lián)系
3.然后進(jìn)入nginx容器,修改nginx的配置文件讓它支持php
[root@centos-04 /]# docker exec -it nginx bash root@c00e5859f876:/#
參數(shù)說(shuō)明
-t 在容器里生產(chǎn)一個(gè)偽終端
-i 對(duì)容器內(nèi)的標(biāo)準(zhǔn)輸入 (STDIN) 進(jìn)行交互
4.在容器里找到nginx的配置文件,默認(rèn)是在/etc/nginx目錄下
nginx的配置文件都在 /etc/nginx/ 下面,可以看到熟悉的 conf.d 文件夾,明顯里面是用戶自定義配置文件的位置。
修改自定義配置
default.conf文件內(nèi)容如下:
安裝vim
root@c00e5859f876:/# apt update
root@c00e5859f876:/# apt install vim
root@c00e5859f876:/# vim /etc/nginx/nginx.conf
location ~ .php$ {
root /var/www/html;
fastcgi_index index.php;
fastcgi_pass phpfpm:9000;//這里改成我們之前--link進(jìn)來(lái)的容器,也可以直接用php容器的ip
fastcgi_param SCRIPT_FILENAME $document_root$fastcdi_script_name;//如果你的根目錄和php容器的根目錄不一樣,這里的$document_root需要換成你php下的根目錄,不然php就找不到文件了
include fastcgi_params;
}
5.重啟nginx容器
[root@centos-04 /]# docker restart c00e5859f876
6.index.php內(nèi)容改為下面內(nèi)容
<?php
try {
$con = new PDO('mysql:host=mysql;dbname=test', 'xuye', 'xy123456');
$con->query('SET NAMES UTF8');
$res = $con->query('select * from test');
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
echo "id:{$row['id']} name:{$row['name']}";
}
} catch (PDOException $e) {
echo '錯(cuò)誤原因:' . $e->getMessage();
}
7.訪問(wèn)測(cè)試,成功了
http://192.168.242.130/index.php
補(bǔ)充
顯示所有容器ip
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
總結(jié)