跳转到内容

Docker部署

Docker 是一个使用容器构建、部署和运行应用程序的工具。

Docker 镜像和容器可以部署到许多不同的云平台(如阿里云、腾讯云、华为云)或者本地服务器环境中, 本节示例不聚焦于如何部署到特定平台, 而是会展示如何将让项目通过Docker服务运行。

  • 安装 Docker。你可以在此处找到适用于你的操作系统的安装指南
  • 安装 docker-compose。你可以在此处找到适用于你的操作系统的安装指南
  • 确保文件 Dockerfile 存在。你可以在此处了解有关 Dockerfile 的更多信息。 Racer在项目中已经自动为Web端、H5端、Spring后端生成了对应的Dockerfile。
  • 文件夹example-ec-ant-tuniao
    • 文件夹db-mysql
      • 文件夹conf
        • my.cnf
      • Dockerfile
    • 文件夹spring-example
      • Dockerfile
    • 文件夹uniapp-example
      • 文件夹deploy
        • 文件夹nginx
          • ruru-ui.conf
        • Dockerfile
    • 文件夹vue3-example
      • 文件夹deploy
        • 文件夹nginx
          • ruru-ui.conf
        • Dockerfile
    • docker-compose.yml

mysql数据库服务Dockerfile内容

段落标题 mysql数据库服务Dockerfile内容
Dockerfile
FROM mysql:5.7
ENV TZ=Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY ./init/*.sql /docker-entrypoint-initdb.d/

后端Spring Dockerfile内容示例

段落标题 后端Spring Dockerfile内容示例

以下是例子项目Dockerfile内容,它将使用 openjdk8 构建你的后端服务。

Dockerfile
FROM adoptopenjdk/openjdk8-openj9:alpine-slim
ADD target/service-example-1.0.1.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

前端Web Dockerfile内容示例

段落标题 前端Web Dockerfile内容示例
Dockerfile
FROM nginx
COPY ./dist /data
RUN rm /etc/nginx/conf.d/default.conf
ADD nginx/ruru-ui.conf /etc/nginx/conf.d/
RUN /bin/bash -c 'echo init ok'

为了构建上面的 Dockerfile,同级目录下nginx下面 ruru-ui.conf 的文件。

ruru-ui.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
# 打包好的dist目录文件,放置到这个目录下
root /data/;
location /ruru-examplem/ {
proxy_pass http://service-example:8888/;
#proxy_set_header Host $http_host;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /index.html;
}

docker-compose.yml 内容示例

段落标题 docker-compose.yml 内容示例
docker-compose.yml
version: '3.3'
networks:
ruru_network:
driver: bridge
name: ruru_network
services:
db-mysql:
container_name: db-mysql
build: ./db-mysql
environment:
MYSQL_ROOT_PASSWORD: "root"
image: "db-mysql:5.7"
restart: always
volumes:
- ./db-mysql/conf/my.cnf:/etc/my.cnf
- ./db-mysql/data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- ruru_network
service-example:
container_name: service-example
image: service-example:v1
build: ./spring-example
restart: always
environment:
- TZ=Asia/Shanghai
volumes:
- ./logs/:/logs/
deploy:
resources:
limits:
cpus: '2.00'
memory: 612M
reservations:
memory: 200M
networks:
- ruru_network
example-web:
container_name: example-web
build: ./vue3-example
image: example-web:v1
restart: always
volumes:
- ./vue3-example/deploy/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./vue3-example/deploy/dist:/usr/share/nginx/html
- ./vue3-example/deploy/nginx/cert:/etc/nginx/cert
- ./vue3-example/deploy/nginx/log:/var/log/nginx
ports:
- 80:80
- 8443:443
networks:
- ruru_network
example-h5:
container_name: example-h5
build: ./uniapp-example
image: example-h5:v1
restart: always
volumes:
- ./uniapp-example/deploy/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./uniapp-example/deploy/dist:/usr/share/nginx/html
- ./uniapp-example/deploy/nginx/cert:/etc/nginx/cert
- ./uniapp-example/deploy/nginx/log:/var/log/nginx
ports:
- 8181:80
- 1443:443
networks:
- ruru_network
  1. 在项目的根目录中运行以下命令来构建docker Image

    终端窗口
    docker-compose build
  2. 启动服务。

    终端窗口
    docker-compose up -d

    接着你应该能够通过 http://your-server-ip 访问你的系统了。

  3. 查看服务运行

    终端窗口
    docker-compose ps