一.基本操作
1.查看容器的进程
sulei@sulei:~/mydocker$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
sulei@sulei:~/mydocker$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96f206d59eec shursulei/hello-world "/hello" 6 minutes ago Exited (0) 6 minutes ago zealous_brattain
034d5a6856eb docker-whale "/bin/sh -c '/usr/..." About an hour ago Exited (0) About an hour ago nifty_hypatia
11d962855abb docker/whalesay "cowsay boo-boo" About an hour ago Exited (0) About an hour ago competent_visvesvaraya
f8a63e7ada3d docker/whalesay "cowsay boo" 2 hours ago Exited (0) 2 hours ago cranky_lalande
e42060d4b310 1815c82652c0 "/hello" 11 hours ago Exited (0) 11 hours ago goofy_carson
dd7ae3703bc9 1815c82652c0 "/hello" 23 hours ago Exited (0) 23 hours ago awesome_mirzakhani
68635209c73e 1815c82652c0 "/hello" 23 hours ago Exited (0) 23 hours ago focused_swanson
2.运行一个hello-world的容器
sulei@sulei:~/mydocker$ sudo docker run ubuntu /bin/echo "hello world"
容器运行时间和命令时间相同
3.运行交互式容器
-t伪字符终端,查看帮助sulei@sulei:~/mydocker$ sudo docker run --help
-i交互式(interactive)
sulei@sulei:~/mydocker$ sudo docker run -t -i ubuntu /bin/bash
4.进入交互式的容器中
ls pwd exit(退出交互式shell/ctrl+D)
5.创建容器,以守护进程进行
#使用-d参数运行容器,-c是检查
sulei@sulei:~/mydocker$ sudo docker run -d ubuntu /bin/sh -c "while-true;do echo hello world;sleep 1;done"
#输出一个长串,表示容器的ID,可以通过该ID操作容器
51f8c445ade27c19450a955c56a3da7a9f04f482fc30517f5515caafd5dd9e43
6.查看容器的日志信息log
#logs后面增加的是ID号或者是名称
sulei@sulei:~/mydocker$ sudo docker logs 980ac2396cd4
root@980ac2396cd4:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@980ac2396cd4:/# ls /
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@980ac2396cd4:/# pwd
/
root@980ac2396cd4:/# cd ~
root@980ac2396cd4:~# lss
bash: lss: command not found
root@980ac2396cd4:~# ls
root@980ac2396cd4:~# pwd
/root
root@980ac2396cd4:~# exit
exit
7.停止容器
#suo docker stop con_id
sulei@sulei:~/mydocker$ sudo docker stop 51f8c445ade2
51f8c445ade2
#或者删除
sudo docker rm 容器名称/ID
删除多个容器
#sudo docker stop name1 name2 name3...
停止容器:
sudo docker stop con_id
重启容器:
sudo docker start con_id
二.运行简单的应用程序
1.查看版本
sulei@sulei:~$ sudo docker version
Client:
Version: 17.04.0-ce
API version: 1.28
Go version: go1.7.5
Git commit: 4845c56
Built: Mon Apr 3 17:54:19 2017
OS/Arch: linux/amd64
Server:
Version: 17.04.0-ce
API version: 1.28 (minimum version 1.12)
Go version: go1.7.5
Git commit: 4845c56
Built: Mon Apr 3 17:54:19 2017
OS/Arch: linux/amd64
Experimental: false
2.命令行帮助sudo docker --help
3.查看子命令的帮助sudo docker run --help
4.运行小的程序(端口映射)
5运行容器,使用training/webapp镜像
-d后台方式,-P port端口映射
sudo docker run -d -P training/webapp python app.py
-p映射指定的端口端口;-P映射所有暴露的端口
sudo docker run -d -p 80:5000 training/webapp python app.py
sulei@sulei:~$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da877be48f01 training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0:80->5000/tcp competent_nobel
6.打开浏览器,使用host主机映射的端口访问虚拟的客户机web服务器
http://localhost:49155
7.查看容器的ip地址
sudo docker-machine ip my-docker-vm
8.端口映射
查看port子命令
sudo docker port --help
查看容器中指定端口在宿主机上的映射端口是多少
sudo docker port con_id 5000
输出的值是:0.0.0.0:80
sulei@sulei:~$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa47be9d5633 training/webapp "python app.py" 6 minutes ago Up 6 minutes 0.0.0.0:32769->5000/tcp practical_archimedes
sulei@sulei:~$ sudo docker port fa47be9d5633 5000
0.0.0.0:32769
9.查看web程序的日志
sudo docker logs -f con_id
sulei@sulei:~$ sudo docker logs -f fa47be9d5633
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [04/Jul/2017 08:52:19] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [04/Jul/2017 08:52:19] "GET /favicon.ico HTTP/1.1" 404 -
172.17.0.1 - - [04/Jul/2017 08:52:19] "GET /favicon.ico HTTP/1.1" 404 -
10.查看容器的进程
sudo docker top con_id
sulei@sulei:~$ sudo docker top fa47be9d5633
UID PID PPID C STIME TTY TIME CMD
root 6844 6825 0 16:52 ? 00:00:00
11.检查容器
sudo docker inspect con_id
sulei@sulei:~$ sudo docker inspect fa47be9d5633
"ResolvConfPath": "/var/lib/docker/containers/fa47be9d563362541a2a281ead6c573b81dd8009bf1af406255d280c5b24d008/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/fa47be9d563362541a2a281ead6c573b81dd8009bf1af406255d280c5b24d008/hostname",
"HostsPath": "/var/lib/docker/containers/fa47be9d563362541a2a281ead6c573b81dd8009bf1af406255d280c5b24d008/hosts",
"LogPath": "/var/lib/docker/containers/fa47be9d563362541a2a281ead6c573b81dd8009bf1af406255d280c5b24d008/fa47be9d563362541a2a281ead6c573b81dd8009bf1af406255d280c5b24d008-json.log",
"Name": "/practical_archimedes",
12.停止容器sudo docker stop con_id
13.重启容器sudo docker start con_id
14.查看容器列表sudo docker ps -l
15.删除容器sudo docker rm con_id