【问题标题】:How to build my client correctly when using docker compose?使用 docker compose 时如何正确构建我的客户端?
【发布时间】:2020-08-25 02:29:18
【问题描述】:

阅读一周左右后,我一直在尝试使用 docker compose。

我有一个 docker compose 文件:

version: "3.8"

x-common-variables: &common-variables
  MYSQL_USER: root
  MYSQL_PASSWORD: MyPassWord
  MYSQL_DATABASE: wedding
  MYSQL_HOST_IP: 127.0.0.1
  REACT_APP_SERVER_PORT: 4000

services:
  mysql:
    image: mysql:latest
    environment:
      <<: *common-variables
      MYSQL_HOST: localhost
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3307:3307
    restart: unless-stopped
    volumes:
      - /wedding_Script2.sql:/docker-entrypoint-initdb.d/wedding_Script2.sql
  phpmyadmin:
    depends_on:
      - mysql
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: mysql
    links:
      - mysql:mysql
    ports:
      - 8080:80
    restart: always
  server:
    build: ./weddingApp-Be
    depends_on:
      - mysql
    expose:
      - 4000
    environment:
      <<: *common-variables
      MYSQL_HOST_IP: mysql
      NODE_PATH: src
    ports:
      - 4000:4000
    volumes:
      - ./weddingApp-Be/src:/server
    links:
      - mysql
    command: npm run dev
  client:
    build: ./weddingApp-Fe
    image: nginx:alpine
    environment:
      <<: *common-variables
      NODE_PATH: src
    expose:
      - 3000
    ports:
      - 3000:3000
    volumes:
      - ./weddingApp-Fe/src:/index
    links:
      - server
    command: nginx, -g, daemon off

我的项目结构:

weddingApp-Fe 我的 dockerFile 内部:

FROM node:12.2.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/package.json
RUN npm install --silent
RUN npm install react-scripts@3.0.1 -g --silent
COPY . /app
RUN npm run build

# production environment
FROM nginx:1.16.0-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]

当我做 docker compose build 时:

mysql uses an image, skipping
phpmyadmin uses an image, skipping
Building server
Step 1/8 : FROM node:10-alpine
 ---> 8e473595b853
Step 2/8 : RUN mkdir -p /app
 ---> Using cache
 ---> 19b56a37f612
Step 3/8 : WORKDIR /app
 ---> Using cache
 ---> 8dce08d04982
Step 4/8 : COPY package.json /app
 ---> Using cache
 ---> e5efea9d7cd2
Step 5/8 : COPY yarn.lock /app
 ---> Using cache
 ---> 94ed16d1b60f
Step 6/8 : COPY . /app
 ---> Using cache
 ---> 57942181ca20
Step 7/8 : RUN npm install
 ---> Using cache
 ---> cc6cf02a698f
Step 8/8 : CMD ["npm", "run dev"]
 ---> Using cache
 ---> 043145db7a94

Successfully built 043145db7a94
Successfully tagged weddingapp_server:latest
Building client
Step 1/12 : FROM node:12.2.0-alpine as build
 ---> f391dabf9dce
Step 2/12 : WORKDIR /app
 ---> Using cache
 ---> 33e6357adf78
Step 3/12 : ENV PATH /app/node_modules/.bin:$PATH
 ---> Using cache
 ---> 90bef54edec6
Step 4/12 : COPY package.json /app/package.json
 ---> Using cache
 ---> 92f7b7643519
Step 5/12 : RUN npm install --silent
 ---> Using cache
 ---> 912d5018ac08
Step 6/12 : RUN npm install react-scripts@3.0.1 -g --silent
 ---> Using cache
 ---> bec736fe9b8f
Step 7/12 : COPY . /app
 ---> Using cache
 ---> 29232568be7d
Step 8/12 : RUN npm run build
 ---> Using cache
 ---> 59219cc1e335

Step 9/12 : FROM nginx:1.16.0-alpine
 ---> ef04b00b089d
Step 10/12 : COPY --from=build /app/build /usr/share/nginx/html
 ---> Using cache
 ---> a5be9b459a1b
Step 11/12 : EXPOSE 3000
 ---> Using cache
 ---> 684c6b70aed5
Step 12/12 : CMD ["nginx", "-g", "daemon off;"]
 ---> Using cache
 ---> c38b886354d6

