提问人:Mustafa Tunahan Tuna 提问时间:11/17/2023 最后编辑:Mustafa Tunahan Tuna 更新时间:11/19/2023 访问量:54
我无法将使用 Docker Compose 创建的容器挂载到本地的文件夹中,这是怎么回事?
I cannot mount the container I created with Docker Compose to a folder in my local, what's wrong?
问:
我正在使用 Node.js 和 Express 运行一个简单的应用程序。我正在尝试创建应用程序的 Docker 映像,并使用 Docker Compose 使用该映像创建容器。我想将工作区中的文件挂载到生成的容器(在本例中为 index.js、package.json 和其他文件)到 docker-compose.yml 文件所在目录中的 ./test,但我收到错误。
源代码
索引.js
const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
res.send("Hello Docker Compose!");
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
package.json
{
"name": "my-node-app",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"start": "node index.js"
}
}
Docker文件
FROM node:14
WORKDIR /app
COPY package.json /app
COPY index.js /app
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]
我正在使用以下命令构建应用程序:
docker build -t express-app:latest .
在另一个名为 test-app 的文件夹中,我使用命令运行以下 docker-compose.yml 文件。docker compose up
容器中 workdir 中的文件没有进入 ./test 文件夹,我收到以下错误。
Attaching to test-app-express-app-1
test-app-express-app-1 | npm ERR! code ENOENT
test-app-express-app-1 | npm ERR! syscall open
test-app-express-app-1 | npm ERR! path /app/package.json
test-app-express-app-1 | npm ERR! errno -2
test-app-express-app-1 | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
test-app-express-app-1 | npm ERR! enoent This is related to npm not being able to find a file.
test-app-express-app-1 | npm ERR! enoent
test-app-express-app-1 |
test-app-express-app-1 | npm ERR! A complete log of this run can be found in:
test-app-express-app-1 | npm ERR! /root/.npm/_logs/2023-11-17T15_23_04_691Z-debug.log
test-app-express-app-1 exited with code 254
我不明白我做错了什么。你可以帮我吗?
docker-compose.yml
这个yml文件是我想运行的
version: '3'
services:
express-app:
container_name:test
image: express-app:latest
ports:
- 3000:3000
volumes:
- ./test:/app
这个yml文件有效,但它没有在我工作的目录中执行挂载操作:
version: '3'
services:
express-app:
container_name:test
image: express-app:latest
ports:
- 3000:3000
volumes:
- test:/app
volumes:
test:
此 yaml 文件是名为 Strapi 的 nodejs 无头 cms 应用程序映像的 docker-compose.yml 文件
version: '3'
services:
strapi:
container_name: strapi
image: strapi/strapi
environment:
- NODE_ENV=production
- DATABASE_CLIENT=postgres
- DATABASE_HOST=db
- DATABASE_PORT=5432
- DATABASE_NAME=strapi
- DATABASE_USERNAME=strapi
- DATABASE_PASSWORD=strapi
ports:
- 1337:1337
volumes:
- ./app:/srv/app
depends_on:
- db
command: 'strapi start'
db:
container_name: postgres
image: postgres
restart: always
volumes:
- ./db:/var/lib/postgresql/data
environment:
POSTGRES_USER: strapi
POSTGRES_PASSWORD: strapi
POSTGRES_DB: strapi
答:
您构建的映像中似乎没有。
键入 时。
你没有在构建docker镜像时遇到错误吗?package.json
docker build -t express-app:latest
就像控制台中的以下错误一样。
=> ERROR [3/4] COPY package.json /app
您可以修改 Dockerfile 以找出 Docker 编写时内部的内容。
FROM node:14
WORKDIR /app
COPY package.json /app
COPY test.js /app
CMD "tail -f /dev/null"
docker compose up后,键入以查看正在运行的docker进程,
键入以获取sh 。docker ps
docker exec -it [docker-name] /bin/sh
更新细节的答案
- 创建测试包.json和测试.js
{
"hi" : "hello"
}
console.log('hi')
- 创建 Dockerfile
FROM node:14
WORKDIR /app
COPY package.json /app
COPY test.js /app
- 构建映像并运行它 确保使用自定义标签不是最新的(为方便起见)。
docker build -f dockerfile -t express-app:local .
docker run -dit --name express express-app:local
docker ps
## check container's running
执行容器的 bin/sh 并检查应用程序是否具有包,test.js
docker exec -it express /bin/sh
# ls
package.json test.js
# pwd
/app
- 现在从映像构建 docker-compose
为了不混淆应用程序,我将另一个挂载卷更改为已加载
version: '3'
services:
mycustom:
container_name: mycustom
image: express-app:local
volumes:
- ./loaded:/app/loaded
command: "tail -f /dev/null"
- 检查容器包含文件夹和 test.js、package.json
docker-compose -f docker-copmose.yaml up
// go to another terminal
docker exec -it mycustom /bin/sh
docker exec -it mycustom /bin/sh
# pwd
/app
# ls
loaded package.json test.js
评论
test-app-express-app-1 | npm ERR! path /app/package.json test-app-express-app-1 | npm ERR! errno -2 test-app-express-app-1 | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
您的日志显示容器内没有文件。
docker cp
评论
volumes:
/app
volumes:
/app
package.json
docker build