使用 DigitalOcean 编写 Dokcer 卷

问题描述

我的 docker build 在我的设备上运行。当我上传到数字海洋服务器并执行“docker-compose up -d”命令时,除了“前​​端”之外的所有容器都被启动。问题出在“卷”上,当我删除它时,一切正常。 如何解决这个问题呢?我需要保留此属性

frontend_1 | > test-docker@0.1.0 dev /app/client
frontend_1 | > next dev
frontend_1 |
frontend_1 | sh: 1: next: not found
frontend_1 | npm ERR! code ELIFECYCLE
frontend_1 | npm ERR! syscall spawn
frontend_1 | npm ERR! file sh
frontend_1 | npm ERR! errno ENOENT
frontend_1 | npm ERR! test-docker@0.1.0 dev: `next dev`
frontend_1 | npm ERR! spawn ENOENT
frontend_1 | npm ERR!
frontend_1 | npm ERR! Failed at the test-docker@0.1.0 dev script.
frontend_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
frontend_1 | npm WARN Local package.json exists,but node_modules missing,did you mean to install?
frontend_1 |
frontend_1 | npm ERR! A complete log of this run can be found in:
frontend_1 | npm ERR! /root/.npm/_logs/2021-01-31T01_14_30_246Z-debug.log

docker-compose.yml:

version: "3.8"

services:
  api:
    restart: always
    ports:
    - 8081:8081
    build:
      context: ./server
      dockerfile: ./Dockerfile
    volumes:
      - ./server:/app/server
    command: npm run dev
    depends_on:
      - mongo
  frontend:
    restart: always
    ports:
      - 3000:3000
    build:
      context: ./client
      dockerfile: ./Dockerfile
    volumes:
      - ./client:/app/client
    depends_on:
      - api
  mongo:
    image: mongo
    expose:
      - 27017
    ports:
      - 27017:27017
    volumes:
      - apiDB:/data/db

volumes:
  apiDB:

version: "3.8"

services:
  api:
    restart: always
    ports:
    - 8081:8081
    build:
      context: ./server
      dockerfile: ./Dockerfile
    volumes:
      - ./server:/app/server
    command: npm run dev
    depends_on:
      - mongo
  frontend:
    restart: always
    ports:
      - 3000:3000
    build:
      context: ./client
      dockerfile: ./Dockerfile
    volumes:
      - ./client:/app/client
    depends_on:
      - api
  mongo:
    image: mongo
    expose:
      - 27017
    ports:
      - 27017:27017
    volumes:
      - apiDB:/data/db

volumes:
  apiDB:

./client/Dockerfile:

FROM node:14

RUN mkdir -p /app/client
workdir /app/client

copY . /app/client
RUN npm install

CMD [ "npm","run","dev"]

解决方法

我不鼓励您使用映像的构建上下文(例如 build: context: ./client)与容器的卷安装(例如 volumes: ./client:${SOMETHING})相同,这可能是问题所在。

本地主机上的构建上下文用于构建映像,您可以将其COPY 放到映像的 /app/client 目录中。

当您运行图像时,您尝试将(之前复制到图像的)目录 (/app/client) 替换为 Droplet 上现在为空的目录。

如果您的 Dockerfile 是正确的(并且您的本地主机的 ./client 被复制),您可以放弃 volumefrontend 挂载(因为它已经在 {{1 }}).