假如我们要部署一个简单的应用,该应用包含 demo_ui(前端安装包)、demo_server(后端安装包)以及数据库 MySQL 服务,如下图:
下面将使用 docker compose 文件快速部署上述服务。
假如我们的项目结构如下图所示:
然后,创建了一个 demo_db 数据库,及 t_user 数据表(用户表),如下:
为数据库 db、demo_server、demo_ui 各自构建 dockerfile 文件,用于构建镜像。
编写一个 dockerfile,用于构建数据镜像,如下:
FROM mysql:5.7 # 将本地的初始化脚本复制到容器内的指定目录 COPY init.sql /docker-entrypoint-initdb.d/ # 可以设置环境变量,例如设置MySQL的root用户密码 ENV MYSQL_ROOT_PASSWORD=aaaaaa
前端项目结构如下图:
创建一个 Dockerfile 文件,用来构建前端项目镜像,Dockerfile 文件内容如下:
FROM ubuntu:latest RUN apt-get update && apt-get install -y nginx curl npm COPY . /app WORKDIR /app RUN npm install --registry=https://registry.npm.taobao.org RUN npm run build RUN rm -rf node_modules RUN cat <<EOF > /etc/nginx/conf.d/demo_app.conf server { listen 8080; server_name localhost; location / { root /app/dist; index index.html index.htm; } } EOF EXPOSE 8080 CMD ["nginx", "-g", "daemon off;"]
后端项目结构如下图:
创建一个 Dockerfile 文件,用来构建后端项目镜像,Dockerfile 文件内容如下:
FROM openjdk:17-alpine WORKDIR /app COPY target/demo_server-0.0.1-SNAPSHOT.jar . ENV ENVIROMENT=dev EXPOSE 8080 ENTRYPOINT ["java", "-jar", "demo_server-0.0.1-SNAPSHOT.jar", "--spring.profiles.active=${ENVIROMENT}"]
为了满足上述需求,我们编写 docker-compose.yml 文件,内容如下:
version: '3' services: mysql: build: context: ./db dockerfile: Dockerfile ports: - "3306:3306" volumes: - /var/demo_data/mysql_data:/var/lib/mysql networks: - mynetwork demo_server: build: context: ./demo_server dockerfile: Dockerfile ports: - "8080:8080" depends_on: - mysql volumes: - /var/demo_data/demo_server_logs:/app/logs networks: - mynetwork demo_ui: build: context: ./demo_ui dockerfile: Dockerfile depends_on: - demo_server ports: - "80:8080" networks: - mynetwork networks: mynetwork: driver: bridge
此时,使用 docker-compose up -d 根据 docker-compose.yml 创建镜像和启动容器,如下:
root@ubuntu22:~/demo_simple# docker-compose up -d Creating network "demo_simple_mynetwork" with driver "bridge" Building mysql 💢 [+] Building 1.9s (7/7) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 264B 0.0s => [internal] load metadata for docker.io/library/mysql:5.7 1.6s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build context 0.0s => => transferring context: 30B 0.0s => [1/2] FROM docker.io/library/mysql:5.7@sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb 0.0s => CACHED [2/2] COPY init.sql /docker-entrypoint-initdb.d/ 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:f0baabbe28598c2e7734a4dec35fc656cfcb2ec77665ea5541f549e8b7f14fba 0.0s => => naming to docker.io/library/demo_simple_mysql 0.0s 1 warning found (use docker --debug to expand): - SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "MYSQL_ROOT_PASSWORD") (line 7) WARNING: Image for service mysql was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Building demo_server 💢 [+] Building 1.6s (8/8) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 265B 0.0s => [internal] load metadata for docker.io/library/openjdk:17-alpine 1.3s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build context 0.0s => => transferring context: 87B 0.0s => [1/3] FROM docker.io/library/openjdk:17-alpine@sha256:4b6abae565492dbe9e7a894137c966a7485154238902f2f25e9dbd9784383d81 0.0s => CACHED [2/3] WORKDIR /app 0.0s => CACHED [3/3] COPY target/demo_server-0.0.1-SNAPSHOT.jar . 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:a56fbb0d5b5e9b47e35374c04f7f3845fa3bcdd22f4775cbfff5522743a2ca84 0.0s => => naming to docker.io/library/demo_simple_demo_server 0.0s WARNING: Image for service demo_server was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Building demo_ui 💢 [+] Building 1.9s (13/13) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 509B 0.0s => [internal] load metadata for docker.io/library/ubuntu:latest 1.6s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/8] FROM docker.io/library/ubuntu:latest@sha256:8a37d68f4f73ebf3d4efafbcf66379bf3728902a8038616808f04e34a9ab63ee 0.0s => [internal] load build context 0.0s => => transferring context: 615B 0.0s => CACHED [2/8] RUN apt-get update && apt-get install -y nginx curl npm 0.0s => CACHED [3/8] COPY . /app 0.0s => CACHED [4/8] WORKDIR /app 0.0s => CACHED [5/8] RUN npm install --registry=https://registry.npm.taobao.org 0.0s => CACHED [6/8] RUN npm run build 0.0s => CACHED [7/8] RUN rm -rf node_modules 0.0s => CACHED [8/8] RUN cat <<EOF > /etc/nginx/conf.d/demo_app.conf 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:4a7ba73991b216f4f89ce95fb0bd88b245af59ecbf59494f0651fa5e66f0aaec 0.0s => => naming to docker.io/library/demo_simple_demo_ui 0.0s WARNING: Image for service demo_ui was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Creating demo_simple_mysql_1 ... done 🌈 Creating demo_simple_demo_server_1 ... done 🌈 Creating demo_simple_demo_ui_1 ... done 🌈
查看创建的镜像:
root@ubuntu22:~/demo_simple# docker images REPOSITORY TAG IMAGE ID CREATED SIZE demo_simple_demo_ui latest 4a7ba73991b2 23 hours ago 1.14GB 👈 demo_simple_demo_server latest a56fbb0d5b5e 24 hours ago 347MB 👈 demo_simple_mysql latest f0baabbe2859 24 hours ago 501MB 👈 nginx latest 5ef79149e0ec 4 weeks ago 188MB redis latest dae83f665c92 6 weeks ago 117MB mongo latest 2076a62a1ced 2 months ago 782MB
查看启动的容器:
root@ubuntu22:~/demo_simple# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------------------ demo_simple_demo_server_1 java -jar demo_server-0.0. ... Up 0.0.0.0:8080->8080/tcp,:::8080->8080/tcp demo_simple_demo_ui_1 nginx -g daemon off; Up 0.0.0.0:80->8080/tcp,:::80->8080/tcp demo_simple_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp
从上面输出可以看到,demo_server 被映射到宿主机的 8080 端口,如下图:
demo_ui 被映射到宿主机的 80 端口,如下图:
点击下载上面涉及的项目文件: