docker pull centos # For example: docker pull centos:5.11 or docker pull
docker images
dcoker container
启动容器:docker run –name -h hostname
停止容器:docker stop CONTAINER ID
查看容器:docker ps
进入容器:docker exec | docker attach
删除容器:docker rm option [-f]
运行容器日志:docker logs CONTAINER ID
1 2
[root@server1 ~]# docker run centos /bin/echo 'Hello world' Hello world
查看容器运行状态
1 2 3
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8c0e090d5b87 centos "/bin/echo 'Hello wo About a minute ago Exited (0) About a minute ago sharp_cori
[root@server1 ~]# docker run --name mydocker -it centos /bin/bash
[root@4875a51a97cb /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11720 1864 ? Ss 10:12 0:00 /bin/bash root 18 0.0 0.0 35836 1488 ? R+ 10:13 0:00 ps aux
[root@4875a51a97cb tmp]# ls ks-script-06FeP3
[root@4875a51a97cb tmp]# mkdir test
[root@4875a51a97cb tmp]# ls ks-script-06FeP3 test
[root@4875a51a97cb tmp]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)
[root@4875a51a97cb tmp]# exit exit
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4875a51a97cb centos "/bin/bash" 12 minutes ago Exited (0) 27 seconds ago mydocker 8c0e090d5b87 centos "/bin/echo 'Hello wo 16 minutes ago Exited (0) 16 minutes ago sharp_cori
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eae421cf2af9 centos:6.6 "/bin/bash" 4 minutes ago Up 19 seconds mycentos 4875a51a97cb centos "/bin/bash" 19 minutes ago Exited (0) 7 minutes ago mydocker 8c0e090d5b87 centos "/bin/echo 'Hello wo 24 minutes ago Exited (0) 24 minutes ago sharp_cori
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eae421cf2af9 centos:6.6 "/bin/bash" 5 minutes ago Exited (137) 8 seconds ago mycentos 4875a51a97cb centos "/bin/bash" 20 minutes ago Exited (0) 8 minutes ago mydocker 8c0e090d5b87 centos "/bin/echo 'Hello wo 25 minutes ago Exited (0) 25 minutes ago sharp_cori
[root@server1 ~]# docker run -d --name myid centos e2e9964282ab789a8f7cab4a397cbf22a8f91d83dbb80173698f1f64aa46b75e [root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e2e9964282ab centos "/bin/bash" 3 seconds ago Exited (0) 3 seconds ago myid
[root@server1 ~]# docker run -d --name mycentos2 centos:6.6 /bin/bash 986e7a8976c2be2d2bb1707eacd3277a2c3368f95bb02ed0919c2abf7e4ef5ed [root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 986e7a8976c2 centos:6.6 "/bin/bash" 5 seconds ago Exited (0) 4 seconds ago mycentos2
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d7a73f9f499 nginx "nginx -g 'daemon of About a minute ago Up About a minute 80/tcp, 443/tcp mynginx
[root@server1 ~]# docker ps -a|grep nginx 2d7a73f9f499 nginx "nginx -g 'daemon of 2 minutes ago Up 2 minutes 80/tcp, 443/tcp mynginx
删除容器,必须先停止容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[root@server1 ~]# docker rm 2d7a73f9f499 Error response from daemon: Cannot destroy container 2d7a73f9f499: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f Error: failed to remove containers: [2d7a73f9f499]
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d7a73f9f499 nginx "nginx -g 'daemon of 7 minutes ago Up 7 minutes 80/tcp, 443/tcp mynginx 8c0e090d5b87 centos "/bin/echo 'Hello wo 46 minutes ago Exited (0) 46 minutes ago sharp_cori
[root@server1 ~]# docker ps -l #最后一个运行容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d7a73f9f499 nginx "nginx -g 'daemon of 15 minutes ago Up 15 minutes 80/tcp, 443/tcp mynginx
[root@server1 ~]# docker attach 2d7a73f9f499 #此命令有些时候无法进去容器,或者不支持,如果ctrl+c,那么容器会自动停止 ^C[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d7a73f9f499 nginx "nginx -g 'daemon of 19 minutes ago Exited (0) 3 seconds ago mynginx
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d7a73f9f499 nginx "nginx -g 'daemon of 23 minutes ago Up 11 seconds 80/tcp, 443/tcp mynginx
[root@server1 ~]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.dad9e43403b6 no veth44be944 virbr0 8000.525400353d8e yes virbr0-nic
[root@server1 ~]# iptables -t nat -L -n Chain PREROUTING (policy ACCEPT) target prot opt source destination DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
[root@server1 ~]# docker run -P -d --name mynginx1 nginx 711c8bc3077d27196c4263cc75bb7fcb7f0bf2c02fb120054f011b9b9b134847
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c8bc3077d nginx "nginx -g 'daemon of 27 seconds ago Up 27 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp mynginx1 2d7a73f9f499 nginx "nginx -g 'daemon of 59 minutes ago Up 35 minutes 80/tcp, 443/tcp mynginx
[root@server1 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39979df7a280 nginx "nginx -g 'daemon of 22 seconds ago Up 21 seconds 443/tcp, 0.0.0.0:91->80/tcp
[root@server1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE nginx latest 2b1e900b514d 2 weeks ago 134.4 MB centos latest 60e65a8e4030 6 weeks ago 196.6 MB centos 6.6 12c9d795d85a 4 months ago 202.6 MB
[root@server1 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39979df7a280 nginx "nginx -g 'daemon of 2 hours ago Up 2 hours 443/tcp, 0.0.0.0:91->80/tcp mynginx2
#启动一个容器centos,并且创建一个/data的数据卷 [root@server1 ~]# docker run -it --name volume-test1 -h nginx -v /data centos [root@nginx /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11720 1868 ? Ss 14:11 0:00 /bin/bash
[root@nginx /]# ls -ld /data/ drwxr-xr-x 2 root root 4096 Feb 11 14:11 /data/
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES af47fece9654 centos "/bin/bash" 7 seconds ago Exited (0) 4 seconds ago volume-test1 39979df7a280 nginx "nginx -g 'daemon of 2 hours ago Up 2 hours 443/tcp, 0.0.0.0:91->80/tcp mynginx2
[root@centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0031b268b26 centos:6.6 "/bin/bash" About an hour ago Exited (0) 24 seconds ago nginx [root@centos ~]# docker commit -m "centos6.6 and nginx-1.9.11" f0031b268b26 skyneteye/nginx:v1 9e78179003020986a7c970c314faff22706b55d762913d7b7f9e2d16702371e5
#测试构建的镜像 [root@centos ~]# docker run -d -p 92:80 skyneteye/nginx:v1 012e567670badb4983ba17066077ac27247c4befa8e999bedf4769a885db0795
============== 运行完成之后就自动退出了….
[root@centos ~]# docker run -it -h nginx skyneteye/nginx:v1 [root@nginx /]# vim /etc/rc.local [root@nginx /]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff.
[root@centos nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0031b268b26 centos:6.6 "/bin/bash" 2 hours ago Exited (0) About an hour ago nginx
[root@centos nginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 247d1840b02e nginx:1.9.11 "nginx" 18 seconds ago Up 17 seconds 0.0.0.0:80->80/tcp distracted_meitner