Docker入门二
容器管理
1.docker create創建一個容器,但容器并沒啟動,就和我們創建虛擬機一樣,創建了虛擬機后沒啟動
[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.啟動容器
[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運行容器
[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退出,然后查看容器發現沒有f4eba170402c,因為我們退出了之前的bash,這是因為我們退出了沒有加-d,加上-a參數查看狀態為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.給容器起個名字
[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.可以直接用名字進入容器
[root@centos-02 ~]# docker exec -it centos6_1 bash [root@5e32153dbbd6 /]#
7.容器執行完直接刪除、命令執行完容器就退出,執行完不留任何痕跡。
[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查看容器運行歷史信息
[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,刪除一個啟動的容器加-f參數
[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 ~]#
倉庫管理
1.用registry鏡像搭建私有倉庫,我們可以用這個鏡像來運行一個容器,這個容器就是我們的私有倉庫。
[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.啟動鏡像
[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加一個標簽
[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.指定私有倉庫的地址
[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.從私有倉庫下載鏡像
[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 ~]#
數據管理
https://blog.csdn.net/u010846177/article/details/54356670
1.掛載本地的目錄到容器里
[root@centos-02 ~]# docker run -tid -v /data/:/data centos_with_net bash (左邊的data是宿主機目錄,右邊的data是容器目錄)(這個叫數據卷容器類似于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.我們在容器里創建一個123目錄,也會在宿主機上自動建一個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.創建一個新的容器,然后把centos_with_net作為一個數據卷
[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 /]#
數據卷備份恢復
1.上圖說明:我們的數據卷容器共享的目錄叫data,宿主機有一個叫data/backup目錄,data/backup目錄和新建容器里面的backup目錄是映射的,也就是說兩個目錄的文件是相同的,現在我們新建容器掛載了數據卷容器,意味著數據卷容器的data和新建容器的data下面的文件是一樣的,現在我們把新建容器里面的data下面的數據拷貝到新建容器里面的backup目錄下,也就是把數據卷容器下面data的數據拷貝到了新建容器data/backup下面
Docker網絡模式
1.虛擬機網絡模式:
橋接:網絡和宿主機網絡是同等關系,他們鏈接了同一個路由器,鏈接了同一個交換機,網段也是一樣的。
NAT:它把宿主機的網絡做成了一個和路由器設備一樣,它實現了網絡地址轉換,只要宿主機能聯網它就能聯網,他們的網段是不一樣的。
僅主機:就是主機和宿主機做一個鏈接,它們直接連了一根網線
2.docker網絡模式:
host模式:
container模式:
none模式:
bridge模式:
1.如何讓外部訪問容器呢?做端口映射,操作過程:首先進入一個容器里,在容器里安裝一個web服務nginx,把這個帶nginx的容器做一個打包或者說導成一個鏡像,鏡像完成之后我們再啟動一個容器,在啟動容器的時候我們加一個端口映射
[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.下面我們把容器導成鏡像
[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.新建的容器啟動nginx的時候會報錯,這是因為dbus-daemon服務沒有起來
[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.牛逼啟動成功
[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端口訪問(配置nginx一定不能清空iptables是,清空之后瀏覽器就不能訪問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 ~]#
配置橋接網絡
1.pipework網絡的一種額外模式,能夠實現讓docker容器和宿主機使用同一個交換機,他們在同一個網段下,這樣就可以直接和外面的網絡通信,這樣我們就可以把docker容器看做成一臺獨立的服務器,我們要操作的網卡是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網卡做一個橋接,拷貝ens33網卡為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上是沒有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.下面我們開啟一個容器
[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.給容器添加一個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.測試
[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.創建鏡像的方法:可以用現有的鏡像搞成容器,我們在容器里做一些操作把它導出成鏡像。還有一種方式去openvz的官網下載一個模板,現在我們通過dockerfile創建鏡像
2.from:指定基于那個鏡像
Dockerfile格式(下)
Dockerfile示例(安裝nginx上)
1.編輯Dockerfile文件,執行提示我們網絡不能工作,網絡不同,這和我們之前配置的pipework有關,我們需要重啟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讓它自動綁定之前的br0網絡
[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.查看是否能聯網
[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.牛逼成功了,下面我們測試下。
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部署服務
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文件并啟動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.然后我們可以通過命令 docker images 查看我們剛剛拉下來的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.運行并啟動一個容器,通過以下命令
參數說明
-d 讓容器在后臺運行
-p 添加主機到容器的端口映射
-e 設置環境變量,這里是設置mysql的root用戶的初始密碼,這個必須設置
–name 容器的名字,隨便取,但是必須唯一
[root@centos-04 /]# docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.6
ps:其實我們可以僅僅使用docker run命令就行了。docker run會先去pull,然后再create。個人習慣先把鏡像pull下來,在run的時候會很快。
4.接下來我們就可以通過命令docker ps -a 查看我們剛剛創建的容器
[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 /]#
這里我們可以看到我的容器狀態的Up狀態,表示容器正在運行,并且把可以看到主機和容器的端口映射關系。
5.接下來,我們就可以進入到我們剛剛創建的容器中,輸入命令
參數說明
-t 在容器里生產一個偽終端
-i 對容器內的標準輸入 (STDIN) 進行交互
[root@centos-04 /]# docker exec -it mysql bash root@f42d98924858:/#
容器中默認是沒有vim的,所以我們首先要安裝vim,需要注意的是安裝前記得先執行apt update命令,不然安裝會出現問題。
進入到mysql容器后,我們通過創建一個遠程可以訪問的用戶,這樣我們就能從別的主機訪問到我們的數據庫了。
docker安裝php-fpm
1.同樣首先我們拉取php-fpm的鏡像
[root@centos-04 /]# docker pull php:7.0-fpm
2.再創建一個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 /]#
參數說明
-d 讓容器在后臺運行
-p 添加主機到容器的端口映射
-v 添加目錄映射,即主機上的/var/nginx/www/html和容器中/var/www/html目錄是同步的
–name 容器的名字
–link 與另外一個容器建立起聯系,這樣我們就可以在當前容器中去使用另一個容器里的服務。
這里如果不指定–link參數其實也是可以得,因為容易本身也是有ip的且唯一,所以我們也可以直接利用ip去訪問容器。
3.然后進入到我們的容器,然后我們在/var/www/html目錄下新建一個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.我們可以看到該目錄下新建了一個php文件
接下來我們回到我們的主機上面,訪問一下我們主機上/var/nginx/www/html
[root@centos-04 /]# ls /var/nginx/www/html index.php [root@centos-04 /]#
我們發現我們在容器里的/var/www/html目錄中新建的文件也在主機的/var/nginx/www/html目錄中,因為在創建容器的時候,我們已經把主機中的目錄掛載到了容器中去了。
5.因為后面我要使用pdo模塊進行測試,所以我需要自己安裝pdo_mysql模塊,在docker容器中可以這樣來安裝
[root@centos-04 /]# docker exec -it phpfpm bash root@c58be577ba9f:/var/www/html# docker-php-ext-install pdo_mysql
6.然后我們可以通過命令php -m查看我們的php的所有擴展模塊,我們可以去看到我們剛剛安裝的pdo_mysql擴展也在里面
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.我們從倉庫里去拉取一個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.接下來運行nginx容器(如果想要把nginx的配置文件掛載到宿主機需要先目錄和文件并且配置文件不能有錯,因為是直接同步到nginx容器中的對應配置,啟動容器的時候nginx直接用的,如果有錯誤nginx啟動不成功)
[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 /]#
參數說明:
-d 讓容器在后臺運行
-p 添加主機到容器的端口映射
-v 添加目錄映射,這里最好nginx容器的根目錄最好寫成和php容器中根目錄一樣。但是不一點非要一模一樣,如果不一樣在配置nginx的時候需要注意
–name 容器的名字
–link 與另外一個容器建立起聯系
3.然后進入nginx容器,修改nginx的配置文件讓它支持php
[root@centos-04 /]# docker exec -it nginx bash root@c00e5859f876:/#
參數說明
-t 在容器里生產一個偽終端
-i 對容器內的標準輸入 (STDIN) 進行交互
4.在容器里找到nginx的配置文件,默認是在/etc/nginx目錄下
nginx的配置文件都在 /etc/nginx/ 下面,可以看到熟悉的 conf.d 文件夾,明顯里面是用戶自定義配置文件的位置。
修改自定義配置
default.conf文件內容如下:
安裝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進來的容器,也可以直接用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內容改為下面內容
<?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 '錯誤原因:' . $e->getMessage();
}
7.訪問測試,成功了
http://192.168.242.130/index.php
補充
顯示所有容器ip
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
總結