文章目录
CentOS Docker 安装
基于 centos 7.5
cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
卸载旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装 Docker Engine-Community
设置仓库
sudo yum install -y yum-utils
使用以下命令来设置稳定的仓库
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安装 Docker Engine-Community
sudo yum install docker-ce docker-ce-cli containerd.io
提示您接受 GPG 密钥,请选是,等待的时间很短,如果错过了,在输入一次命令,下载的内容有缓存,不会再次下载
要安装特定版本的 Docker Engine-Community
请在存储库中列出可用版本,然后选择并安装:
1、列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。
yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
2、通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1。
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
启动 Docker
sudo systemctl start docker
通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。
sudo docker run hello-world
返回如下提示表示安装成功
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/
在systemctl start docker
的时候可能会报错,docker.servier unit is not found 原因是因为安装的时候,下载速度慢于是我就走开了,在提醒输入密钥的时候超时了,没有安装成功,但是没有具体的提示信息,此时重新执行一次 yum install 就好了。
配置Docker 镜像加速
请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
之后重新启动服务:
sudo systemctl daemon-reload
sudo systemctl restart docker
安装Registry
到 https://hub.docker.com/_/registry?tab=tags 上面搜索 Registry,
我这里选择了2.7版本
shell docker pull registry:2.7
启动 Registry,后台运行,分配端口,指定文件挂载目录,设置自动重启
docker run -d -p 5000:5000 -v /docker/registry/data:/var/lib/registry --restart=always --name registry registry:2.7
执行后用 docker ps
查看执行状态是否启动成功
curl 检查服务,返回 {} 表示运行成功
curl localhost:5000/v2/
{}
本机打标签推送
创建一个镜像
# docker tag hello-world localhost:5000/caoke/hello-world:v1
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry 2.7 708bc6af7e5e 3 months ago 25.8MB
hello-world latest bf756fb1ae65 3 months ago 13.3kB
localhost:5000/caoke/hello-world v1 bf756fb1ae65 3 months ago 13.3kB
这样我们就创建好了一个本地的镜像,现在我们把它推送到 Registry
# docker push localhost:5000/caoke/hello-world
The push refers to repository [localhost:5000/caoke/hello-world]
9c27e219663c: Mounted from hello-world
v1: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525
通过api我们就可以查看我们镜像是否推送成功
curl localhost:5000/v2/caoke/hello-world/tags/list
{"name":"caoke/hello-world","tags":["v1"]}
这里返回了我们这个镜像的标签列表
其它机器推送
默认情况下,Registry启用https服务,此时在其它机器上推送镜像就会失败,如下提示:
docker push ip:5000/koo/hello-world
The push refers to repository [ip:5000/koo/hello-world]
Get https://ip:5000/v2/: http: server gave HTTP response to HTTPS client
用证书是一种方式,另一种简单方式是忽略验证,在/etc/docker/daemon.json中配置"insecure-registries":["10.155.20.26:5000"]
完整如下:
{
"registry-mirrors":["https://hub-mirror.c.163.com/"],
"insecure-registries":["10.155.20.26:5000"]
}
记得配置完 daemon.json 后重启docker服务。systemctl restart docker
配置docker 服务开机启动
开机启动
sudo systemctl enable docker
## 或者
chkconfig docker on
禁止启动
sudo systemctl disable docker
用 chkconfig docker 检查返回 enable
或者 systemctl 检测
systemctl list-unit-files docker.service
UNIT FILE STATE
docker.service enabled
1 unit files listed.