Successfully built c38b886354d6
Successfully tagged nginx:alpine

问题:

在这之后我会docker compose up

当我收到以下错误时:

Removing weddingapp_client_1
weddingapp_mysql_1 is up-to-date
weddingapp_phpmyadmin_1 is up-to-date
weddingapp_server_1 is up-to-date
Recreating 5b9004354493_weddingapp_client_1 ... error

ERROR: for 5b9004354493_weddingapp_client_1  Cannot start service client: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"nginx,\": executable file not found in $PATH": unknown

ERROR: for client  Cannot start service client: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"nginx,\": executable file not found in $PATH": unknown
Encountered errors while bringing up the project.

我认为这是我的 nginx 的问题,但我不知道是什么。

编辑:

如果我换成npm start

在 docker-compose 的底部,然后我得到

错误:对于weddingapp_client_1 无法启动服务客户端:OCI 运行时创建失败:container_linux.go:349:启动容器 进程导致“exec:”npm“:$PATH 中找不到可执行文件”: 未知

错误:客户端无法启动服务客户端:OCI 运行时创建 失败:container_linux.go:349:启动容器进程导致 "exec: "npm": $PATH 中找不到可执行文件": 未知

【问题讨论】:

  • 您确定在客户端应用中需要image: nginx:alpine 吗?我之所以问,是因为您已经使用 build 指令告诉 docker compose 容器基于哪个图像。
  • 我完全不确定。这是我为了让它工作而尝试的东西。没有帮助
  • 删除它并重建所有图像,我认为它可能会有所帮助。
  • 它没有帮助。但是,如果我删除底部“命令:nginx,-g,守护进程关闭”中的命令,那么它可以工作,但它永远不会启动客户端。
  • 啊,对不起,我从第一个答案看不懂。如果这是 client 所依赖的,您可以添加 depends_on: server 吗?

标签: reactjs docker docker-compose


【解决方案1】:

你告诉 Compose 用一些没有意义的东西覆盖命令:

command: nginx, -g, daemon off

这与在命令行中运行该命令的效果相同:逗号被解释为命令的一部分。

你实际上并不需要这个选项,因为 Dockerfile 有

CMD ["nginx", "-g", "daemon off;"]

这就是你想要它做的事情。

您可以类似地删除expose:links:(在当今的Docker 网络中没有用)和volumes:(因为代码已经在您的映像中)。如果你是 build: 你自己的镜像,你不希望 image: 命名一个公共 Docker Hub 镜像(你本地构建的镜像会覆盖那个镜像,你会得到令人困惑的结果),并且数据库地址可能不是127.0.0.1(“这个容器”)。您可能可以成功地将docker-compose.yml 文件的那部分修剪为

client:
  build: ./weddingApp-Fe
  environment:
    MYSQL_USER: root
    MYSQL_PASSWORD: MyPassWord
    MYSQL_DATABASE: wedding
    MYSQL_HOST_IP: mysql
  ports:
    - 3000:3000

(如果你真的想覆盖 Dockerfile 中的命令,这里需要 YAML 列表语法 command: [nginx, -g, 'daemon off;'],但使用 Dockerfile 中的默认命令更好。)

【讨论】:

  • 在此之后我创建了两个撰写文件。一个用于开发,一个用于生产。我想我把这搞混了。你解决了一个我还没有弄清楚如何正确做的数据库问题,也许你可以提供一些建议。在我的服务器应用程序中,我连接到本地 mysql 数据库,然后我机器上的主机 ip 是 127.0.0.1 但是在 dev/prod 中构建它时,如何确保我得到正确的主机?非常感谢!
【解决方案2】:

您的客户端应用程序的 Dockerfile 将 node:12.2.0-alpine 指定为不包含 nginx 的基本映像。您需要在此映像中安装 NGINX 才能使其正常工作。即使您在 docker-compose 文件中将 nginx:alpine 作为指定图像,您也已将 docker-compose 构建上下文指向 ./weddingApp-Fe。这意味着 docker-compose 将在该目录中搜索 Dockerfile 并使用它来构建您的映像。未安装 NGINX 也是当您从撰写文件中删除 command: nginx, -g, daemon off 行时它不会崩溃的原因。

【讨论】:

    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多