Centos 8部署Docker

Centos 8部署Docker

基础信息

系统 ip (NAT) 内存 硬盘
Centos 8.5 192.168.48.10 2G 40G

基本配置好可以访问互联网即可

切换阿里yum

1
2
3
4
5
6
7
mkdir repo.bak
cp /etc/yum.repos.d/* repo.bak/
rename '.repo' '.repo.bak' /etc/yum.repos.d/*.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum clean all && yum makecache
systemctl disable --now firewalld
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

安装Docker

删除旧版docker

1
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine -y

安装依赖

1
yum install -y yum-utils device-mapper-persistent-data lvm2

添加阿里docker存储库

1
2
dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum clean all && yum makecache

安装docker软件

1
yum install -y --allowerasing docker-ce docker-ce-cli

启动docker服务

1
systemctl enable docker --now

验证是否安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@Docker ~]# docker run hello-world
#未检测到hello-world镜像
Unable to find image 'hello-world:latest' locally
#从远程的DockerHub仓库拉取镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
Status: Downloaded newer image for hello-world:latest
#出现下面说明docker运行成功
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/

安装docker compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 设置 Docker 镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

# 安装 Docker Compose
sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo systemctl enable docker --now
sudo systemctl restart docker

sudo usermod -aG docker $USER

使用Docker创建新的镜像

检索Docker仓库中的Ubuntu镜像

1
2
3
4
[root@Docker ~]# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 15464 [OK]
......

获取Ubuntu镜像

1
2
3
4
5
6
7
[root@Docker ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6e3729cf69e0: Pull complete
Digest: sha256:27cb6e6ccef575a4698b66f5de06c7ecd61589132d5a91d098f7f3f9285415a9
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

查看本地Docker镜像

1
2
3
4
[root@Docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 6b7dfa7e8fdb 5 weeks ago 77.8MB
hello-world latest feb5d9fea6a5 16 months ago 13.3kB
1
2
3
4
5
6
Docker镜像列表选项字段说明如下。
REPOSITORY 镜像的名称
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像创建时间
SIZE 镜像大小

启动docker容器

1
2
3
4
5
6
7
8
使用docker run 命令可启动容器,Docker 启动时其执行过程如下。
· 检查本地是否存在指定的Docker 镜像,如果不存在将从Docker仓库下载。
· 使用Docker 镜像创建并启动Docker 容器。
· 为Docker容器分配一个文件系统,并在只读的镜像层外面挂载一层可读写层。
· 从Docker宿主机中配置的网桥接口中桥接一个虚拟接口到容器。
· 从Docker 网络的地址池中分配一个IP地址给当前容器。
· 执行用户指定的程序。
` 执行完毕后终止容器。
1
2
3
4
5
6
创建基于本地ubuntu镜像的docker容器
[root@Docker ~]# docker run -it ubuntu /bin/bash
root@136156e4c448:/#
root@136156e4c448:/# exit
exit
[root@Docker ~]#

更新并创建Docker镜像

更新ubuntu镜像添加ping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
当本地的镜像不能满足日常需求,可从已创建的容器中更新并提交镜像。最新版的ubuntu没有安装ping,本步骤将在ubuntu安装ping,并建立新的镜像
#启动 ubuntu容器,每次创建容器会产生新的id(9eac21a09449)要记录好
[root@Docker ~]# docker run -t -i ubuntu:latest /bin/bash
root@9eac21a09449:/# apt update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
......
root@9eac21a09449:/# ping jd.com
bash: ping: command not found
#安装ping
root@9eac21a09449:/# apt-get install -y iputils-ping
Reading package lists... Done
root@9eac21a09449:/# ping -c 2 jd.com
PING jd.com (111.13.149.108) 56(84) bytes of data.
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=1 ttl=127 time=43.7 ms
64 bytes from 111.13.149.108: icmp_seq=2 ttl=127 time=44.6 ms
#退出
root@9eac21a09449:/# exit
exit
#操作完成后可基于该docker容器创建新的docker镜像
#创建新的docker镜像
[root@Docker ~]# docker commit -m 'ubuntu增加ping命令' -a 'docker' 9eac21a09449 ping/ubuntu:v2
sha256:7655d7758bcfa635dde3cf8064602a32ac625100a1b7763e7856ff6052c8abee
[root@Docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ping/ubuntu v2 7655d7758bcf 10 seconds ago 120MB
ubuntu latest 6b7dfa7e8fdb 5 weeks ago 77.8MB
hello-world latest feb5d9fea6a5 16 months ago 13.3kB
#运行新创建的容器,测试ping
[root@Docker ~]# docker run -it ping/ubuntu:v2 /bin/bash
root@7015378380d7:/# ping jd.com -c 3
PING jd.com (111.13.149.108) 56(84) bytes of data.
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=1 ttl=127 time=44.1 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=2 ttl=127 time=44.9 ms
64 bytes from 111.13.149.108: icmp_seq=3 ttl=127 time=45.1 ms

使用dockerfile创建镜像

1
Dockerfile是由一系列命令和参数脚本构成的脚本,这些命令可在基础镜像上一次执行,最终创建一个新的Docker镜像

编辑dockerfile

1
2
3
4
5
6
7
8
9
10
11
[root@Docker ~]# vi /root/Dockerfile
#基础镜像信息
FROM ubuntu
#维护者信息
MAINTAINER book_user book@123.com
#镜像操作指令
RUN apt update
RUN apt-get install -y iputils-ping
#容器启动时执行的命令
CMD cd /opt
CMD echo 'new docker' > readme.txt

Dockerfile中每一个指令都会在镜像上创建一个新的镜像层,每一个指令的前缀都必须是大写的,其创建过程如下

使用docker build 命令从dockerfile文件创建镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@Docker ~]# docker build -t "book/ubuntu:v3" .
#末尾小数点别忘了,接下来,他就会按照dockerfile文件中的命令一一执行

[root@Docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
book/ubuntu v3 90bbd384b2a3 59 seconds ago 120MB
ping/ubuntu v2 7655d7758bcf 17 minutes ago 120MB
ubuntu latest 6b7dfa7e8fdb 5 weeks ago 77.8MB
hello-world latest feb5d9fea6a5 16 months ago 13.3kB

#删除本地的docker镜像
[root@Docker ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 5542ae3caafa is using its referenced image feb5d9fea6a5
出现报错说明container使用到了这个images,删除镜像前需要删除基于此镜像创建的容器

#查询容器列表
[root@Docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7015378380d7 ping/ubuntu:v2 "/bin/bash" 19 minutes ago Exited (127) 15 minutes ago nervous_lamarr
37ac7bf75dbc ping/ubuntu:v2 "/bin/bash" 19 minutes ago Exited (0) 19 minutes ago peaceful_galois
9eac21a09449 ubuntu:latest "/bin/bash" 32 minutes ago Exited (130) 26 minutes ago angry_golick
136156e4c448 ubuntu "/bin/bash" 34 minutes ago Exited (130) 33 minutes ago intelligent_proskuriakova
5542ae3caafa hello-world "/hello" 46 minutes ago Exited (0) 46 minutes ago pensive_newton
#删除容器
[root@Docker ~]# docker rm 5542ae3caafa
5542ae3caafa
删除镜像
[root@Docker ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359

特别声明
千屹博客旗下的所有文章,是通过本人课堂学习和课外自学所精心整理的知识巨著
难免会有出错的地方
如果细心的你发现了小失误,可以在下方评论区告诉我,或者私信我!
非常感谢大家的热烈支持!

Centos 8部署Docker
https://blog.qianyios.top/posts/51253/
作者
严千屹
发布于
2023年1月17日
更新于
2024年9月14日
许可协议