无法使用Docker上下文将本地目录复制到带有Docker的远程容器
我一直在使用本地计算机,没有任何问题。
我一直在使用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
anddocker 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
卷:
始终由运行容器的Docker守护程序解释。如果您使用上下文指向远程Docker守护程序,则卷:
命名卷和文件路径由该远程守护程序解释,并指向远程主机上的文件。Docker Build
将将本地构建上下文发送到远程Docker守护程序,而卷:
不能用于在主机之间复制文件。此处的直接解决方案是从组合设置中删除
卷:
:然后,当您
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, thevolumes:
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: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 rundocker-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.