文章目录
1. 安装Docker
1.1 linux安装Docker
演示环境为CentOS-7-Minimal
内容持续完善中
-
安装基础依赖
gcc
gcc-c++
[root@docker src]# yum -y install gcc gcc-c++
-
如果之前安装过Docker,请先卸载旧版本
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
-
安装需要的软件包
[root@docker src]# yum install 1 -y yum-utils
-
设置镜像仓库
[root@docker src]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-
更新yum软件包索引
[root@docker src]# yum makecache fast
-
安装Docker CE
[root@docker src]# yum install docker-ce docker-ce-cli containerd.io
-
启动Docker
[root@docker src]# systemctl start docker
-
测试
# 查看docker版本信息 [root@docker src]# docker version Client: Docker Engine - Community Version: 24.0.5 API version: 1.43 Go version: go1.20.6 Git commit: ced0996 Built: Fri Jul 21 20:39:02 2023 OS/Arch: linux/amd64 Context: default Server: Docker Engine - Community Engine: Version: 24.0.5 API version: 1.43 (minimum version 1.12) Go version: go1.20.6 Git commit: a61e2b4 Built: Fri Jul 21 20:38:05 2023 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.22 GitCommit: 8165feabfdfe38c65b599c4993d227328c231fca runc: Version: 1.1.8 GitCommit: v1.1.8-0-g82f18fe docker-init: Version: 0.19.0 GitCommit: de40ad0 # 测试hello-world [root@docker src]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 719385e32844: Pull complete Digest: sha256:926fac19d22aa2d60f1a276b66a20eb765fbeea2db5dbdaafeb456ad8ce81598 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ # 查看本地镜像 [root@docker src]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 9c7a54a9a43c 3 months ago 13.3kB
-
卸载
systemctl stop docker yum -y remove docker-ce docker-ce-cli containerd.io rm -rf /var/lib/docker
2. Docker常用命令
2.1 帮助命令
命令 | 含义 |
---|---|
docker version | 显示 Docker 版本信息 |
docker info | 显示 Docker 系统信息,包括镜像和容器数 |
docker --help | 帮助 |
2.2 镜像命令
2.2.1 docker images(列出本地镜像)
[root@docker src]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
说明:
标签 | 含义 |
---|---|
REPOSITORY | 镜像的仓库源 |
TAG | 镜像的标签 |
IMAGE ID | 镜像的ID |
CREATED | 镜像创建时间 |
SIZE | 镜像大小 |
同一个仓库源可以有多个 TAG
,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG
定义不同的镜像,如果你不定义镜像的标签版本,docker将默认TAG
为lastest
。
常用的可选项:
选项 | 含义 |
---|---|
-a | 列出本地所有镜像 |
-q | 只显示镜像id |
--digests | 显示镜像的摘要信息 |
2.2.2 docker search (搜索镜像)
[root@docker src]# docker search hello-world
NAM DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello Wo… 2077 [OK]
说明:
标签 | 含义 |
---|---|
NAM | 镜像的名称 |
DESCRIPTION | 镜像的描述 |
STARS | 镜像的收藏数 |
OFFICIAL | 官方来源标识 |
AUTOMATED | 自动化构建标识 |
常用的可选项:
选项 | 含义 |
---|---|
--filter=stars=50 | 过滤收藏数小于50的镜像 |
2.2.3 docker pull :(拉取镜像)
[root@docker src]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
49bb46380f8c: Pull complete
aab3066bbf8f: Pull complete
d6eef8c26cf9: Pull complete
0e908b1dcba2: Pull complete
480c3912a2fd: Pull complete
ef90fc42d4db: Pull complete
a2f7c585c753: Pull complete
e2ef842ff3d6: Pull complete
c6c990e874d7: Pull complete
a554d403eafe: Pull complete
Digest: sha256:6a5dbd2819e36048669639811461f27fee48da1e22039e5d31f4273a20d542f6
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
不写TAG
默认拉取latest
版本的镜像。
指定版本下载:
[root@docker src]# docker pull mysql:5.7
2.2.4 删除镜像
命令 | 含义 |
---|---|
docker rmi <IMAGE_ID> | 删除指定的镜像 |
docker rmi $(docker images -q) | 删除所有的本地镜像 |
2.3 容器命令
2.3.1 docker run 创建容器并启动
常用的可选项:
选项 | 含义 |
---|---|
--name="NAME" or --name NAME | 指定镜像名称 |
-d | 后台方式运行,会返回容器的id |
-i | 以交互模式运行容器,通常和 -t 一起使用 |
-t | 给容器重新分配一个终端,通常和 -i 一起使用 |
-P | 随机端口映射 |
-p | 指定端口映射(小结),一般有以下四种写法: ip:hostPort:containerPort ip::containerPort hostPort:containerPort (常用) containerPort |
测试
-
使用centos:7镜像以交互模式启动容器,在容器内执行/bin/bash命令。
[root@docker src]# docker run -it centos:7 /bin/bash Unable to find image 'centos:7' locally 7: Pulling from library/centos 2d473b07cdd5: Already exists Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 Status: Downloaded newer image for centos:7 # 注意地址,已经切换到容器内部了! [root@c1763265c97a /]# ls anaconda-post.log dev home lib64 mnt proc run srv tmp var bin etc lib media opt root sbin sys usr # 使用 exit 退出容器 [root@c1763265c97a /]# exit exit [root@docker src]#
docker run
的时候,如果本地不存在run
的镜像,会自动从远程仓库拉取镜像;不写TAG
默认运行latest
版本的镜像。交互模式运行容器,在容器中直接执行
exit
推出容器的同事容器也会停止,如果只想退出容器,让容器保持运行的话可以使用ctrl + P + Q
。 -
使用centos:7镜像以后台运行的方式启动容器
[root@docker src]# docker run -d centos:7 efe640995b8924a9062a9bdd42268801e2b30e257a5a7d90e68d26524270e0dd [root@docker src]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
这里在运行
centos:7
之后,使用docker ps
查看容器运行状态,发现容器已经退出了。这是因为,Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命令,就会自动退出。比如,你运行了nginx服务,但是docker前台没有运行应用,这种情况下,容器启动后,会立即自杀,因为他觉得没有程序了,所以最好的情况是,将你的应用使用前台进程的方式运行启动。
2.3.2 docker ps 列出所有运行的容器
[root@docker src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
说明:
标签 | 含义 |
---|---|
CONTAINER ID | 容器的的ID |
IMAGE | 容器使用的镜像ID |
COMMAND | 容器的主要信息 |
CREATED | 容器创建时间 |
STATUS | 容器的状态 |
PORTS | 容器的端口映射 |
NAMES | 容器的名称 |
常用的可选项:
选项 | 含义 |
---|---|
-a | 列出当前所有正在运行的容器 + 历史运行过的容器 |
-l | 显示最近创建的容器 |
-n=5 | 显示最近创建的5个容器 |
-q | 静默模式,只显示容器编号 |
2.3.3 容器的启动与停止
命令 | 含义 |
---|---|
docker start <CONTAINER_ID or CONTAINER_NAME> | 启动容器 |
docker restart <CONTAINER_ID or CONTAINER_NAME> | 重启容器 |
docker stop <CONTAINER_ID or CONTAINER_NAME> | 停止容器 |
docker kill <CONTAINER_ID or CONTAINER_NAME> | 强制停止容器 |
docker stop $(docker ps -aq) | 停止全部正在运行的容器 |
2.3.4 删除容器
命令 | 含义 |
---|---|
docker rm <CONTAINER_ID> | 删除指定容器 |
docker rm -f $(docker ps -a -q) | 删除所有容器 |
docker ps -a -q xargs docker rm | 删除所有容器 |