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
[[email protected] ~]# docker run centos /bin/echo 'Hello world' Hello world
查看容器运行状态
1 2 3
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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]
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# docker attach 2d7a73f9f499 #此命令有些时候无法进去容器,或者不支持,如果ctrl+c,那么容器会自动停止 ^C[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.dad9e43403b6 no veth44be944 virbr0 8000.525400353d8e yes virbr0-nic
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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的数据卷 [[email protected] ~]# docker run -it --name volume-test1 -h nginx -v /data centos [[email protected] /]# 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
[[email protected] /]# ls -ld /data/ drwxr-xr-x 2 root root 4096 Feb 11 14:11 /data/
[[email protected] ~]# 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
[[email protected] ~]# 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 [[email protected] ~]# docker commit -m "centos6.6 and nginx-1.9.11" f0031b268b26 skyneteye/nginx:v1 9e78179003020986a7c970c314faff22706b55d762913d7b7f9e2d16702371e5
[[email protected] ~]# docker run -it -h nginx skyneteye/nginx:v1 [[email protected] /]# vim /etc/rc.local [[email protected] /]# 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.
[[email protected] 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
[[email protected] 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