1. 安装docker
安装docker详细教程
https://developer.aliyun.com/article/110806
# 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# 开启Docker服务
sudo service docker start
配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://lz5xr9iw.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info
网络查看
# 查看所有容器的ip
docker network inspect 网络名称
docker network inspect mynet
# 查看指定容器的ip
docker inspect 容器id或者容器名称
docker inspect mysql-5.7
默认网络模式 - bridge
无网络模式 - none
宿主网络模式 - host
多容器共享网络模式 - container
设置容器IP案例
# 创建网络
docker network create --driver bridge --subnet=172.18.0.0/16 --gateway=172.18.0.1 mynet
# MySQL
docker pull mysql:5.7.36;
docker run \
-dp 3306:3306 \
--restart=always \
--privileged=true \
--name mysql-5.7 \
--network=mynet \
--ip 172.18.0.10 \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ="Asia/Shanghai" \
-v /opt/docker-mysql/conf:/etc/mysql/conf.d \
-v /opt/docker-mysql/data:/var/lib/mysql \
mysql:5.7.36;
# Redis
docker pull redis:6.2.6;
docker run -d \
-dp 6379:6379 \
--restart=always \
--privileged=true \
--name redis-6.2 \
--network=mynet \
--ip 172.18.0.11 \
-e TZ="Asia/Shanghai" \
-v /opt/docker-redis/data:/data \
-v /opt/docker-redis/conf/redis.conf:/etc/redis/redis.conf \
redis:6.2.6 \
--requirepass 123456;
# Server
docker run -d \
-p 8080:8080 \
--restart=always \
--privileged=true \
--name ruoyi-admin-1.0 \
--network=mynet \
--ip 172.18.0.12 \
-e TZ="Asia/Shanghai" \
-e RY_REDIS_DB="0" \
-e RY_REDIS_HOST="172.18.0.11" \
-e RY_REDIS_PORT="6379" \
-e RY_REDIS_PASSWORD="123456 " \
-e RY_MYSQL_DB_NAME="ry-vue" \
-e RY_MYSQL_DB_PORT="3306" \
-e RY_MYSQL_HOST="172.18.0.10" \
-e RY_MYSQL_USER="root" \
-e RY_MYSQL_PASSWORD="123456" \
-v /home/ruoyi/uploadPath:/ruoyi/uploadPath \
ruoyi-admin:1.0;
2. 安装dockerUI
docker pull portainer/portainer:1.24.2
docker run \
-dp 9000:9000 \
--restart=always \
--privileged=true \
--name dockerUI \
-v /var/run/docker.sock:/var/run/docker.sock \
portainer/portainer:1.24.2;
3. 安装MySQL
docker pull mysql:5.7.36
docker run \
-dp 3306:3306 \
--restart=always \
--privileged=true \
--name mysql-5.7 \
-e MYSQL_ROOT_PASSWORD=123456 \
-v /opt/docker-mysql/conf:/etc/mysql/conf.d \
-v /opt/docker-mysql/data:/var/lib/mysql \
mysql:5.7.36;
4. 安装Redis
docker pull redis:6.2.6
docker run -d \
-dp 6379:6379 \
--restart=always \
--privileged=true \
--name redis-6.2 \
-v /opt/docker-redis/data:/data \
-v /opt/docker-redis/conf/redis.conf:/etc/redis/redis.conf \
redis:6.2.6;
5. 部署后台服务
Dockerfile
# 给容器添加字体
FROM openjdk:8-jre-alpine
ENV LANG en_US.UTF-8
RUN set -xe && apk --no-cache add ttf-dejavu fontconfig
VOLUME /tmp
ADD ruoyi-admin.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
制作镜像
语法:docker build -t 镜像名称[:版本号] .
例子:docker build -t ruoyi-admin:1.0 .
启动容器
# 仅主机模式(network=host)
docker run -d \
--restart=always \
--privileged=true \
--name ruoyi-admin-2.0 \
--network=host \
-v /home/ruoyi/uploadPath:/home/ruoyi/uploadPath \
ruoyi-admin:2.0;
# 桥接模式(network=bridge)
docker run -d \
-p 8088:8080 \
--restart=always \
--privileged=true \
--name ruoyi-admin-2.0 \
--network=mynet \
--ip 172.18.0.12 \
-v /home/ruoyi/uploadPath:/home/ruoyi/uploadPath \
ruoyi-admin:2.0;
# none
6. 安装Nginx
宿主机安装
nginx默认安装位置:/etc/nginx/conf/default.conf
# 升级依赖
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安装主程序
yum -y install nginx
# 查看版本
nginx -version
# 启动服务
systemctl start nginx
systemctl enable nginx
文件位置:/etc/nginx/conf.d/default.conf
修改完成后,必须重新加载nginx服务:systemctl reload nginx
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /prod-api/ {
proxy_pass http://172.18.0.12:8080/;
}
}
7. 镜像的导入导出
导出
语法:docker save -o 导出镜像名称.tar 镜像名称:版本
例子:docker save -o ruoyi-admin_centos7_v2.tar ruoyi-admin:2.0
导入
语法:docker load -i 导出镜像名称.tar
例子:docker load -i ruoyi-admin_centos7_v2.tar
评论区