Docker 卷问题

huangapple go评论54阅读模式
英文:

Docker Volume Issues

问题

以下是您要翻译的部分:

我一直在尝试使用 Docker 卷来持久保存我的应用程序。我对 Docker 还不熟悉,我是在 Mac 上进行操作的。每次运行我的 Docker 命令时,都会出现错误,说我的工作目录是“无效模式”。任何帮助将不胜感激。

这是我的 Docker 文件:

FROM node:18-alpine

WORKDIR /usr/app

COPY . .

RUN npm install

CMD [ "npm", "run", "start" ]

EXPOSE 5200

使用以下命令运行我的镜像:

docker run -p 5200:5200 -it --name server -v $(pwd):/usr/app -v /usr/app/node_modules server-docker

这是我收到的错误:

docker: Error response from daemon: invalid mode: /usr/app.
See 'docker run --help'.
英文:

I have been trying to use docker volume to persist my app. I'm new to docker and I am doing this on a mac. Every time I run my docker command I get an error saying my workdir is "invalid mode". Any help would be appreciated

This is my docker file

FROM node:18-alpine

WORKDIR /usr/app

COPY . .

RUN npm install

CMD [ "npm", "run", "start" ]

EXPOSE 5200

Running my image with this

docker run -p 5200:5200 -it --name server -v $(pwd):/usr/app -v /usr/app/node_modules server-docker

This is the error i receive

docker: Error response from daemon: invalid mode: /usr/app.
See 'docker run --help'.

答案1

得分: 0

问题出在你的Docker卷挂载语法这一部分:-v /usr/app/node_modules。Docker期望卷挂载语法的格式是<主机目录>:<容器目录>,但在你的命令中,似乎缺少容器目录路径。

你已经将当前目录 ($(pwd)) 挂载到容器中的 /usr/app。你不应该将 node_modules 挂载到容器中,因为当你从 Dockerfile 运行 npm run install 命令时,node_modules 目录会被创建。

最佳解决方案是在 Dockerfile 所在位置创建一个 .dockerignore 文件,并将 node_modules 添加到忽略列表中,就像这样添加到 .dockerignore 文件中:

node_modules

之后,你可以运行以下命令启动容器:

docker run -p 5200:5200 -it --name server -v $(pwd):/usr/app server-docker

我建议学习多阶段构建和了解 dockerignore。只需在搜索引擎中搜索关于Node或你正在使用的任何框架的相关信息。

英文:

The issue here is with your Docker volume mount syntax in this part: -v /usr/app/node_modules. Docker is expecting the volume mount syntax to be in the form <host-dir>:<container-dir>, but in your command, it appears that the container directory path is missing.

You're already mounting the current directory ($(pwd)) into /usr/app in the container. You should never mount node_modules into the container because node_modules directory get created when you docker run the npm run install command from the dockerfile.
Best solution would be to create .dockerignore file on the location where dockerfile is and add node_modules to the ignore list like this to the .dockerignore file.

node_modules

After that you can run following command to start the container.

docker run -p 5200:5200 -it --name server -v $(pwd):/usr/app server-docker

I would suggest to learn multistage build and about dockerignore. Just google it for node or whatever framework you are using.

huangapple
  • 本文由 发表于 2023年5月25日 02:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定