docker 常用命令
[root@localhost ~]# docker versionClient: Version: 17.05.0-ce API version: 1.29 Go version: go1.7.5 Git commit: 89658be Built: Thu May 4 22:06:25 2017 OS/Arch: linux/amd64Server: Version: 17.05.0-ce API version: 1.29 (minimum version 1.12) Go version: go1.7.5 Git commit: 89658be Built: Thu May 4 22:06:25 2017 OS/Arch: linux/amd64 Experimental: false[root@localhost ~]# docker infoContainers: 3 Running: 1 Paused: 0 Stopped: 2Images: 29Server Version: 17.05.0-ceStorage Driver: overlay Backing Filesystem: xfs Supports d_type: trueLogging Driver: json-fileCgroup Driver: cgroupfsPlugins: Volume: local Network: bridge host macvlan null overlaySwarm: inactiveRuntimes: runcDefault Runtime: runcInit Binary: docker-initcontainerd version: 9048e5e50717ea4497b757314bad98ea3763c145runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228init version: 949e6faSecurity Options: seccomp Profile: defaultKernel Version: 3.10.0-693.el7.x86_64Operating System: CentOS Linux 7 (Core)OSType: linuxArchitecture: x86_64CPUs: 4Total Memory: 1.781GiBName: localhost.localdomainID: QHTO:DMIZ:PMAS:SRXP:KTR4:CH7T:3IKV:6IWK:TRKT:I6VP:CVES:PNSPDocker Root Dir: /var/lib/dockerDebug Mode (client): falseDebug Mode (server): falseRegistry: https://index.docker.io/v1/Experimental: falseInsecure Registries: 127.0.0.0/8Live Restore Enabled: false[root@localhost ~]# docker logs -f 2cff //查看容器日志WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.Could not load host key: /etc/ssh/ssh_host_ecdsa_keyCould not load host key: /etc/ssh/ssh_host_ed25519_key^C[root@localhost ~]# docker ps //查看正在运行的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2cff51b765c2 sshdocker "/usr/sbin/sshd -D" 22 minutes ago Up 22 minutes 0.0.0.0:32769->22/tcp mysshdocker[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2cff51b765c2 sshdocker "/usr/sbin/sshd -D" 22 minutes ago Up 22 minutes 0.0.0.0:32769->22/tcp mysshdocker2b3b55ec6693 sshdocker "/usr/sbin/sshd -D" 4 hours ago Exited (0) 35 minutes ago mysshdcontainer2323f1ff98d7 bd0c225f3c21 "/bin/sh -c 'yum i..." 4 hours ago Exited (1) 4 hours ago eager_meitner[root@localhost ~]# docker ps -a -q //为查看所有的容器,包括已经停止的。2cff51b765c22b3b55ec66932323f1ff98d7
删除所有容器docker rm $(docker ps -a -q)删除单个容器docker rm <容器名orid> 停止、启动、杀死一个容器docker stop <容器名orid> docker start <容器名orid> docker kill <容器名orid> 查看所有镜像docker p_w_picpaths删除所有镜像docker rmi $(docker p_w_picpaths | grep none | awk '{print $3}' | sort -r)运行一个新容器,同时为它命名、端口映射、文件夹映射。以redmine镜像为例docker run --name redmine -p 9003:80 -p 9023:22 -d -v /var/redmine/files:/redmine/files -v /var/redmine/mysql:/var/lib/mysql sameersbn/redmine一个容器连接到另一个容器docker run -i -t --name sonar -d -link mmysql:db tpires/sonar-serversonar容器连接到mmysql容器,并将mmysql容器重命名为db。这样,sonar容器就可以使用db的相关的环境变量了。拉取镜像docker pull <镜像名:tag> 如docker pull sameersbn/redmine:latest# 保存镜像到一个tar包; -o, --output="" Write to an file $docker save p_w_picpath_name -o file_path # 加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file $docker load -i file_path # 机器a $docker save p_w_picpath_name > /home/save.tar # 使用scp将save.tar拷到机器b上,然后: $docker load < /home/save.tar eg:[root@localhost ~]# docker save centos7/ssh:version1 -o /home/save.tar[root@localhost ~]# ls /home/save.tar/home/save.tar[root@localhost ~]#构建自己的镜像docker build -t <镜像名>如Dockerfile在当前路径:docker build -t xx/gitlab .eg:docker build -t centos7:tag1 /root/dockerfile/centos7/ 镜像名> 镜像名:tag> 容器名orid> 容器名orid> 容器名orid> 容器名orid>
Docker容器进入的方式。转载自 //http://www.cnblogs.com/xhyan/p/6593075.html
docker attach ssh nsenter exec
[root@localhost ~]# docker exec -it 2cff /bin/bash[root@2cff51b765c2 /]# ifconfigeth0: flags=4163mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 5255 bytes 15870393 (15.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4615 bytes 271329 (264.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: 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@2cff51b765c2 /]# [root@localhost ~]#[root@localhost ~]# ssh admin@172.17.0.2admin@172.17.0.2's password:[admin@2cff51b765c2 ~]$ ifconfigeth0: flags=4163 mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 5289 bytes 15875283 (15.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4639 bytes 275951 (269.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: 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[admin@2cff51b765c2 ~]$ exitlogoutConnection to 172.17.0.2 closed.[root@localhost ~]# docke ps-bash: docke: command not found[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2cff51b765c2 sshdocker "/usr/sbin/sshd -D" 12 minutes ago Up 12 minutes 0.0.0.0:32769->22/tcp mysshdocker
重新查看container的stdout# 启动top命令,后台运行$ ID=$(sudo docker run -d ubuntu /usr/bin/top -b)# 获取正在running的container的输出$ sudo docker attach $IDtop - 02:05:52 up 3:05, 0 users, load average: 0.01, 0.02, 0.05Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombieCpu(s): 0.1%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%stMem: 373572k total, 355560k used, 18012k free, 27872k buffersSwap: 786428k total, 0k used, 786428k free, 221740k cached^C$$ sudo docker stop $IDeg[root@localhost ~]# docker run -d centos7/ssh /usr/bin/top -bUnable to find p_w_picpath 'centos7/ssh:latest' locally^C[root@localhost ~]# docker run -d centos7/ssh:version1 /usr/bin/top -ba17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101[root@localhost ~]# docker attach"docker attach" requires exactly 1 argument(s).See 'docker attach --help'.Usage: docker attach [OPTIONS] CONTAINERAttach local standard input, output, and error streams to a running container[root@localhost ~]# docker attach a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101top - 16:08:55 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie%Cpu(s): 0.1 us, 1.8 sy, 0.0 ni, 98.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1867048 total, 473488 free, 225960 used, 1167600 buff/cacheKiB Swap: 2097148 total, 2095864 free, 1284 used. 1329652 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 40188 1676 1296 R 0.0 0.1 0:00.05 toptop - 16:08:58 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1867048 total, 473360 free, 226088 used, 1167600 buff/cacheKiB Swap: 2097148 total, 2095864 free, 1284 used. 1329528 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 40188 1676 1296 R 0.0 0.1 0:00.05 toptop - 16:09:01 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie%Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1867048 total, 473360 free, 226088 used, 1167600 buff/cacheKiB Swap: 2097148 total, 2095864 free, 1284 used. 1329528 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 40188 1676 1296 R 0.0 0.1 0:00.05 toptop - 16:09:04 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie%Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1867048 total, 473268 free, 226176 used, 1167604 buff/cacheKiB Swap: 2097148 total, 2095864 free, 1284 used. 1329440 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 40188 1676 1296 R 0.0 0.1 0:00.05 top^C[root@localhost ~]#[root@localhost ~]#[root@localhost ~]# docker stop a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101
docker commit
docker commit命令-提交一个新的p_w_picpath
[root@localhost ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2cff51b765c2 sshdocker "/usr/sbin/sshd -D" 6 minutes ago Up 6 minutes 0.0.0.0:32769->22/tcp mysshdocker[root@localhost ~]# docker commit 2cff centos7/ssh:version1sha256:7369170d76efb411ea023bc795aa2b2e403077c9d324f9334429cad35df7a005[root@localhost ~]# docker p_w_picpathsREPOSITORY TAG IMAGE ID CREATED SIZEcentos7/ssh version1 7369170d76ef 9 seconds ago 422MBsshdocker latest 3f5fd510439f 4 hours ago 323MBbd0c225f3c21 4 hours ago 281MBcentos7 tag1 b7350ef2bb8d 5 hours ago 621MBcentos centos7.1.1503 c288e5945774 5 weeks ago 212MB[root@localhost ~]#