一些 Docker 基础指令
Posted GoldenaArcher
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一些 Docker 基础指令相关的知识,希望对你有一定的参考价值。
一些 Docker 基础指令
-
docker run <image name> <command>
创建并运行一个 docker 容器,是
docker create <image name>
与docker start <image name>
的结合command
中的内容可以重制默认指令,如:➜ ~ docker run busybox echo hi there hi there ➜ ~ docker run busybox echo bye there bye there ➜ ~ docker run busybox ls bin dev etc home proc root sys tmp usr var
注:
command
必须存在于容器之中,如busybox
中不存在java
,那么运行就会失败,如:➜ ~ docker run busybox java --version docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "java": executable file not found in $PATH: unknown. ERRO[0000] error waiting for container: context canceled
-
docker ps -a
-a
为可选 flag,加上-a
会显示所有运行过的容器;不加则显示所有正在运行的容器 -
docker start -a <container name>
-a
的 flag 会让 docker 监听当前容器的输出值,如:➜ ~ docker start -a d03798f77923 bye there ➜ ~ docker start d03798f77923 d03798f77923
-
docker system prune
清理指令,清理的内容包括:
-
all stopped containers
所有停止的容器
-
all networks not used by at least one container
所有不被容器使用的网络
-
all dangling images
dangling image
好像说是创建了但是未命名的容器 -
all dangling build cache
未被使用的缓存?
-
总归说这个命令会将所有的缓存也清除,下一次再创建容器的时候需要重新从 DockerHub 上拉镜像。
-
docker logs <container id>
换句话说,使用
docker logs <container id> > <filename>
也可以写出日志 -
docker stop <container id>
如果容器十秒钟内没有被停止的话,那么就会执行下面这条指令
-
docker kill <container id>
-
docker exec -it <container id> <command>
-it
这个 flag 使得容器允许接受用于提供的指令。即-i -t
两个 flag。该指令可以在容器内执行额外的命令,如:
# 运行redis ➜ ~ docker run redis 1:C 17 Sep 2022 22:35:48.352 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 17 Sep 2022 22:35:48.352 # Redis version=7.0.4, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 17 Sep 2022 22:35:48.352 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 17 Sep 2022 22:35:48.353 * monotonic clock: POSIX clock_gettime 1:M 17 Sep 2022 22:35:48.353 * Running mode=standalone, port=6379. 1:M 17 Sep 2022 22:35:48.354 # Server initialized 1:M 17 Sep 2022 22:35:48.354 * Ready to accept connections
在另一个终端中:
➜ ~ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 494018fcc41c redis "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp optimistic_merkle ➜ ~ docker exec -it 494018fcc41c redis-cli 127.0.0.1:6379>
如果在没有安装 redis 的前提下,直接运行
redis-cli
会报错。运行
docker exec 494018fcc41c redis-cli
则不会有任何的效果。 -
docker exec -it <container id> sh
和上面的命令相似,不过可以直接打开容器中的命令行:
➜ ~ docker exec -it 494018fcc41c sh # redis-cli 127.0.0.1:6379> # echo hi there hi there # export b=5 # echo $b 5 #
这个 sh 中推出不是
ctrl+c
,而是ctrl+d
。 -
docker run -it <container name> sh
在开始启动容器时就能运行 sh,如:
➜ ~ docker run -it busybox sh / # ls bin dev etc home proc root sys tmp usr var / #
-
docker build .
根据已有的 Dockerfile 生成一个新的镜像
-
docker build -t dockerId/Repo/Project-name:version
tag docker 容器,tag 完可以不需要使用
<container id>
去启动容器,如:➜ redis-img docker build -t ga/docker-server:latest . [+] Building 0.3s (6/6) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 37B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:latest 0.2s => [1/2] FROM docker.io/library/alpine@sha256:bc41182d7ef5ffc53a40b044e7 0.0s => CACHED [2/2] RUN apk add --update redis 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:2ad794a60a4d842a8563c81027aaa1102332d33bcd08d 0.0s => => naming to docker.io/ga/docker-server:latest 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them ➜ redis-img ➜ redis-img ➜ redis-img docker run ga/docker-server 1:C 18 Sep 2022 02:42:22.839 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 18 Sep 2022 02:42:22.839 # Redis version=7.0.4, bits=64, commit=4bd17450, modified=0, pid=1, just started 1:C 18 Sep 2022 02:42:22.839 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 18 Sep 2022 02:42:22.840 * monotonic clock: POSIX clock_gettime 1:M 18 Sep 2022 02:42:22.841 * Running mode=standalone, port=6379. 1:M 18 Sep 2022 02:42:22.841 # Server initialized 1:M 18 Sep 2022 02:42:22.842 * Ready to accept connections
以上是关于一些 Docker 基础指令的主要内容,如果未能解决你的问题,请参考以下文章