目录
Docker的安装
在centos7版本下
1.卸载旧版本Docker
bash
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.首先安装g++与gcc
bash
yum -y install gcc
bash
yum -y install g++
3.安装需要的安装包
bash
yum install -y yum-utils
4.设置镜像仓库地址
bash
yum-config-manager --add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
5.更新yum软件包索引
bash
yum makecache fast
6.安装Docker
bash
yum -y install docker-ce docker-ce-cli containerd.io
7.查看Docker版本
bash
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 24.0.7
API version: 1.43
Go version: go1.20.10
Git commit: afdd53b
Built: Thu Oct 26 09:09:18 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.7
API version: 1.43 (minimum version 1.12)
Go version: go1.20.10
Git commit: 311b9ff
Built: Thu Oct 26 09:08:20 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.24
GitCommit: 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
runc:
Version: 1.1.9
GitCommit: v1.1.9-0-gccaecfc
docker-init:
Version: 0.19.0
GitCommit: de40ad0
8.设置开机自动启动
bash
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Docker安装完成
Docker的使用
常用命令
启动Docker
bash
[root@localhost ~]# systemctl start docker
查看Docker的运行状态
bash
[root@localhost ~]# systemctl status docker
重启Docker
bash
[root@localhost ~]# systemctl restart docker
停止Docker服务
bash
[root@localhost ~]# systemctl stop docker
查看命令相关信息
docker 相关命令 --help
bash
[root@localhost ~]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' 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 cp
Options:
-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 if
no terminal is attached
镜像命令
列出本地主机上的镜像
docker images
bash
[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 | 镜像大小 |
-a : 列出本地所有的镜像(含历史映像层)。
-q : 只显示镜像 ID 。
搜索某个镜像的名称
docker search XXX
bash
[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个
bash
[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 拉取指定标签的镜像
bash
[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
bash
[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
bash
[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 |
bash
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@168fd40e5a27:/#
参数说明:
-i: 交互式操作。
-t: 终端。
ubuntu: ubuntu 镜像。
/bin/bash :放在镜像名后的是命令,这里我们希望有个交互式 Shell ,用来解
释执行 linux 指令。因此用的是 /bin/bash 。
要退出终端,直接输入 exit 。
列出正在运行的容器
docker ps
bash
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
可以携带的参数:
-a : 列出当前所有正在运行的容器 + 历史上运行过的
-l : 显示最近创建的容器。
-n :显示最近 n 个创建的容器。
-q : 只显示正在运行的容器编号。
退出容器
- exit 容器停止
- ctrl + p + q 容器不停止
启动已停止运行的容器
docker start 容器 ID 或者容器名
bash
[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
重启容器
docker restart 容器 ID 或者容器名
bash
[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或者容器名
bash
[root@localhost ~]# docker stop cef340972b6b
cef340972b6b
强制停止容器
docker kill 容器ID或者容器名称
bash
[root@localhost ~]# docker kill 95cece2337ab
95cece2337ab
删除已停止的容器
docker rm 容器ID
bash
[root@localhost ~]# docker rm 95cece2337ab
95cece2337ab
删除正在运行的容器需要加上-f
docker rm -f XXX
启动守护式容器与进入容器
如果是前台运行
这种启动方式会霸占我们整个 linux终端,导致我们不能做其他的事情。所以在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以通过-d 指定容器的后台运行模式。
bash
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
bash
[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
查看容器内运行的进程
docker top 容器 ID
bash
[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 inspect 容器 ID
进入容器内部
docker exec -it 容器ID /bin/bash
bash
[root@localhost ~]# docker exec -it 0365c86064ff /bin/bash
root@0365c86064ff:/data#
使用exit退出后容器依然在运行
也可以通过docker attach 容器ID的方式进入容器,但是exit退出会导致容器停止
从容器内拷贝文件到主机上
docker cp 容器ID:容器内路径 目的主机路径