目录
Docker的基本使用
常用命令
启动Docker
查看Docker的运行状态
重启Docker
停止Docker服务
查看命令相关信息
镜像命令
列出本地主机上的镜像
搜索某个镜像的名称
拉取指定的镜像到本地
查看镜像/容器/数据卷所占的空间
删除某个镜像
容器命令
启动容器
列出正在运行的容器
退出容器
启动已停止运行的容器
重启容器
停止容器
强制停止容器
删除已停止的容器
启动守护式容器与进入容器
查看容器日志
查看容器内运行的进程
查看容器内部具体细节命令
进入容器内部
从容器内拷贝文件到主机上
Docker的基本使用
常用命令
启动Docker
[root@localhost ~]# systemctl start docker
查看Docker的运行状态
[root@localhost ~]# systemctl status docker
重启Docker
[root@localhost ~]# systemctl restart docker
停止Docker服务
[root@localhost ~]# systemctl stop docker
查看命令相关信息
docker 相关命令 --help
[root@localhost ~]# docker cp --helpUsage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATHCopy files/folders between a container and the local filesystemUse '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.Aliases:docker container cp, docker cpOptions:-a, --archive Archive mode (copy all uid/gid information)-L, --follow-link Always follow symbol link in SRC_PATH-q, --quiet Suppress progress output during copy. Progress output is automatically suppressed ifno terminal is attached
镜像命令
列出本地主机上的镜像
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB
redis 6.2.7 4b1123a829a1 11 months ago 113MB
apache/rocketmq 4.9.3 9a54e5b5b6c1 20 months ago 535MB
alpine latest c059bfaa849c 2 years ago 5.58MB
apacherocketmq/rocketmq-dashboard latest eae6c5db5d11 2 years ago 738MB
portainer/portainer latest 580c0e4e98b0 2 years ago 79.1MB
参数 | 含义 |
REPOSITORY | 表示镜像的仓库源 |
TAG | 镜像的标签版本号。在下载镜像的时候,如果默认不加版本号,就会下载默认最新版本的镜像。如果加上指定版本号的镜像,下载的就是对应版本的镜像。 |
IMAGE ID | 镜像ID |
CREATED | 镜像创建时间 |
SIZE | 镜像大小 |
搜索某个镜像的名称
[root@localhost ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 12498 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 61
redislabs/redisinsight RedisInsight - The GUI for Redis 94
redis/redis-stack-server redis-stack-server installs a Redis server w… 60
redis/redis-stack redis-stack installs a Redis server with add… 82
redislabs/rebloom A probablistic datatypes module for Redis 25 [OK]
redislabs/redis Clustered in-memory database engine compatib… 40
docker search --limit N XXX
只列出N个镜像,默认为25个
[root@localhost ~]# docker search --limit 5 redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 12498 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 61
redislabs/redisinsight RedisInsight - The GUI for Redis 94
redis/redis-stack-server redis-stack-server installs a Redis server w… 60
redis/redis-stack redis-stack installs a Redis server with add… 82
拉取指定的镜像到本地
docker pull XXX 没带标签就拉取最新版本的镜像
docker pull XXX:6.1.10 拉取指定标签的镜像
[root@localhost ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
查看镜像/容器/数据卷所占的空间
docker system df
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 9 8 3.063GB 112.7MB (3%)
Containers 15 5 10.4MB 1.993MB (19%)
Local Volumes 4 4 218.8kB 0B (0%)
Build Cache 0 0 0B 0B
删除某个镜像
docker rmi XXX
docker rmi 某个镜像的IMAGE ID
[root@localhost ~]# docker rmi redis
Untagged: redis:latest
Untagged: redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Deleted: sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631
Deleted: sha256:49c70179bc923a7d48583d58e2b6c21bde1787edf42ed1f8de9e9b96e2e88e65
Deleted: sha256:396e06df5d1120368a7a8a4fd1e5467cdc2dd4083660890df078c654596ddc1c
Deleted: sha256:434d118df2e9edb51238f6ba46e9efdfa21be68e88f54787531aa39a720a0740
Deleted: sha256:2047f09c412ff06f4e2ee8a25d105055e714d99000711e27a55072e640796294
Deleted: sha256:13d71c9ccb39b206211dd1900d06aa1984b0f5ab8abaa628c70b3eb733303a65
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
不能删除正在运行容器的镜像,可以加上参数-f强制删除
docker rmi -f XXX
docker rmi -f 镜像名1:TAG 镜像名2:TAG 可以删除多个镜像
容器命令
启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS | 含义 |
--name="容器新名字" | 为容器指定一个名称; |
-d | 后台运行容器并返回容器ID,也即启动守护式容器(后台运行); |
-i | 以交互模式运行容器,通常与 -t 同时使用; -t: |
-t | 为容器重新分配一个伪输入终端,通常与 -i 同时使用; 也即启动交互式容器( 前台有伪终端,等待交互 ) ; |
-P | 随机端口映射,大写P |
-p | 指定端口映射,小写p |
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@168fd40e5a27:/#
参数说明:
列出正在运行的容器
docker ps
root@168fd40e5a27:/# [root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
168fd40e5a27 ubuntu "/bin/bash" About a minute ago Up About a minute jovial_einstein
e2592a496c60 portainer/portainer "/portainer" 7 weeks ago Up 34 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp portainer
2984b012f3be apacherocketmq/rocketmq-dashboard:latest "sh -c 'java $JAVA_O…" 7 weeks ago Up 34 minutes 0.0.0.0:8280->8080/tcp, :::8280->8080/tcp rocketmq-console
799a4159b6cf apache/rocketmq:4.9.3 "sh mqbroker -c /hom…" 7 weeks ago Restarting (255) 41 seconds ago rocketmq-broker
e7cc34338422 redis:6.2.7 "docker-entrypoint.s…" 2 months ago Up 34 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis6
12b28b13b6cd mysql:8.0.20 "docker-entrypoint.s…" 2 months ago Up 34 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
可以携带的参数:
退出容器
- exit 容器停止
- ctrl + p + q 容器不停止
启动已停止运行的容器
[root@localhost ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95cece2337ab ubuntu "/bin/bash" 24 seconds ago Up 23 seconds frosty_fermi
27f56882f7fe ubuntu "/bin/bash" 43 seconds ago Up 42 seconds charming_lamport
[root@localhost ~]# docker start 95cece2337ab
95cece2337ab
重启容器
[root@localhost ~]# docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95cece2337ab ubuntu "/bin/bash" About a minute ago Up About a minute frosty_fermi
27f56882f7fe ubuntu "/bin/bash" About a minute ago Up About a minute charming_lamport
cef340972b6b ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago eloquent_buck
[root@localhost ~]# docker restart cef340972b6b
cef340972b6b
停止容器
docker stop 容器ID或者容器名
[root@localhost ~]# docker stop cef340972b6b
cef340972b6b
强制停止容器
docker kill 容器ID或者容器名称
[root@localhost ~]# docker kill 95cece2337ab
95cece2337ab
删除已停止的容器
docker rm 容器ID
[root@localhost ~]# docker rm 95cece2337ab
95cece2337ab
删除正在运行的容器需要加上-f
docker rm -f XXX
启动守护式容器与进入容器
如果是前台运行
0365c86064ff2f9fdf46a6d90c12685bb43f1b56f5c5bc6560abfda3e1dbc3ac
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0365c86064ff redis "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 6379/tcp
查看容器日志
docker logs 容器ID
[root@localhost ~]# docker logs 0365c86064ff
1:C 25 Nov 2023 07:30:25.693 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 25 Nov 2023 07:30:25.693 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 25 Nov 2023 07:30:25.693 # 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 25 Nov 2023 07:30:25.694 * monotonic clock: POSIX clock_gettime
1:M 25 Nov 2023 07:30:25.698 * Running mode=standalone, port=6379.
1:M 25 Nov 2023 07:30:25.698 # Server initialized
1:M 25 Nov 2023 07:30:25.698 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 25 Nov 2023 07:30:25.698 * Ready to accept connections
查看容器内运行的进程
[root@localhost ~]# docker top 0365c86064ff
UID PID PPID C STIME TTY TIME CMD
systemd+ 26755 26736 0 15:30 ? 00:00:00 redis-server *:6379
查看容器内部具体细节命令
进入容器内部
docker exec -it 容器ID /bin/bash
[root@localhost ~]# docker exec -it 0365c86064ff /bin/bash
root@0365c86064ff:/data#
使用exit退出后容器依然在运行
也可以通过docker attach 容器ID的方式进入容器,但是exit退出会导致容器停止
从容器内拷贝文件到主机上
docker cp 容器ID:容器内路径 目的主机路径