学习任何一门技术都是需要不断的去淬炼,才能融会贯通,下面都是为大家列出一些常用的docker镜像命令。也是我在学习docker时反复去用的,希望对需要的小伙伴有帮助
查看本机上的docker的镜像
docker images
[root@yingxiong ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATE 镜像的创建时间
SIZE 镜像的大小
#docker images的参数用法
docker images -a
docker images -q
[root@yingxiong ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
[root@localhost ~]# docker images -q
d1165f221234
#可选项
-a 表示列出所有镜像
-q 表示显示镜像的ID
docker search搜索镜像
docker search mysql
[root@yingxiong ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation鈥 10877 [OK]
mariadb MariaDB Server is a high performing open sou鈥 4103 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create鈥 808 [OK]
percona Percona Server is a fork of the MySQL relati鈥 537 [OK]
#可选项
--filter=STARS=3000 那么搜索的镜像体积就是3000的
#docker search的参数用法
docker search mysql --filter=STARS=3000
[root@yingxiong ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation鈥 10877 [OK]
mariadb MariaDB Server is a high performing open sou鈥 4103 [OK]
# 表示搜索3000以上的
docker pull下载镜像
docker pull mysql
[root@yingxiong ~]# docker pull mysql
Using default tag: latest #表示默认下载最新版的mysql
latest: Pulling from library/mysql
69692152171a: Pull complete #分层下载,docker images的核心
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的真实地址
#docker pull指定地址下载
docker pull mysql:5.7
invalid reference format: repository name must be lowercase
[root@yingxiong ~]# 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 rmi -f 删除镜像
docker rmi -f d1165f221234
[root@yingxiong ~]# docker rmi -f d1165f221234 #后面可以添加多个容器ID
Untagged: hello-world:latest
Untagged: hello-world@sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
[root@yingxiong ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 2c9028880e58 4 days ago 447MB
mysql latest c0cdc95609f1 4 days ago 556MB
#docker rmi -f删除全部容器
docker rmi -f $(docker images -qa)
[root@yingxiong ~]# docker rmi -f $(docker images -qa)
Untagged: mysql:5.7
Untagged: mysql@sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Deleted: sha256:2c9028880e5814e8923c278d7e2059f9066d56608a21cd3f83a01e3337bacd68
Deleted: sha256:c49c5c776f1bc87cdfff451ef39ce16a1ef45829e10203f4d9a153a6889ec15e
Deleted: sha256:8345316eca77700e62470611446529113579712a787d356e5c8656a41c244aee
Deleted: sha256:8ae51b87111404bd3e3bde4115ea2fe3fd2bb2cf67158460423c361a24df156b
Deleted: sha256:9d5afda6f6dcf8dd59aef5c02099f1d3b3b0c9ae4f2bb7a61627613e8cdfe562
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
[root@yingxiong ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
使用容器的前提就是在有了镜像之后,所以会了镜像命令就要会使用容器命令,想不到吧,两者彼此依赖,但是使用命令的格式是差不多的,只要多加练习也是很好掌握的
下载一个centos镜像进行测试学习
docker pull centos
[root@yingxiong ~]# 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 ip:主机端口:容器端口
-p 容器端口:主机端口
-p 容器端口
-P 随机指定端口
#启动并进入容器
docker run -it centos /bin/bash
[root@yingxiong ~]# docker run -it centos /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
#ls 查看命令
[root@e828d60a6e99 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#exit 停止后退出容器
[root@e828d60a6e99 /]# exit
exit
#快捷键Ctrl+p+q 不停止后退出容器
[root@e73c05ef5db4 /]# [root@yingxiong ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e73c05ef5db4 centos "/bin/bash" 27 seconds ago Up 27 seconds elegant_engelbart
docker ps查看运行的容器
docker ps
docker pa -a
docker ps -aq
[root@yingxiong ~]# docker ps #查看正在运行的容器(这里没有正在运行的)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@yingxiong ~]# docker ps -a #查看已经运行过和正在运行的容器(这里是已经运行过但停了的)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e828d60a6e99 centos "/bin/bash" 8 minutes ago Exited (130) 5 minutes ago sleepy_archimedes
9595dfffd087 d1165f221234 "/hello" 4 days ago Exited (0) 4 days ago festive_proskuriakova
[root@yingxiong ~]# docker ps -aq #查看运行过的容器的ID
e828d60a6e99
9595dfffd087
docker rm 容器ID 删除容器
docker rm 9595dfffd087
[root@yingxiong ~]# docker rm 9595dfffd087 #不能删除正在运行的镜像,如果要那么必须加上-f
9595dfffd087
#docker rm -f删除全部镜像
docker rm -f $(docker ps -qa)
[root@yingxiong ~]# docker rm -f $(docker ps -qa)
e73c05ef5db4
e828d60a6e99
[root@yingxiong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
启动和停止容器的操作
docker start 容器ID #启动一个容器
docker restart 容器ID #重启容器
docker stop 容器ID #停止当前运行容器
docker kill 容器ID #强制干掉当前正在运行的容器
docker start b6f8e461d36
docker stop b6f8e461d36
[root@yingxiong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6f8e4681d36 centos "/bin/bash" 26 seconds ago Exited (0) 17 seconds ago nifty_swirles
[root@yingxiong ~]# docker start b6f8e4681d36
b6f8e4681d36
[root@yingxiong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6f8e4681d36 centos "/bin/bash" About a minute ago Up 7 seconds nifty_swirles
[root@yingxiong ~]# docker stop b6f8e4681d36
b6f8e4681d36
[root@yingxiong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6f8e4681d36 centos "/bin/bash" About a minute ago Exited (0) 3 seconds ago nifty_swirles
这里呢也是给大家总结一些其它的常用命令,多学一点总是好的。
后台启动命令
#命令docker run -d 镜像名
[root@yingxiong ~]# docker run -d centos
WARNING: IPv4 forwarding is disabled. Networking will not work.
c2615273d0932bc5a2a85b044f829f7358ef2ede3f77be46550a8c9620454eeb
[root@yingxiong ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#问题发现centos是停止的
#常见的坑,docker容器使用后台运行就必须要有一个前台进程,不然就会自动停止
查看日志
#docker logs -t -f --tail 镜像ID
查看进程
#docker top 镜像ID
docker top 43e2abb1b613
#这里要先运行容器,然后查看进程
[root@yingxiong ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43e2abb1b613 centos "/bin/bash" 2 minutes ago Up 2 minutes lucid_swanson
[root@yingxiong ~]# docker top 43e2abb1b613
UID PID PPID C STIME TTY TIME CMD
root 14961 14939 0 08:17 pts/0
查看镜像的元数据
#docker inspect 镜像ID
[root@yingxiong ~]# docker inspect 43e2abb1b613
[
{
"Id": "43e2abb1b613d42c7ed68f246dde95f0886acc46bb193a449f148f77281dfe3b",
"Created": "2021-05-16T12:17:15.74088968Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 14961,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-16T12:17:16.120921247Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
#能看到很多的信息
进入当前正在运行的容器
#docker exec -it 镜像ID
docker exec -it 43e2bb1b613
[root@yingxiong ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43e2abb1b613 centos "/bin/bash" 29 minutes ago Up 29 minutes lucid_swanson
[root@yingxiong ~]# docker exec -it 43e2abb1b613 /bin//bash
[root@43e2abb1b613 /]#
#方式二,docker attach 容器ID
docker attach 5506558efb7f
[root@5506558efb7f /]# [root@yingxiong ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5506558efb7f centos "/bin/bash" 18 seconds ago Up 17 seconds exciting_germain
[root@yingxiong ~]# docker attach 5506558efb7f
[root@5506558efb7f /]#
#总结
#docker exec 进入容器并开启一个新的容器,可以操作
#docker attach 进入正在执行的容器,不会启动新的进程