无法使用Docker上下文将本地目录复制到带有Docker的远程容器

发布于 2025-02-12 02:39:50 字数 1085 浏览 0 评论 0原文

我一直在使用本地计算机,没有任何问题。

我一直在使用dokerfile下面的docker-compose.ylm dokerfile

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install 
COPY . .
EXPOSE 5000
CMD [ "npm", "start" ]

撰写文件

...
app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app 
    restart: always 
    image: nodeapp
    ports:
      - 5000:5000
    volumes: 
      - ./:/app
      - /app/node_modules
    networks:
      - pnet 
...

时, 切换到远程主机

Docker上下文创建-docker“ host = ssh:// root@hostname”远程Docker上下文使用远程 并运行docker构成我最终遇到了错误

npm err!代码enoent API | npm err! Syscall开放 API | npm err!路径 /app/package.json paylign_api | npm err! errno -2 API | npm err! enoent enoent:没有这样的文件或目录,打开'/app/package.json' API | npm err! INOENT这与NPM无法找到文件有关。 API | npm err!恩典 API | API | npm err!可以在以下方式中找到此运行的完整日志 API | npm err! /Root/.npm/_logs/2022-07-01T14_33_33_58_429Z-debug.log

我如何修复此此操作以部署到远程主机

I have been working on my local machine with no issue using the Dokerfile below and docker-compose.ylm

Dokerfile

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install 
COPY . .
EXPOSE 5000
CMD [ "npm", "start" ]

compose file

...
app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app 
    restart: always 
    image: nodeapp
    ports:
      - 5000:5000
    volumes: 
      - ./:/app
      - /app/node_modules
    networks:
      - pnet 
...

When I use docker context to switch to remote host

docker context create --docker "host=ssh://root@hostname" remote
and
docker context use remote
and run docker compose up I end up with the error

npm ERR! code ENOENT
api | npm ERR! syscall open
api | npm ERR! path /app/package.json
paylign_api | npm ERR! errno -2
api | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
api | npm ERR! enoent This is related to npm not being able to find a file.
api | npm ERR! enoent
api |
api | npm ERR! A complete log of this run can be found in:
api | npm ERR! /root/.npm/_logs/2022-07-01T14_33_58_429Z-debug.log

How can I fix this so as to deploy to remote host

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2025-02-19 02:39:50

卷:始终由运行容器的Docker守护程序解释。如果您使用上下文指向远程Docker守护程序,则卷:命名卷和文件路径由该远程守护程序解释,并指向远程主机上的文件。

Docker Build将将本地构建上下文发送到远程Docker守护程序,而卷:不能用于在主机之间复制文件。

此处的直接解决方案是从组合设置中删除卷:

version: '3.8'
services:
  app:
    build: .
    restart: always 
    ports:
      - 5000:5000
    # volumes: will cause problems
    # none of the other options should be necessary
    # (delete every networks: block in the file)

然后,当您Docker-Compose build build图像时,构建器机制将您的本地文件树发送到远程Docker守护程序。当您运行Docker-Compose时,远程Docker将使用图像中内置的代码,而不是尝试用远程系统上的内容覆盖它。

同样的原则适用于不一定涉及第二主机的其他情况。例如,如果您使用主机的Docker守护程序从另一个容器内部启动一个容器,则音量安装在主机而不是容器文件系统中。通常,我建议不要尝试用卷覆​​盖您的应用程序代码: sounts;运行实际上内置在图像中的代码,并使用本地的非偶数工具进行实时开发。

The volumes: are always interpreted by the Docker daemon running the container. If you're using contexts to point at a remote Docker daemon, the volumes: named volumes and file paths are interpreted by that remote daemon, and point at files on the remote host.

While docker build will send the local build context to a remote Docker daemon, volumes: cannot be used to copy files between hosts.

The straightforward solution here is to delete the volumes: from your Compose setup:

version: '3.8'
services:
  app:
    build: .
    restart: always 
    ports:
      - 5000:5000
    # volumes: will cause problems
    # none of the other options should be necessary
    # (delete every networks: block in the file)

Then when you docker-compose build the image, the builder mechanism will send your local file tree to the remote Docker daemon. When you then run docker-compose up the remote Docker will use the code built into the image, and not try to overwrite it with content on the remote system.

This same principle applies in other contexts that don't necessarily involve a second host; for example, if you're launching a container from inside another container using the host's Docker daemon, volume mounts are in the host and not the container filesystem. In general I'd recommend not trying to overwrite your application code with volumes: mounts; run the code that's actually built into the image instead, and use local non-Docker tooling for live development.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文