Docker安装及Docker C/S 分离部署
·
安装docker
环境选择
容器需要管理工具、runtime 和操作系统,我们的选择如下:
- 管理工具 - Docker Engine,Docker 最流行使用最广泛。
- runtime - runc,Docker 的默认 runtime
- 操作系统 - CentOS Stream8
安装操作系统
基于 CentOS-Stream-8模板制作.pdf这个实验手册做出来的模板克隆一个虚拟机命名为docker,并配置 静态IP 192.168.108.30,主机名改为docker
[root@localhost ~]# hostnamectl set-hostname docker
[root@localhost ~]# nmcli connection modify ens160 ipv4.method manual
ipv4.addresses 192.168.108.30/24 ipv4.gateway 192.168.108.2 ipv4.dns
192.168.108.2 autoconnect yes
[root@localhost ~]# nmcli con up ens160
安装 Docker Docker
支持几乎所有的 Linux 发行版,也支持 Mac 和 Windows。各操作系统的安装方法可以访问: htt ps://docs.docker.com/engine/installation/
卸载旧版本(可选)
[root@docker ~]# yum remove docker-ce
安装必要工具
# devicemapper 存储驱动已经在 docker 18.09 版本中被废弃,所以在后续的安Docker装中无需安装
devicemapper支持。
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
vim
[root@localhost ~]# yum-config-manager --add-repo
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker
ce.repo
[root@localhost ~]# yum makecache
Docker CE Stable - x86_64
57 kB/s | 66 kB
CentOS Stream 8 - BaseOS
8.3 kB/s | 3.9 kB
00:01
00:00
CentOS Stream 8 - AppStream
9.8 kB/s | 4.4 kB
Metadata cache created.
allinone部署
安装软件
[root@docker ~]# yum install -y docker-ce
配置服务
[root@docker ~]# systemctl enable docker.service --now
验证安装
查看docker版本
[root@docker ~]# docker --version
Docker version 26.1.3, build b72abbb
验证docker状态
[root@docker ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor
preset: disabled)
Active: active (running) since Wed 2025-09-03 13:51:07 CST; 36s ago
Docs: https://docs.docker.com
Main PID: 1813 (dockerd)
Tasks: 10
Memory: 135.7M
CGroup: /system.slice/docker.service
└─1813 /usr/bin/dockerd -H fd:// --
containerd=/run/containerd/containerd.sock
配置镜像加速器(华为云)
[root@docker ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": [
"https://054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com" ]
}
# 按“Esc”,输入:wq保存并退出。
#重启容器引擎
[root@docker ~]# systemctl restart docker
#确认配置结果
[root@docker ~]# docker info
Client: Docker Engine - Community
Version: 26.1.3
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.14.0
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.27.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 26.1.3
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 8b3b7ca2e5ce38e8f31a34f35b2b68ceb8470d89
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 4.18.0-553.6.1.el8.x86_64
Operating System: CentOS Stream 8
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.486GiB
Name: docker
ID: 96160847-9a97-40f4-997c-d12efaaef738
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
`https://054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com/
看这
Live Restore Enabled: false
运行第一个容器
环境就绪,马上运行第一个容器,执行命令:
[root@docker ~]# docker run hello-world
其过程可以简单的描述为:
- 从本地查找hello-wrold镜像,没找到
- 从 Docker Hub 下载hello-world镜像。
- 启动hello-world容器。
清空刚才的实验环境:
[root@docker ~]# docker rm -f $(docker ps -aq)
#删除所有容器
[root@docker ~]# docker rmi -f hello-world
此刻docker环境没有问题了,关机拍摄快照
运行第二个容器
[root@docker ~]# docker run -d -p 80:80 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
852e50cd189d: Pull complete
67d51c33d390: Pull complete
b0ad2a3b9567: Pull complete
136f1f71f30c: Pull complete
01f8ace29294: Pull complete
Digest: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456
Status: Downloaded newer image for httpd:latest
817f24cca3514568f9b7a7cac6f183734077fb74caf4c62502acd2a4d0d29520
其过程可以简单的描述为:
- 从 Docker Hub 下载 httpd 镜像。镜像中已经安装好了 Apache HTTP Server。
- 启动 httpd 容器,并将容器的 80 端口映射到 host 的 80 端口。
下面我们可以通过浏览器验证容器是否正常工作。在浏览器中输入 http://[your host os IP]
[root@docker ~]# curl 192.168.108.30
<html><body><h1>It works!</h1></body></html>
docker C/S分离部署
基于 CentOS-Stream-8模板制作.pdf这个实验手册做出来的模板克隆虚拟机命名为docker_client和 docker_server
docker_server端
配置ip 192.168.108.30 配置hostname docker_server
[root@localhost ~]# hostnamectl set-hostname docker_server
[root@localhost ~]# nmcli connection modify ens160 ipv4.method manual
ipv4.addresses 192.168.108.30/24 ipv4.gateway 192.168.108.2 ipv4.dns
192.168.108.2 autoconnect yes
[root@localhost ~]# nmcli con up ens160
安装软件
[root@docker_server ~]# yum install -y yum-utils device-mapper-persistent-data
lvm2 vim
[root@localhost ~]# yum-config-manager --add-repo
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker
ce.repo
[root@docker_server ~]# yum makecache
[root@docker_server ~]# yum install -y docker-ce
[root@docker_server ~]# systemctl enable docker.service --now
[root@docker_server ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": [
"https://054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com" ]
}
# 按“Esc”,输入:wq保存并退出。
#重启容器引擎
[root@docker_server ~]# systemctl restart docker
配置服务
[root@docker_server ~]# vim /usr/lib/systemd/system/docker.service
# 在ExecStart参数中最后添加 -H tcp://0.0.0.0:2375,docker默认监听2375
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
[root@docker_server ~]# systemctl daemon-reload
[root@docker_server ~]# systemctl restart docker.service
[root@docker_server ~]# systemctl stop firewalld
验证
[root@docker_server ~]# yum install lsof
[root@docker_server ~]# lsof -i :2375
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dockerd 5440 root
3u IPv6 44991 0t0 TCP *:docker (LISTEN)
docker_client端
配置ip 192.168.108.31
[root@localhost ~]# hostnamectl set-hostname docker_client
[root@localhost ~]# nmcli connection modify ens160 ipv4.method manual
ipv4.addresses 192.168.108.31/24 ipv4.gateway 192.168.108.2 ipv4.dns
192.168.108.2 autoconnect yes
[root@localhost ~]# nmcli con up ens160
只安装docker客户端
[root@docker-client ~]# yum install -y yum-utils device-mapper-persistent-data
lvm2 vim
[root@docker-client ~]# yum-config-manager --add-repo
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker
ce.repo
[root@docker-client ~]# yum makecache
[root@docker-client ~]# yum install -y docker-ce-cli
验证
[root@docker_client ~]# docker run hello-world
务端
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is
the docker daemon running?.
See 'docker run --help'.
# client端连接server端执行命令
[root@docker-client ~]# docker -H 192.168.108.30 run hello-world
#client直接执行报错,没有装服
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-client ~]# docker -H 192.168.108.30 images
REPOSITORY
TAG
hello-world
latest
IMAGE ID
d2c94e258dcb
CREATED
SIZE
17 months ago 13.3kB
#这里也可以切换到Server端查看现象
说明:client只做管理,image和container存储在server端。
更多推荐
所有评论(0)