Docker常用命令
帮助命令
https://docs.docker.com/registry/spec/api/
docker version
docker info
docker 命令 --help
docker启动
systemctl start docker
service docker start
重启docker服务
systemctl restart docker
sudo service docker restart
关闭docker
systemctl stop docker
service docker stop
查看是否启动成功
docker ps -a
镜像命令
[root@localhost var]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0 040bdb29ab37 2 days ago 649MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
## 说明
REPOSITORY 镜像仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
docker search 查找镜像
[root@localhost var]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10380 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3848 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 758 [OK]
percona Percona Server is a fork of the MySQL relati… 519 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
#带过滤条件的
[root@localhost var]# docker search mysql -f STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10380 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3848 [OK]
下载镜像 docker pull
#docker pull 镜像名[:tag]
[root@localhost var]# docker pull mysql
Using default tag: latest #命令不写tag,默认下载最新版的 latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete #分层下载,docker image的核心 ,联合文件系统
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
69dd1fbf9190: Pull complete
5346a60dcee8: Pull complete
ef28da371fc9: Pull complete
fd04d935b852: Pull complete
050c49742ea2: Pull complete
Digest: sha256:0fd2898dc1c946b34dceaccc3b80d38b1049285c1dab70df7480de62265d6213 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@localhost var]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists #分层下载,已经存在的就不会再次下载
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
7065aaa2655f: Pull complete
b4bc531db40f: Pull complete
8c3e9d7c9815: Pull complete
fadfb9734ed2: Pull complete
Digest: sha256:e08834258fcc0efd01df358222333919df53d4a0d9b2a54da05b204b822e3b7b
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost var]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0 040bdb29ab37 2 days ago 649MB
mysql 5.7 cc8775c0fe94 3 days ago 449MB
mysql latest d4c3cafb11d5 3 days ago 545MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
删除镜像 docker rmi
[root@localhost var]# docker rmi -f d4c3cafb11d5
Untagged: mysql:latest
Untagged: mysql@sha256:0fd2898dc1c946b34dceaccc3b80d38b1049285c1dab70df7480de62265d6213
Deleted: sha256:d4c3cafb11d573699728f9e7de10d1b976089b01298c0360e03f0afd9a1a8b36
Deleted: sha256:3ce5f6a2175f88412c0e8241e9298e721cface0773ea2fd70f1fdaf0e606c7fa
Deleted: sha256:f840f44fb69007f9b78a9bb552882753066b8e4a7835aa8f1be83ba50466e346
Deleted: sha256:d5c65d80478a6f623a715e67e39c4756ce0d4d09348881d1e86b87033322c70c
Deleted: sha256:2a33bb3c3abf34893e689effc0f204c998eb1c8c137050808b9b2725589e3c66
Deleted: sha256:537f09811c9ed9704ccbda6a316f3a2bf346530a296753de6545025b2dfde532
# 删除所有的镜像
[root@localhost var]# docker rmi -f $(docker images -aq)
#删除 指定的多个镜像
[root@localhost var]# docker rmi -f d4c3cafb11d5 a8s4ojaoi116e
容器命令
说明:有了镜像才能创建容器
下载centos学习
[root@localhost var]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
新建容器并启动
#docker run [可选参数] image
#参数说明
--name='NAME' 容器名称,用来区分创建的容器
-d 后台运行方式
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8088:8080
-p 主机ip:端口
-p 主机端口:容器端口(常用)
-p 容器端口
#测试 启动并进入容器
[root@localhost var]# docker run -it centos /bin/bash
[root@bc297469a043 /]#
# 查看容器内的centos, 基础版本,很多内容不全
[root@bc297469a043 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#退出命令 退出容器,当前容器会自动停止
[root@bc297469a043 /]# exit
exit
按住 ctrl键加q加p 键 可以退出容器,并保持容器运行
列出所有运行的容器
# docker ps
#列出正在运行的容器
-a #列出正在运行的容器+历史运行过的容器
-n=? #显示最近创建的?个容器
-q #列出正在运行的容器id
-lq #列出最后运行的容器id
[root@localhost var]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost var]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc297469a043 centos "/bin/bash" 3 minutes ago Exited (127) About a minute ago vibrant_jemison
92