文章目录
前言
在开始Docker的实操前,先熟悉下Docker的三要素:image镜像、container容器、repository仓库。仓库是存放镜像的地方,通过镜像可以run出一个又一个容器,举个Java例子来理解下镜像和容器的关系,镜像就好比Java的实体类,通过Java的new语法,可以通过实体类new出多个对象实例,而这个对象实例就好比Docker的容器。
一、安装Docker
通过 Docker的官网 提供了Docker的安装方法 ,大家可以阅读Docker官网上提供的方法,然后根据该教程一步一步安装。
1.安装gcc相关
yum -y install gcc
yum -y install gcc-c++
2.安装docker需要的repository
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
注意:
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
命令与官网提供的命令有区别。因为Docker来自国外,使用国外的repository下载会超级慢甚至连接超时,此处我们使用阿里云提供的镜像。
3.更新yum软件包索引
# centos7使用 yum makecache fast 命令
yum makecache fast
# centos8使用 yum makecache 命令
yum makecache
4.安装Docker引擎
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
5.启动Docker
systemctl start docker
6.查看Docker版本
docker version
7.运行hello world镜像
docker run hello-world
如果查看Docker版本,运行hello-world镜像都可以打印正常信息,证明Docker安装并启动成功。
8.停用并卸载Docker(了解即可)
systemctl stop docker
yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
二、阿里云镜像加速
1.镜像加速器
使用支付宝账户登陆阿里云官网,找到 容器镜像服务 ,获取镜像加速器的地址。
2.配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
注意:根据阿里云容器镜像服务提供的linux命令来配置Docker的加速地址时。路径要使用你自己的,而不是文章中的!
三、Docker常用命令
1.帮助启动类命令
- 启动docker:
systemctl start docker
- 停止docker:
systemctl stop docker
- 重启docker:
systemctl restart docker
- 查看docker状态:
systemctl status docker
- 开启启动:
systemctl enable docker
- 查看docker概要信息:
docker info
- 查看docker总体帮助文档:
docker --help
- 查看docker命令帮助文档:
docker 具体命令 --help
这类命令比较简单,运行docker后,使用一下就全明白了。
2.镜像命令
- 列出本地主机上的镜像:
docker images
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 9 months ago 72.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
各个选项说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签版本号
IAMGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
- 查询某个镜像:
docker search 某个镜像名字
[root@hecs-340056 /]# docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 11202 [OK]
bitnami/redis Bitnami Redis Docker Image 227 [OK]
bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 39 [OK]
bitnami/redis-cluster 34
rapidfort/redis RapidFort optimized, hardened image for Redi… 15
rapidfort/redis-cluster RapidFort optimized, hardened image for Redi… 15
circleci/redis CircleCI images for Redis 14 [OK]
ubuntu/redis Redis, an open source key-value store. Long-… 10
redis/redis-stack redis-stack installs a Redis server with add… 10
bitnami/redis-exporter 9
redis/redis-stack-server redis-stack-server installs a Redis server w… 8
clearlinux/redis Redis key-value data structure server with t… 4
ibmcom/redis-ha 1
bitnami/redis-sentinel-exporter 1
ibmcom/ibm-cloud-databases-redis-operator Container image for the IBM Operator for Red… 1
vmware/redis-photon 0
rapidfort/redis6-ib RapidFort optimized, hardened image for Redi… 0
greenbone/redis-server A redis service container image for the Gree… 0
cimg/redis 0
rancher/redislabs-ssl 0
drud/redis redis 0 [OK]
corpusops/redis https://github.com/corpusops/docker-images/ 0
blackflysolutions/redis Redis container for Drupal and CiviCRM 0
ibmcom/redisearch-ppc64le 0
ibmcom/redis-ppc64le 0
[root@hecs-340056 /]#
各个选项说明:
NAME:镜像名称
DESCRIPTION:镜像说明
STARS:点赞数量
OFFICIAL:是否是官方的
AUTOMATED:是否是自动构建的
- 下载镜像:
docker pull 某个镜像名字
[root@hecs-340056 /]# 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
[root@hecs-340056 /]#
如果输入docker pull redis
命令,默认下载最新版本redis镜像;如果想要指定redis版本,则 docker pull redis:6.0.8
。
- 查看镜像/容器/数据卷所占的空间:
docker system df
[root@hecs-340056 /]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 3 2 185.5MB 112.7MB (60%)
Containers 6 1 8B 8B (100%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
[root@hecs-340056 /]#
- 删除镜像:
docker rmi 某个镜像名字或ID
[root@hecs-340056 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 7 months ago 113MB
ubuntu latest ba6acccedd29 9 months ago 72.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
[root@hecs-340056 /]# docker rmi redis
Untagged: redis:latest
Untagged: redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Deleted: sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631
Deleted: sha256:49c70179bc923a7d48583d58e2b6c21bde1787edf42ed1f8de9e9b96e2e88e65
[root@hecs-340056 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 9 months ago 72.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
[root@hecs-340056 /]#
本文使用redis的名称来删除,还可以使用redis的ID删除。
3.容器命令
- 新建+启动容器:
docker run [OPTIONS] 镜像 [COMMAND] [ARG...]
OPTIONS说明: 注意:有些是一个减号,有些是两个减号
–name=容器新名字 为容器指定一个名称(–name是两个减号)
-d 后台运行容器并返回容器ID,即启动守护式容器,后台运行
-i 以交互模式运行容器,通常与 -t 同时使用
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用,即启动交互式容器
-P 随机端口映射
-p 指定端口映射
观察下列示例,执行docker run -it ubuntu /bin/bash
命令,以前台交互式启动乌班图实例,在容器内执行/bin/bash命令。要退出乌班图终端,直接输入exit
或 ctrl+p+q
即可。
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 9 months ago 72.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
[root@hecs-340056 ~]# docker run -it ubuntu /bin/bash
root@118a8c76a228:/# ls //此时已经进入到了乌班图终端
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@118a8c76a228:/#
- 列出当前所有正在运行的容器:
docker ps [OPTIONS]
OPTIONS说明:
-a 列出当前所有正在运行的和历史上运行过的容器
-l 显示最近创建的容器
-n 显示最近n个创建的容器
-q 静默模式,只显示容器编号
[root@hecs-340056 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
118a8c76a228 ubuntu "/bin/bash" 22 seconds ago Up 21 seconds funny_gagarin
[root@hecs-340056 /]#
- 退出容器:
exit
或者ctrl+p+q
两种退出方式的区别:
exit:run启动交互式容器,exit退出,容器停止
ctrl+p+q:run启动交互式容器,ctrl+p+q退出,容器不停止 - 启动已停止运行的容器:
docker start 容器ID或者容器名
- 重启容器:
docker restart 容器ID或者容器名
- 停止容器:
docker stop 容器ID或者容器名
- 强制停止容器:
docker kill 容器ID或者容器名
- 删除已停止的容器:
docker rm 容器ID或者容器名
- 启动守护式容器:
docker run -d 镜像名
本文以后台守护式启动redis容器,启动成功会返回容器ID。这个命令是日常工作中最常使用的。
[root@hecs-340056 ~]# docker run -d redis:6.0.8
2bdb979a4fc7b65cce1cfed3cfbe924bca675b698ac508812af99b290a4fd726
[root@hecs-340056 ~]#
- 启动交互式容器:
docker run -it 镜像名
本文以前台交互式启动redis容器,当使用exit或者ctrl+c会停止并退出容器;使用ctrl+p+q退出但不停止容器。
[root@hecs-340056 ~]# docker run -it redis:6.0.8
1:C 09 Aug 2022 03:18:34.749 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 09 Aug 2022 03:18:34.749 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 09 Aug 2022 03:18:34.749 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 09 Aug 2022 03:18:34.750 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 09 Aug 2022 03:18:34.750 # Server initialized
1:M 09 Aug 2022 03:18:34.750 # 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 09 Aug 2022 03:18:34.750 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 09 Aug 2022 03:18:34.751 * Ready to accept connections
- 进入正在运行的容器并与命令行交互:
docker exec -it 容器ID /bin/bash
[root@hecs-340056 ~]# docker exec -it 2bdb979a4fc7 /bin/bash
root@2bdb979a4fc7:/data# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
- 查看容器日志:
docker logs 容器ID
- 查看容器内运行的进程:
docker top 容器ID
- 查看容器内部细节:
docker inspect 容器ID
- 从容器内拷贝文件到主机上:
docker cp 容器ID:容器内文件路径 目的主机路径
- 导出容器:
docker export 容器ID > 文件名.tar
export导出容器的内容留作为一个tar归档文件,相当于备份一个容器
[root@hecs-340056 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2bdb979a4fc7 redis:6.0.8 "docker-entrypoint.s…" 3 hours ago Up 3 hours 6379/tcp adoring_galois
[root@hecs-340056 /]# ls
bin home root '启动命令 .txt'
[root@hecs-340056 /]# docker export 2bdb979a4fc7 > abcd.tar
[root@hecs-340056 /]# ls
abcd.tar bin home root '启动命令 .txt'
[root@hecs-340056 /]#
- 导入容器:
cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
import从tar包中的内容创建一个新的文件系统再导入为镜像,相当于恢复之前备份的容器
[root@hecs-340056 /]# cat abcd.tar | docker import - suibian/redis:6.0.8
sha256:80190400e705843ce0816e474ac16e4c1ca3581e9f2d4c53788ecf7606cea849
[root@hecs-340056 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
suibian/redis 6.0.8 80190400e705 16 seconds ago 101MB
redis latest 7614ae9453d1 7 months ago 113MB
ubuntu latest ba6acccedd29 9 months ago 72.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
redis 6.0.8 16ecd2772934 21 months ago 104MB
四、镜像
1.提交容器副本使之成为一个新的镜像
docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:标签名
描述:假如在docker中下载并启动了centos镜像或者rabbitmq镜像,新启动的centos或者rabbitmq容器是最简洁版的,缺少很多插件,比如centos缺少vim命令、或者rabbitmq缺少第三方插件。如果我们给centos容器安装了vim命令、或者给rabbitmq安装了某些插件后,想把安装插件后的容器生成一个镜像时,就需要使用该命令。
[root@hecs-340056 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7c44182cfb5 centos "/bin/bash" 7 minutes ago Up 7 minutes pedantic_chaum
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
[root@hecs-340056 ~]# docker commit -m '随便写点什么' -a="小Y" a7c44182cfb5 mycentos:1.0.0
sha256:9db67ed69c43af6017333644b25647016747ef25aef4e1480dd5b7347ace8cca
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0.0 9db67ed69c43 5 seconds ago 233MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@hecs-340056 ~]#
2.本地镜像发布到阿里云
在上一节中,我们使用commit命令生成了新的镜像,本小节讲解把新生成的镜像发布到阿里云,变成一个开源的镜像,供其他开发人员使用。比如上一节,我们在contos安装了vim命令并生成了新的mycentos镜像,本次我们就以该镜像为案例推送到阿里云。
首先在 阿里云容器镜像服务 创建命名空间。
然后点击“镜像仓库”,创建一个全新的仓库。在打开的弹窗中输入仓库名称,“代码源”选择“本地仓库”。
创建成功后,可以查看docker操作阿里云仓库相关的命令。包含拉取和推送镜像等命令。注意:下列命令连接的是作者的仓库,大家需要使用自己的仓库。
接下来,我们把mycentos镜像推送到阿里云仓库。根据阿里云提供的命令可以直接推送阿里云仓库。
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0.0 9db67ed69c43 5 days ago 233MB
centos latest 5d0da3dc9764 11 months ago 231MB
[root@hecs-340056 ~]# docker login --username=ys199568 registry.cn-beijing.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@hecs-340056 ~]# docker tag 9db67ed69c43 registry.cn-beijing.aliyuncs.com/tirami/my_contos:1.0.0
[root@hecs-340056 ~]#
[root@hecs-340056 ~]# docker push registry.cn-beijing.aliyuncs.com/tirami/my_contos:1.0.0
The push refers to repository [registry.cn-beijing.aliyuncs.com/tirami/my_contos]
c00d96ef2383: Pushed
74ddd0ec08fa: Pushed
1.0.0: digest: sha256:dc64618fbb348a84fe14ad5406d0cfda7284312fcd67bb062ac871833a27d965 size: 739
[root@hecs-340056 ~]#
拉取的命令大家自行验证,这里就不演示了。
3.Docker私有库
Docker Registry是官方提供的工具,可以用于构建私有镜像仓库。
- 下载镜像Docker Registry
[root@hecs-340056 ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete
3790aef225b9: Pull complete
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
- 运行私有库Registry,相当于本地有个私有Docker hub
[root@hecs-340056 ~]# docker run -d -p 5000:5000 -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
9c4dc5c8c3ada8d0c30fe2e9845e80a6d6e119f116afe900d7185ba34177b679
[root@hecs-340056 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c4dc5c8c3ad registry "/entrypoint.sh /etc…" 4 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp elegant_johnson
[root@hecs-340056 ~]#
–privileged=true :开启权限
-v 设置挂载路径,即容器数据卷,把宿主机路径文件和容器路径文件绑定起来,实现数据备份。容器数据卷的用处并不只是备份数据,比如在启动redis容器时,通过容器数据卷可以指定宿主机的redis.conf文件来运行redis容器。
- curl验证私服库上有什么镜像
[root@hecs-340056 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c4dc5c8c3ad registry "/entrypoint.sh /etc…" 27 minutes ago Up 27 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp elegant_johnson
[root@hecs-340056 ~]# curl -XGET http://192.168.0.63:5000/v2/_catalog
{"repositories":[]}
[root@hecs-340056 ~]#
- 将新镜像mycentos:1.2修改符合私服规范的Tag
修改名称为 ip:端口/名称:版本号
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.2 f7c683ea9933 4 seconds ago 231MB
registry latest b8604a3fe854 9 months ago 26.2MB
centos latest 5d0da3dc9764 11 months ago 231MB
[root@hecs-340056 ~]# docker tag mycentos:1.2 192.168.0.63:5000/mycentos:1.2
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.0.63:5000/mycentos 1.2 f7c683ea9933 About a minute ago 231MB
mycentos 1.2 f7c683ea9933 About a minute ago 231MB
registry latest b8604a3fe854 9 months ago 26.2MB
centos latest 5d0da3dc9764 11 months ago
- 修改配置文件使之支持http
[root@hecs-340056 ~]# vim /etc/docker/daemon.json
修改daemon.json,在底部添加 “insecure-registries”:[“192.168.0.63:5000”],修改之后的文件如下所示
{
"registry-mirrors": ["https://055qrtfe.mirror.aliyuncs.com"],
"insecure-registries":["192.168.0.63:5000"]
}
- push推送到私服库
docker push ip:端口/名称:版本号
[root@hecs-340056 ~]# docker push 192.168.0.63:5000/mycentos:1.2
The push refers to repository [192.168.0.63:5000/mycentos]
74ddd0ec08fa: Pushed
1.2: digest: sha256:2d5b8509c6f0d304d2d00ccb95f3322f0fc330861e24cf264664fd94939bb210 size: 529
[root@hecs-340056 ~]#
- curl再次验证私服库上有什么镜像
[root@hecs-340056 ~]# curl -XGET http://192.168.0.63:5000/v2/_catalog
{"repositories":["mycentos"]}
- pull拉取私服库的镜像
为了方便查看效果,大家可以通过docker rmi命令删除docker上的centos镜像,方便观察。
docker pull ip:端口/名称:版本号
[root@hecs-340056 ~]# docker pull 192.168.0.63:5000/mycentos:1.2
1.2: Pulling from mycentos
a1d0c7532777: Already exists
Digest: sha256:2d5b8509c6f0d304d2d00ccb95f3322f0fc330861e24cf264664fd94939bb210
Status: Downloaded newer image for 192.168.0.63:5000/mycentos:1.2
192.168.0.63:5000/mycentos:1.2
[root@hecs-340056 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.0.63:5000/mycentos 1.2 f7c683ea9933 8 minutes ago 231MB
registry latest b8604a3fe854 9 months ago 26.2MB
五、Docker安装MySQL、Redis等注意事项
到此为止,大家已经学会了Docker的常用命令,但我们学习Docker的最终目的是以容器的方式运行tomcat、mysql、redis或者nginx之类的软件。此处不一一演示了,大家可以自行百度,毕竟这篇文章是以介绍docker基础命令为主,网上有不少docker安装redis的文章,写的很详尽,大家在学完这篇文章的基础上可以参考他们的文章安装。但是需要注意,运行容器时一定要设置容器数据卷,防止容器被删除丢失数据。
总结
目前,我们已经总结完了Docker入门级的操作教程。关于Docker进阶级教程,大家可以关注我的下一篇文章 Docker进阶教程。