容器管理
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
Test Page for the Nginx HTTP Server on Fedora
Welcome to nginx on Fedora!
This page is used to test the proper operation of the
nginx 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.
Website Administrator
This is the default index.html page that
is distributed with nginx on
Fedora. It is located in
/usr/share/nginx/html.
You should now put your content in a location of
your choice and edit the root configuration
directive in the nginx
configuration file
/etc/nginx/nginx.conf.
[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 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
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 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
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 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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 mtu 1500
inet6 fe80::3097:1eff:fe0e:2a68 prefixlen 64 scopeid 0x20
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 mtu 1500
inet6 fe80::90a7:97ff:fed0:344d prefixlen 64 scopeid 0x20
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 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
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 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
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 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 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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 mtu 1500
inet6 fe80::3097:1eff:fe0e:2a68 prefixlen 64 scopeid 0x20
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 mtu 1500
inet6 fe80::90a7:97ff:fed0:344d prefixlen 64 scopeid 0x20
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 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/[email protected]
[root@centos-02 ~]# docker exec -it 8df534 bash
[root@8df53477e027 /]# ifconfig
eth1: flags=4163 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 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 [email protected]
# 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 [email protected]
---> [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= ...
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
or
subscription-manager repos --disable=
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=.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内容改为下面内容
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)