一、Docker 镜像命令
1.1 Docker 的帮助命令
docker version #显示docker的版本信息。
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
[root@VM-12-16-centos ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
[root@VM-12-16-centos ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11869 [OK]
mariadb MariaDB Server is a high performing open sou… 4536 [OK]
Docker 命令的帮助文档地址:
https://docs.docker.com/engine/reference/commandline/build/
1.2 查看镜像
docker images 查看所有本地的主机上的镜像
[root@VM-12-16-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
解释:
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
可选项 Options:
-a, --all #列出 所有镜像
-q, --quiet # 只显示镜像的id
[root@VM-12-16-centos ~]# docker images -aq #显示所有镜像的id
feb5d9fea6a5
1.3 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@VM-12-16-centos ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载: docker image 的核心 联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
# 签名 防伪
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#等价于
docker pull mysql:5.7
docker pull docker.io/library/mysql:5.7
[root@VM-12-16-centos ~]# docker pull mysql:5.7 #指定版本
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
1.4 删除镜像
删除指定的镜像
[root@VM-12-16-centos ~]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 11 days ago 448MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos latest 5d0da3dc9764 3 months ago 231MB
[root@VM-12-16-centos ~]# docker rmi -f feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[root@VM-12-16-centos ~]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 11 days ago 448MB
centos latest 5d0da3dc9764 3 months ago 231MB
docker rmi -f 镜像id 镜像id 镜像id 镜像id #删除指定的镜像
docker rmi -f $(docker images -aq) #删除全部的镜像
二、Docker 容器命令
我们有了镜像才可以创建容器,Linux,现在下载 centos 镜像来学习。
[root@VM-12-16-centos ~]# docker container docker容器命令
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
新建容器并启动
docker run [可选参数] image | docker container run [可选参数] image
#参书说明
--name="Name" #容器名字 tomcat01 tomcat02 用来区分容器
-d #后台方式运行
-it #使用交互方式运行,进入容器查看内容
-p #指定容器的端口 (下面五种方式)
-p 8080(宿主机):8080(容器)
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P(大写) #随机指定端口
[root@VM-12-16-centos ~]# docker run -it centos /bin/bash
[root@18f2d8b71b5d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
exit # 退出容器
ctrl +P +Q #容器不停止退出
docker ps -a #查看当前运行的容器和历史运行的容器
docker ps #查看当前正在运行的容器
docker ps -aq #只显示容器id
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf
docker rm -f $(docker ps -aq) #删除指定的容器
docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
三、常用的其他命令
后台启动命令
[root@VM-12-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-12-16-centos ~]# docker run -d centos
216d356762901ac88b305c1b67bcd403b145dcee39802e8d7ed1d42cb5acf28b
[root@VM-12-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
问题 docker ps,发现 centos 停止了:
常见的坑,docker 容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止 。
例如 nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
[root@VM-12-16-centos ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
查看 n 行日志
[root@VM-12-16-centos ~]# docker run -d centos /bin/bash -c "while true;do echo 6666;sleep 1;done";
4d241f98e11a39ebbaeaa185cad34d72882f9c4971f6088ab8a791e09111ee41
[root@VM-12-16-centos ~]# docker logs -t --tail 10 4d241f98e11a39ebbaeaa185cad34d72882f9c4971f6088ab8a791e09111ee41;
2022-01-01T06:33:46.297490857Z 6666
2022-01-01T06:33:47.300527856Z 6666
2022-01-01T06:33:48.302687132Z 6666
2022-01-01T06:33:49.305016734Z 6666
2022-01-01T06:33:50.306697383Z 6666
2022-01-01T06:33:51.309025177Z 6666
2022-01-01T06:33:52.311626742Z 6666
2022-01-01T06:33:53.313950858Z 6666
2022-01-01T06:33:54.316572975Z 6666
2022-01-01T06:33:55.318794408Z 6666
[root@VM-12-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d241f98e11a centos "/bin/bash -c 'while…" About a minute ago Up About a minute nice_dewdney
docker logs -ft 容器id #显示日志信息(一直更新)
查看容器中进程信息
[root@VM-12-16-centos ~]# docker top 4d241f98e11a
UID PID PPID C STIME TTY TIME CMD
root 3663 3646 0 14:32 ? 00:00:00 /bin/bash -c while true;do echo 6666;sleep 1;done
root 5089 3663 0 14:38 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
[root@VM-12-16-centos ~]# docker inspect 4d241f98e11a
[
{
"Id": "4d241f98e11a39ebbaeaa185cad34d72882f9c4971f6088ab8a791e09111ee41",
"Created": "2022-01-01T06:32:56.747538926Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo 6666;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3663,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-01T06:32:57.179147942Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/4d241f98e11a39ebbaeaa185cad34d72882f9c4971f6088ab8a791e09111ee41/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/4d241f98e11a39ebbaeaa185cad34d72882f9c4971f6088ab8a791e09111ee41/hostname",
进入当前正在运行的容器
我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
[root@VM-12-16-centos ~]# docker exec -it 4d241f98e11a /bin/bash
[root@4d241f98e11a /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
方式二 docker attach 容器id
[root@VM-12-16-centos ~]# docker attach 4d241f98e11a #此时他会直接运行前面的脚本程序
区别:
- docker exec 进入当前容器后开启一个新的终端,可以在里面操作。(常用)
- docker attach 进入容器正在执行的终端
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 主机目的路径
步骤:进入centos容器,新建文件,退出,拷贝该文件到主机上,查看拷贝成功