Docker常用命令

Docker常用命令

严少安 Lv.6

Docker Usage

docker的四种状态(start/pause/restart/stop)

  • 基础命令
1
2
docker info
docker search
  • 拉取镜像
1
2
docker pull
docker pull centos:7.3.1611
  • 容器状态
1
2
3
4
5
6
7
8
9
# start
docker start happy_hahaha
# restart
docker restart happy_hahaha
# stop
docker stop happy_hahaha
# pause
docker pause happy_hahaha
docker unpause happy_hahaha
  • 运行容器
1
2
3
4
5
6
7
8
9
10
11
# docker run
docker run --name centos7.3 -i -t centos:7.3.1611 /bin/bash
# --detach => -d
docker run --name centos7.3_d -i -t -d centos:7.3.1611 /bin/bash
# 自动重启
docker run --restart=always
# 挂载数据卷
docker run --rm -it -v /host/data/:/data:rw happy_hahaha /bin/bash
docker run --name centos7.3-elk -dit -v /Volumes/HDD/dockerdata:/data:rw -p 8080:80 -p 5601:5601 -p 9200:9200 --privileged=true shawnyan/docker:centos7.3.1611 /usr/sbin/init
# rename
docker rename centos7.3 centos7.3.1611
  • 容器交互
1
2
docker exec -t -i centos7.3 /bin/bash
docker exec -it centos7.3 /bin/bash
  • 查看镜像
1
2
3
4
5
6
7
docker images
docker images shawnyan/docker
docker image ls -a
docker image rm <CONTAINER ID>
docker image rm `docker image ls -a -q`
# 构建镜像的每一层
docker history <id>
  • 删除容器
1
2
docker rm <CONTAINER ID>
docker rm `docker ps -a -q`
  • 监控容器
1
2
3
4
5
6
7
docker ps
docker ps -a
docker container ls -a
# status
docker stats centos7.3
docker logs centos7.3
docker logs -ft centos7.3
  • 查看容器信息
1
2
3
4
5
# In Liquid format double '{}' means variables.
docker inspect centos7.3
# docker inspect centos7.3 --format='\{\{ .ID \}\}'
# docker inspect centos7.3 --format='\{\{ .NetworkSettings.IPAddress \}\}'
# docker inspect centos7.3 --format='\{\{ .State.Status \}\}'
  • 导出导入镜像
1
2
3
4
# export
docker save -o ol7.tar oraclelinux:7-slim
# import
docker load -i ol7.tar
  • 提交定制容器
1
2
3
docker commit -m "comment" <CONTAINER ID> <user/repo>
docker commit -m "centos7.3 core" -a "shawnyan" e819c4cb23a4 shawnyan/docker:centos7.3
docker commit -m "centos7_v2" -a "shawnyan" centos7 shawnyan/docker:centos7_v2
  • 推送镜像
1
docker push <user/repo:tag>
  • 本文标题: Docker常用命令
  • 本文作者: 严少安
  • 创建时间: 2018-02-18 23:40:01
  • 更新时间: 2018-02-18 23:40:01
  • 本文链接: https://shawnyan.cn/2018/docker/docker-usage/
  • 版权声明: 本文为原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上文章出处,文章链接和本声明。
 评论
目录