参考文档:https://docs.docker.com/engine/install/centos/#prerequisites
在CentOS上安装 Docker Engine并部署springboot项目运行环境
一. 安装docker
1.1. 系统要求
要安装 Docker 引擎,您需要 CentOS 7、CentOS 8 (stream) 或 CentOS 9 (stream) 的維護版本。不支持或测试存档版本。
必须启用存储库。默认情况下,此存储库处于启用状态,但如果已禁用它,则需要重新启用它。centos-extras
建议使用存储驱动程序。overlay2
1.2. 卸载老的版本
sudo yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-engine
1.3. 使用存储库安装docker
1.3.1) 设置存储库
sudo yum install -y yum-utils
sudo yum-config-manager
--add-repo
https://download.docker.com/linux/centos/docker-ce.repo
1.3.2) 安装 最新版Docker 引擎
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
1.3.3) 启动Docker
sudo systemctl start docker
设置成开机自启动
systemctl enable docker
1.3.4) 通过运行映像来验证 Docker 引擎是否已正确安装。hello-world
二. 配置阿里云镜像加速
用对应账号登录阿里云网站,找到-》镜像工具=》镜像加速器
https://cr.console.aliyun.com/?spm=a2c4g.11186623.0.0.19e6a42dmXkt6R
加速器地址:https://对应的.mirror.aliyuncs.com
2.1). 安装/升级Docker客户端
推荐安装1.10.0以上版本的Docker客户端,参考文档docker-ce
2.2). 配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://对应的.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.3). 验证是否配置成功
docker info
检查Registry mirrors: 是否已经是阿里云加速器地址
三. 安装项目依赖环境
1. JDK
[root@localhost ~]# java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
备注:springboot项目中,mysql,redis, host写成IP 宿主机的IP
Dockerfile
FROM java:8
VOLUME /tmp
ADD admin.jar jrt.jar
EXPOSE 20004
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/jrt.jar"]
mkdir /docker-v/jrtdocker/
cd /docker-v/jrtdocker
admin.jar
Dockfile
docker build -t myjrt .
docker run -d --restart=always -p 20004:20004 myjrt
docker logs -f 76b
2. Redis
[root@localhost ~]# redis-cli
127.0.0.1:6379> info
Server
redis_version:6.2.6
Docker安装Redis:
docker pull redis:6.2.6
docker run --name myredis -d -p 6379:6379 --restart=always -v /docker-v/myredis/conf:/usr/local/etc/redis -v /docker-v/myredis/data:/data redis:6.0.2 redis-server /usr/local/etc/redis/redis.conf
vim /docker-v/myredis/conf/redis.conf
# requirepass t123456
3. Nginx
[root@localhost ~]# nginx -V
nginx version: nginx/1.16.1
docker pull nginx:1.16.1
docker run -p 80:8080 -d --restart=always --name mynginx
-v /docker-v/mynginx/log:/var/log/nginx
-v /docker-v/mynginx/conf:/etc/nginx/conf.d
-v /home/cloud_platform/dist_1/:/home/cloud_platform/dist_1/
-v /home/Medical/resource/uploadPath/:/home/Medical/resource/uploadPath/
nginx:1.16
vim /docker-v/mynginx/conf/test.conf
server {
listen 8080;
server_name localhost;
client_max_body_size 52M;
root /home/cloud_platform/dist_1;
location / {
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# location /download {# 文件下载
# add_header Content-Type "application/octet-stream";# 避免浏览器打开文件
# alias /tmp/test/upload;
# }
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.157:20004/;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
vim /docker-v/mynginx/conf/online.conf
4. Mysql
docker pull mysql:5.7
docker run --name mymysql -p 3306:3306 --restart=always -v /docker-v/mysql/custom/my.cnf:/etc/mysql/my.cnf
-e MYSQL_ROOT_PASSWORD='UIIO988IINan'
-v /docker-v/mysql/data:/var/lib/mysql
-d mysql:5.7
vim my.cnf
[mysqld]
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
docker-compose 编排
docker-compose.yml
version: "3.0"
services:
db:
image: mysql:5.7
container_name: mysql
ports:
- "3306:3306"
volumes:
- /docker-v/mysql/data:/var/lib/mysql
- /docker-v/mysql/custom/my.cnf:/etc/mysql/my.cnf
- /docker-v/mysql/log:/var/log/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: "UIIO988IINan"
redis:
image: redis:6.2.6
container_name: redis
ports:
- "6379:6379"
restart: always
volumes:
- /docker-v/myredis/conf:/usr/local/etc/redis
- /docker-v/myredis/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
web:
build: ./
restart: always
depends_on:
- db
- redis
ports:
- "20004:20004"
nginx:
image: nginx:1.16.1
container_name: nginx
ports:
- "443:443"
- "80:8080"
restart: always
volumes:
- /docker-v/mynginx/log:/var/log/nginx
- /docker-v/mynginx/conf:/etc/nginx/conf.d
- /home/cloud_platform/dist_1/:/home/cloud_platform/dist_1/
启动所有服务:
docker-compose up -d
注意: mysql数据库密码中含有特殊字符$时 要进行转移 写成$$。
目录结构
[root@localhost docker-v]# tree ./
./
├── jrtdocker
│ ├── docker-compose.yml
│ ├── Dockerfile
│ └── admin.jar
├── mynginx
│ ├── conf
│ │ └── test.conf
│ ├── html
│ └── log
│ ├── access.log
│ └── error.log
├── myredis
│ ├── conf
│ │ └── redis.conf
│ └── data
│ └── dump.rdb
└── mysql
├── custom
│ └── my.cnf -> /etc/alternatives/my.cnf
├── data
├── log
还有点问题
nginx.conf /4xx.html /5xx.html需要映射
mysql 服务创建一个新用户