成功构建后在docker容器中找不到yarn

问题描述 投票:0回答:1

我导航到 root 并运行

docker build frontend
,它会在 /frontend 目录中找到一个 Dockerfile。 然后我使用
docker exec frontend-1 sh
进入 docker 容器,一旦进入容器
npm
node
yarn
,都显示
not found
。 看起来容器确实构建成功,甚至在构建过程中运行了一个yarn命令,所以我不知道为什么我不能从容器内部手动运行
yarn build

真正的问题我试图解决的是,每次我对React应用程序进行更改时,我都必须重建容器,而我当然只想刷新热重载。

Dockerfile

# Stage 1: Build the React app
FROM node:16-alpine as build
WORKDIR /app
  
COPY package.json ./
COPY yarn.lock ./
RUN yarn install
COPY . ./
RUN yarn build
    
# Stage 2: Serve the React app using Nginx
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

compose.yaml 文件

    services:
      web:
        # the application's web service (container) will use an image based on our Dockerfile
        build: .
        # map the internal port 80 to port 8000 on the host
        ports:
          - "8000:80"
        # map the host directory to app (which allows us to see and edit files inside the container)
        volumes:
          - ".:/app:rw"
          - "./data:/data:rw"
        # the default command to run whenever the container is launched
        command: python manage.py runserver 0.0.0.0:80
        # the URL 'postgres' or 'mysql' will point to the application's db service
        networks:
          - djangocmsnet
    
        env_file: .env-local
    
      database_default:
        # Select one of the following db configurations for the database
        image: postgres:15-alpine
        ports:
          - "${DB_PORT:-5432}:5432/tcp"  # allow your local dev env to connect to the db if variable set
        environment:
          POSTGRES_DB: "db"
          POSTGRES_PASSWORD: "password"
          POSTGRES_HOST_AUTH_METHOD: "trust"
          SERVICE_MANAGER: "fsm-postgres"
    
        networks:
          - djangocmsnet
    
        volumes:
           - postgres-data:/var/lib/postgresql/data/
    
      frontend:
        # Build the React app from the Dockerfile in the frontend directory
        build: ./frontend
        ports:
          - "3000:80"  # Map React's internal port to 3000 on the host
        volumes:
          - ./frontend:/app
        networks:
          - djangocmsnet
        environment:
          - WDS_SOCKET_HOST=127.0.0.1 
          - CHOKIDAR_USEPOLLING=true
          - WATCHPACK_POLLING=true 
    
    networks:
      djangocmsnet:
    
    volumes:
      postgres-data:

构建输出

[+] Building 71.9s (18/18) FINISHED                                                                                                                                                            
 => [internal] load build definition from Dockerfile                                                                                                                                      0.0s
 => => transferring dockerfile: 37B                                                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/nginx:alpine                                                                                                                           1.1s
 => [internal] load metadata for docker.io/library/node:16-alpine                                                                                                                         1.1s
 => [auth] library/node:pull token for registry-1.docker.io                                                                                                                               0.0s
 => [auth] library/nginx:pull token for registry-1.docker.io                                                                                                                              0.0s
 => CACHED [stage-1 1/2] FROM docker.io/library/nginx:alpine@sha256:2140dad235c130ac861018a4e13a6bc8aea3a35f3a40e20c1b060d51a7efd250                                                      0.0s
 => [build 1/8] FROM docker.io/library/node:16-alpine@sha256:a1f9d027912b58a7c75be7716c97cfbc6d3099f3a97ed84aa490be9dee20e787                                                             0.0s
 => [internal] load build context                                                                                                                                                         2.5s
 => => transferring context: 3.80MB                                                                                                                                                       2.4s
 => CACHED [build 2/8] WORKDIR /app                                                                                                                                                       0.0s
 => [build 3/8] COPY package.json ./                                                                                                                                                      0.0s
 => [build 4/8] COPY yarn.lock ./                                                                                                                                                         0.0s
 => [build 5/8] RUN yarn install                                                                                                                                                         50.6s
 => [build 6/8] COPY . ./                                                                                                                                                                 6.0s
 => [build 7/8] RUN which yarn                                                                                                                                                            0.3s 
 => [build 8/8] RUN yarn build                                                                                                                                                           10.4s 
 => [stage-1 2/2] COPY --from=build /app/build /usr/share/nginx/html                                                                                                                      0.0s 
 => exporting to image                                                                                                                                                                    0.0s 
 => => exporting layers                                                                                                                                                                   0.0s 
 => => writing image sha256:339d21ce951e1ab26ff40136083f8eedae3ead3000a893dd60f36291bb659af9
reactjs node.js docker npm yarnpkg
1个回答
0
投票
  1. 首先,为什么
    npm
    node
    yarn
    都显示在容器中找不到 当你执行
    docker exec frontend-1 sh
    时。因为你的容器 你基于多阶段Docker构建。
    # Stage 1
    您使用了
    FROM node:16-alpine as build
    。这包括(已安装)nodejs 环境,这样你就可以运行
    npm
    ,
    node
    。但第二阶段
    # Stage 2
    。您使用了
    FROM nginx:alpine
    ,这意味着它只包括 (已安装)
    nginx
    。所以你不能在这里使用
    node
  2. 为您解决刷新热重载等实际问题。你应该 使用您中的
    mount
    目录
    docker-compose
    并且 need 使用
    development stage
  • 将更多

    development-stage
    作为
    dev
    添加到 Dockerfile:

    来自节点:16-alpine 作为开发者
    工作目录/应用程序
    复制 package.json 纱线.lock ./
    运行纱线安装
    复制 。 .
    暴露3000
    CMD [“纱线”,“开始”]

-将前端服务中的 docker-compose 更改为:

build:
    context: ./frontend
    target: dev  # Use the dev stage
ports:
      - "3000:80" # port for prod
      - "3000:3000" # port for dev
© www.soinside.com 2019 - 2024. All rights reserved.