Docker 实战(三):Docker 常用命令

images 查看镜像

1
2
3
4
5
6
7
8
$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c6cfe59eb987 6 days ago 357 MB
batizhao/ubuntu latest a15b93298276 9 days ago 297.2 MB
ubuntu 16.04 f753707788c5 5 weeks ago 127.2 MB
ubuntu latest f753707788c5 5 weeks ago 127.2 MB
swarm latest 942fd5fd357e 3 months ago 19.47 MB

ps 查看容器

运行中的

1
2
3
4
$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6eef58b3544 tomcat "catalina.sh run" 16 minutes ago Up 14 minutes 0.0.0.0:32770->8080/tcp evil_wozniak

所有的

1
2
3
4
5
6
7
8
9
10
$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6eef58b3544 tomcat "catalina.sh run" 17 minutes ago Up 15 minutes 0.0.0.0:32770->8080/tcp evil_wozniak
d4faf2e9badf tomcat "catalina.sh run" 35 minutes ago Exited (130) 26 minutes ago ecstatic_bohr
47d97451e8a1 tomcat "catalina.sh run" 36 minutes ago Exited (130) 35 minutes ago berserk_mclean
54c65a764bf0 batizhao/ubuntu "/usr/bin/supervisord" 7 days ago Exited (0) 7 days ago jolly_allen
9246f78c4b8f batizhao/ubuntu "/usr/bin/supervisord" 7 days ago Exited (0) 7 days ago stupefied_gates
d49845097aa0 batizhao/ubuntu "/usr/bin/supervisord" 7 days ago Exited (0) 7 days ago sad_stallman
311ba7bb9ffd swarm "/swarm --help" 7 days ago Exited (0) 7 days ago naughty_bhaskara

run 运行容器

启动 MySQL Docker 容器

1
2
3

$ docker run -itd -e MYSQL_ROOT_PASSWORD=passw0rd -e MYSQL_ROOT_HOST=% -e MYSQL_DATABASE=iwamp2 -p 3306:3306 --name=mysql batizhao/mysql:latest

Tomcat Docker 容器连接宿主机 MySQL

1
2
3

$ docker run -itd -p 9000:8080 --name iwamp -v /Users/batizhao/Downloads/web:/opt/tomcat/webapps --add-host='iwamp2.dev:172.17.0.1' batizhao/tomcat:8-jre8

Tomcat Docker 容器连接 MySQL Docker 容器

1
2
3

$ docker run -it -p 9000:8080 --name iwamp2 -v /Users/batizhao/Downloads/web:/opt/tomcat/webapps --link mysql:iwamp2.dev batizhao/tomcat:8-jre8

Tomcat Docker 容器连接物理 Oracle 数据库

1
2
3

$ docker run -itd -p 9000:8080 --name iwamp -v /Users/batizhao/Downloads/web:/opt/tomcat/webapps --add-host='iwamp.dev:172.31.21.216' batizhao/tomcat:8-jre8

rm 删除容器

删除单个容器

1
2
3
$ docker rm 47d97451e8a1

47d97451e8a1

删除所有停止运行的容器

1
$ docker rm $(docker ps -a -q)

rmi 删除镜像

删除 REPOSITORY 为 swarm,TAG 为 latest 的镜像

1
2
3
$ docker rmi swarm

Error response from daemon: conflict: unable to remove repository reference "swarm" (must force) - container 311ba7bb9ffd is using its referenced image 942fd5fd357e

如果提示以上错误,可以先删除所关联容器,或者直接使用 -f 参数

1
2
3
4
5
$ docker rmi -f swarm

Untagged: swarm:latest
Untagged: swarm@sha256:c9e1b4d4e399946c0542accf30f9a73500d6b0b075e152ed1c792214d3509d70
Deleted: sha256:942fd5fd357e2fe2fcecbaf3dd77c313f22ce18a84a5a4d288c0df407a61e623

批量删除 tag 为 none 的镜像

1
2
3
4
$ docker images|grep none|awk '{print $3}'|xargs docker rmi -f

Deleted: sha256:d43fcd0c191c0fa7ae1df73ea59ed374a5e9b5c25788ddc4183800257cc8a38f
Deleted: sha256:59e448d53f303721ca12a40513c3dbc8651cc046311186825b9d7eec0805baac

port 查看端口映射

1
2
$ docker port e6eef58b3544 8080
0.0.0.0:32770

exec 进入运行中的容器

1
2
3
$ docker exec -it e6eef58b3544 bash

root@e6eef58b3544:/usr/local/tomcat#