环境:
CentOS 8.5.2111
Docker 20.10.10
Docker-Compose 2.1.0
服务:
db redis web nginx
NET Core 6+MySQL 8+Nginx + Redis
Kubernetes 又称 K8s ,是集群的 ,单机用 Docker Compose 编排容器 比较方便
cat /etc/system-release #查看版本 sudo yum update dnf -y update #更新系统环境
先安装 Docker Engine 和 Docker Compose
1. Docker 安装
Docker三大核心概念:
镜像(Image)
容器(Container)
仓库(Repository)
https://docs.docker.com/compose/install/
https://cloud.tencent.com/document/product/213/46000
DockerHub镜像加速器
使用加速器可以提升获取Docker官方镜像的速度
Docker Version不低于1.10时,建议直接通过daemon config进行配置
配置文件/etc/docker/daemon.json(没有时新建该文件)
1. 创建一个Docker的配置文件
sudo vim /etc/docker/daemon.json
2. 编写配置文件
{
"registry-mirrors": [
"https://个人申请的.mirror.aliyuncs.com",
"https://mirror.ccs.tencentyun.com",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
阿里云的个人加速器地址:https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors
安装 Docker
1.添加 Docker 软件源 (docker-ce 社区版)
dnf config-manager --add-repo=https://mirrors.cloud.tencent.com/docker-ce/linux/centos/docker-ce.repo
2.查看已添加的 Docker 软件源
dnf list docker-ce
3.安装 Docker
dnf install -y docker-ce
4.重新加载并重启
sudo systemctl daemon-reload
sudo systemctl restart docker
5.设置Docker开机自启
sudo systemctl enable docker
启动docker
sudo systemctl start docker
关闭docker
sudo systemctl stop docker
更新docker-ce
yum update docker-ce
Docker 常用命令
docker info #查看信息
docker version #查看版本
docker images #查看已有镜像
docker ps #查看当前运行中的容器
docker ps -a #查看所有容器,包括未运行中的
docker inspect netcoreip #查看容器详细信息
docker exec -it nginx ls #进入容器查看目录结构与文件 docker rm -f $(docker ps -aq) #删除全部容器
docker rmi -f $(docker images -aq) #删除全部镜像 docker run -it ImageId /bin/bash #ImageId docker images 命令获取, 退出容器:执行 exit 命令
docker exec -it 容器ID /bin/bash #进入后台运行的容器
docker logs 容器ID/名称 #如未查看到刚才运行的容器,查看启动日志排错
强制删除镜像 例子
docker rmi -f 60c033c82171
docker rmi -f netcorezeng
docker rmi -f microsoft/dotnet
强制删除容器 例子
docker rm -f mysql8
卸载Docker
sudo yum remove docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
2.Docker-Compose 容器编排
https://cloud.tencent.com/developer/article/1438222
V1 版本安装(不用这个)
https://docs.docker.com/compose/install/
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 #设置权限 docker compose version #查看版本 sudo rm /usr/local/bin/docker-compose #删除
V2 版本安装 (当前用这个)
https://docs.docker.com/compose/cli-command/#install-on-linux
https://github.com/docker/compose
1.创建目录
mkdir -p /usr/local/lib/docker/cli-plugins
2.下载
curl -SL https://github.com/docker/compose/releases/download/v2.1.0/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins
上面方法 一般都会因为网络原因下载失败 使用下面方法
下载 docker-compose-linux-x86_64 重命名为 docker-compose 放到 /usr/local/lib/docker/cli-plugins
3.添加可执行权限
chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
3.Docker-Compose 配置
目录结构:
工程目录下 /home/docker_compose/docker-compose.yml
docker-compose.yml 配置
version: "3.9"
services:
db:
image: mysql
container_name: mysql8
restart: always
volumes:
- ./my.cnf:/etc/mysql/my.cnf
- /home/data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "root密码"
MYSQL_DATABASE: "数据库名称"
MYSQL_USER: "用户名"
MYSQL_PASSWORD: "用户密码"
ports:
- 3306:3306
command:
- --default-authentication-plugin=mysql_native_password
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_bin redis:
image: redis
container_name: redis
volumes:
- /home/data/redis:/var/lib/redis
- ./redis.conf:/usr/local/etc/redis/redis.conf
ports:
- 6379:6379
command:
redis-server /usr/local/etc/redis/redis.conf web:
image: ${DOCKER_REGISTRY-}netcorezeng
container_name: netcoreapp
build:
context: /home/app
dockerfile: Dockerfile
restart: always
ports:
- 5000:80
depends_on:
- db
- redis
environment:
TZ: Asia/Shanghai nginx:
image: nginx
container_name: nginx
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- /home/sslcrt:/etc/nginx/cert
- /home/log/nginx:/var/log/nginx
- /home/app:/usr/share/nginx/html
my.cnf 配置
[client] #socket = /usr/mysql/mysqld.sock default-character-set = utf8mb4 [mysqld] #pid-file = /var/run/mysqld/mysqld.pid
#socket = /var/run/mysqld/mysqld.sock
#datadir = /var/lib/mysql
#socket = /usr/mysql/mysqld.sock
#pid-file = /usr/mysql/mysqld.pid datadir = /home/data/mysql
character_set_server = utf8mb4
collation_server = utf8mb4_bin
default-time_zone = '+8:00'
secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/
nginx.conf 配置
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx;
worker_processes auto;
error_log error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
worker_connections 1024;
}
http {
log_format main '$remote_addr $http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log access.log main; sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server {
listen 80;
server_name xx.com www.xx.com;
location / {
root /usr/share/nginx/html;
index index.html;
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Origin $http_origin;
}
} server {
listen 443 ssl;
server_name xx.com www.xx.com;
ssl_certificate "cert/1_bundle.crt";
ssl_certificate_key "cert/2_.key";
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
root /usr/share/nginx/html;
index index.html;
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Origin $http_origin;
}
}
}
Dockerfile 配置
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443 FROM base AS final
WORKDIR /app
# 将本地应用拷贝到 容器/app 目录下
COPY ./ ./
ENTRYPOINT ["dotnet", "NETCoreZeng.dll"]
1.创建工程目录
mkdir /home/docker_compose
cd /home/docker_compose
2.创建 Dockerfile 模板文件
注意:一定要是UNIX格式,不能是windows格式
/home/app/Dockerfile
3.配置服务文件 docker-compose.yml
注意:不可以有制表符
语法错误: found character that cannot start any token (不可以有制表符)
did not find expected key
使用 YAML 时需要注意下面事项:
●不支持制表符 tab 键缩进,需要使用空格缩进
●通常开头缩进2个空格
●字符后缩进1个空格,如冒号 : 、逗号 , 、横杠 -
●用 # 号注释
●如果包含特殊字符用单引号 ‘’ 引起来
●布尔值必须用引号 “” 括起来
4.运行 Compose
错误提示:can't find a suitable configuration file in this directory or any parent: not found
切换到工程目录下
cd /home/docker_compose
再运行
docker compose up -d
错误提示:yaml: line 33: did not find expected key(格式要规范 不能多空格 如 web db redis 前面的空格)
错误提示:cannot load certificate No such file or directory(证书默认目录 /etc/nginx )
docker compose version #查看版本
docker compose info
docker compose up #创建并启动所有服务
docker compose up -d #在后台所有启动服务
docker compose images #所有镜像
docker compose ps #查看当前正在运行的容器
docker compose ps -a #查看所有容器,包括未运行中的
docker compose stop #停止所有容器
docker compose stop web #停止某个服务
docker compose rm -f web #移除已经停止的服务容器
docker compose restart #重启 docker compose logs -t --tail="10" #查看nginx最新10条日志 sudo rm /usr/local/lib/docker/cli-plugins/docker-compose #卸载 Docker Compose
容器时间与宿主机不一致
docker-compose.yml 里面添加
environment:
TZ: Asia/Shanghai
容器之间连接
SERVICE 服务分别是 :db redis web nginx
nginx.conf
proxy_pass http://web;
appsettings.json
"ConnectionStrings": {
"MySqlConnection": "server=db;xxxxxxxx;"
},
最后
看起来 很多东西配置 其实 配置好 docker_compose 里面几个服务的配置 灵魂的东西都在里面了
切换到工程目录下
cd /home/docker_compose
再运行
docker compose up -d
就可以了