docker 常用命令
[root@localhost ~]# docker version Client: 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/amd64 Server: 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 info Containers: 3 Running: 1 Paused: 0 Stopped: 2 Images: 29 Server Version: 17.05.0-ce Storage Driver: overlay Backing Filesystem: xfs Supports d_type: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 9048e5e50717ea4497b757314bad98ea3763c145 runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-693.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 1.781GiB Name: localhost.localdomain ID: QHTO:DMIZ:PMAS:SRXP:KTR4:CH7T:3IKV:6IWK:TRKT:I6VP:CVES:PNSP Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Experimental: false Insecure Registries: 127.0.0.0/8 Live 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_key Could not load host key: /etc/ssh/ssh_host_ed25519_key ^C [root@localhost ~]# docker ps //查看正在运行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cff51b765c2 sshdocker "/usr/sbin/sshd -D" 22 minutes ago Up 22 minutes 0.0.0.0:32769->22/tcp mysshdocker [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cff51b765c2 sshdocker "/usr/sbin/sshd -D" 22 minutes ago Up 22 minutes 0.0.0.0:32769->22/tcp mysshdocker 2b3b55ec6693 sshdocker "/usr/sbin/sshd -D" 4 hours ago Exited (0) 35 minutes ago mysshdcontainer 2323f1ff98d7 bd0c225f3c21 "/bin/sh -c 'yum i..." 4 hours ago Exited (1) 4 hours ago eager_meitner [root@localhost ~]# docker ps -a -q //为查看所有的容器,包括已经停止的。 2cff51b765c2 2b3b55ec6693 2323f1ff98d7
删除所有容器 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-server sonar 容器连接到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路径> 如Dockerfile在当前路径: docker build -t xx/gitlab . eg: docker build -t centos7:tag1 /root/dockerfile/centos7/
Docker容器进入的方式。转载自 //http://www.cnblogs.com/xhyan/p/6593075.html
docker attach ssh nsenter exec
[root@localhost ~]# docker exec -it 2cff /bin/bash [root@2cff51b765c2 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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 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 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@2cff51b765c2 /]# [root@localhost ~]# [root@localhost ~]# ssh admin@172.17.0.2 admin@172.17.0.2's password: [admin@2cff51b765c2 ~]$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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 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 [admin@2cff51b765c2 ~]$ exit logout Connection to 172.17.0.2 closed. [root@localhost ~]# docke ps -bash: docke: command not found [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cff51b765c2 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 $ID top - 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 zombie Cpu(s): 0.1%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 373572k total, 355560k used, 18012k free, 27872k buffers Swap: 786428k total, 0k used, 786428k free, 221740k cached ^C$ $ sudo docker stop $ID eg [root@localhost ~]# docker run -d centos7/ssh /usr/bin/top -b Unable to find p_w_picpath 'centos7/ssh:latest' locally ^C [root@localhost ~]# docker run -d centos7/ssh:version1 /usr/bin/top -b a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101 [root@localhost ~]# docker attach "docker attach" requires exactly 1 argument(s). See 'docker attach --help'. Usage: docker attach [OPTIONS] CONTAINER Attach local standard input, output, and error streams to a running container [root@localhost ~]# docker attach a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101 top - 16:08:55 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09 Tasks: 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 st KiB Mem : 1867048 total, 473488 free, 225960 used, 1167600 buff/cache KiB 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 top top - 16:08:58 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09 Tasks: 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 st KiB Mem : 1867048 total, 473360 free, 226088 used, 1167600 buff/cache KiB 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 top top - 16:09:01 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09 Tasks: 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 st KiB Mem : 1867048 total, 473360 free, 226088 used, 1167600 buff/cache KiB 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 top top - 16:09:04 up 2 days, 1:23, 0 users, load average: 0.00, 0.07, 0.09 Tasks: 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 st KiB Mem : 1867048 total, 473268 free, 226176 used, 1167604 buff/cache KiB 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 a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101 a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101
docker commit
docker commit命令-提交一个新的p_w_picpath
[root@localhost ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cff51b765c2 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:version1 sha256:7369170d76efb411ea023bc795aa2b2e403077c9d324f9334429cad35df7a005 [root@localhost ~]# docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED SIZE centos7/ssh version1 7369170d76ef 9 seconds ago 422MB sshdocker latest 3f5fd510439f 4 hours ago 323MB <none> <none> bd0c225f3c21 4 hours ago 281MB centos7 tag1 b7350ef2bb8d 5 hours ago 621MB centos centos7.1.1503 c288e5945774 5 weeks ago 212MB [root@localhost ~]#