启动docker
service docker start # 启动docker
service docker stop # 关闭docker
sudo service docker restart # 重新启动docker
常用命令
docker version # 显示版本
docker info # 显示镜像详细信息,更加详细
docker 命令 --help # 万能命令
systemctl start docker # 启动docker
镜像命令
https://docs.docker.com/engine/reference/commandline/build/
官方文档地址
#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE #镜像的大小
# 可选项
# docker images --help
-a, --all # 列出全部的镜像
-q, --quiet # 只列出镜像的ID
docker search 搜索镜像
docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10905 [OK]
mariadb MariaDB Server is a high performing open sou… 4117 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 810 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 88
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 85
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 52 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 43
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
prom/mysqld-exporter 39 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
# 可选项
-f, --filter filter #
--format string
--limit int
--no-trunc
# 通过收藏过滤
--filter=STARS=3000 # 搜索出来的镜像的是STRARS大于3000的
docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10910 [OK]
mariadb MariaDB Server is a high performing open sou… 4119 [OK]
下载镜像
root@root ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet
#下载 docker pull 名称 [:TAG]
# docker pull mysql # 默认下载最新的
[root@root ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
69692152171a: Pull complete # 分层下载,docker image的核心,联合文件系统
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于
docker pull mysql == docker pull docker.io/library/mysql:latest
# 下载其他版本,指定版本
[root@root ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Already exists # 前面一样不需要下载
1651b0be3df3: Already exists
951da7386bc8: Already exists
0f86c95aa242: Already exists
37ba2d8bd4fe: Already exists
6d278bb05e94: Already exists
497efbd93a3e: Already exists
a023ae82eef5: Pull complete # 下载前面不同的
e76c35f20ee7: Pull complete
e887524d2ef9: Pull complete
ccb65627e1c3: Pull complete
Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
删除镜像
docker images # 查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 2c9028880e58 11 days ago 447MB
mysql latest c0cdc95609f1 11 days ago 556MB
hello-world latest d1165f221234 2 months ago 13.3kB
[root@root ~]# docker rmi -f 2c9028880e58 # 根据ID删除镜像--指定删除
[root@root ~]# docker rmi -f 2c9028880e58 镜像ID 镜像ID # 根据ID删除多个镜像--指定删除
Untagged: mysql:5.7
Untagged: mysql@sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Deleted: sha256:2c9028880e5814e8923c278d7e2059f9066d56608a21cd3f83a01e3337bacd68
Deleted: sha256:c49c5c776f1bc87cdfff451ef39ce16a1ef45829e10203f4d9a153a6889ec15e
Deleted: sha256:8345316eca77700e62470611446529113579712a787d356e5c8656a41c244aee
Deleted: sha256:8ae51b87111404bd3e3bde4115ea2fe3fd2bb2cf67158460423c361a24df156b
Deleted: sha256:9d5afda6f6dcf8dd59aef5c02099f1d3b3b0c9ae4f2bb7a61627613e8cdfe562
docker rmi -f $(docker images -aq) # 递归删除镜像(删除全部容器)
Untagged: mysql:latest
Untagged: mysql@sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Deleted: sha256:c0cdc95609f1fc1daf2c7cae05ebd6adcf7b5c614b4f424949554a24012e3c09
Deleted: sha256:137bebc5ea278e61127e13cc7312fd83874cd19e392ee87252b532f0162fbd56
Deleted: sha256:7ed0de2ad4e43c97f58fa9e81fba73700167ef9f8a9970685059e0109749a56b
Deleted: sha256:9375660fbff871cd29c86b8643be60e376bfc96e99a3d7e8f93d74cd61500705
Deleted: sha256:d8a47065d005ac34d81017677330ce096eb5562eeb971e2db12b0e200fdb1cb6
Deleted: sha256:ca13c8ad9df5d824d5a259a927eaa6c04a60f022bc2abe8fc7866cf4b2b366f4
Deleted: sha256:7af1865d5c19316c3dc0829a2ee2b3a744ae756f7fec9c213d3afc5f1f6ed306
Deleted: sha256:f205c8f3c8aaa6376442b34c0c2062738461d37e0aa16ba021cd7e09c67213c2
Deleted: sha256:d690e8a8242cf13cbe98c5b2faffdd0fc7e6c4c13425b5da31de991aa1f89a76
Deleted: sha256:24efeee958e9f3d859fe15540e9296d5aaa6d3eb3b5f5494a2e8370608a4cfaa
Deleted: sha256:654f2ffede3bb536fd62d04c9c7b7826e890828bec92182634e38684959b2498
Deleted: sha256:de478a06eaa676052e665faa0b07d86a007f4b87cf82eb46a258742dc2d32260
Deleted: sha256:02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33
Untagged: hello-world:latest
Untagged: hello-world@sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
容器命令
有了镜像才可以传教容器
准备工作,先下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名称 tomcat01,tomcat02,用来区分容器
-d 后台方式运行
-i,-t 使用交互方式运行
-p 指定容器端口 -p 8080:8080
-p ip:z主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
#测试,启动并进入容器
docker run -it centos /bin/bash
[root@395155bb34ba /]# ls # 服务器里面的服务器--容器内的服务器
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
eixt # 退出到主机
列出所有运行容器
# docker ps 命令
# 列出当前运行的容器
-a # 列出当前运行的容器+历史运行的容器
-n=? #显示最近创建的容器
-q # 只显示容器的编号
[root@root ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@root ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
395155bb34ba centos "/bin/bash" 14 minutes ago Exited (0) 8 minutes ago suspicious_northcutt
6dbb55f64333 d1165f221234 "/hello" 16 hours ago Exited (0) 16 hours ago amazing_lichterman
[root@root ~]# docker ps -a -n=1 # 限制个数
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
395155bb34ba centos "/bin/bash" 19 minutes ago Exited (0) 13 minutes ago suspicious_northcutt
[root@root ~]# docker ps -a -q
395155bb34ba
6dbb55f64333
退出容器
exit # 直接容器停止并退出
Ctrl +P + Q #容器不停止退出
删除容器
docker rm 容器Id # 删除指定的容器 不能删除正在运行的容器,如果要强制删除rm -f
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q|xargs docker rm # 删除所有的容器
[root@root ~]# docker ps -a -q 等价于 docker ps -aq
7c28ac5bc840
395155bb34ba
6dbb55f64333
[root@root ~]# docker ps -aq
7c28ac5bc840
395155bb34ba
6dbb55f64333
启动容器和停止
docker start 容器id # 启动容器
docker restart 容器id # 查询启动容器
docker stop 容器id # 停止当前正在容器
docker kill 容器id # 强制停止当前容器
常用其他命令
后台启动
# 命令 docker run -d 镜像名
[root@root ~]# docker run -d centos
6025f5cf760514eb5b1890176261cc1bd4df601d10423c1c2d28d80f6601fbe3
[root@root ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题docker ps ,发现centos停止了
# 常见的坑:docker 容器使用后台运行,就必须有一个前台进程,docker发现没有就会自动停止
# nginx ,容器启动后,发现直接没有提供服务,就会立即停止,就是没有程序了
查看日志
docker logs - f -t -taill n 容器id # 容器,没有日志,n代表显示日志的条数
-tf # 显示全部的
--tail number # 显示日志的条数
查看进程信息
# 命令 docker top 容器id
[root@root /]# docker top a7d2a39a090d
UID PID PPID C STIME TTY TIME CMD
root 187190 187170 0 13:33 pts/0 00:00:00 /bin/bash
查看元数据
docker inspect a7d2a39a090d # docker inspect 容器id