我最近开始从Docker容器运行postgres,并将后端迁移到单独的容器。这是我的docker-compose.yml文件
version: "3.7"
services:
postgres:
image: postgres:9.6-alpine
ports:
- "5432"
volumes:
- postgres_data:/var/lib/postgresql/data
backend:
build:
context: ./
dockerfile: Dockerfile
volumes:
- ../../:/var/www
ports:
- "3000:3000"
links:
- postgres
restart: always
environment:
- PORT=3000
- HOST=0.0.0.0
- PG_HOST=postgres
- PG_USER=postgres
- PG_DATABASE=postgres
- NODE_ENV=development
volumes:
postgres_data:
这是我的Dockerfile
FROM alpine:3.8
RUN apk add -U python g++ yarn nodejs npm make
WORKDIR /var/www/server
COPY package*.json ./
RUN npm install
COPY . .
RUN pwd
RUN ls -l
EXPOSE 3000
CMD ["npm", "start"]
我的Makefile看起来像这样
build: docker-compose.yml
docker-compose build
touch build
dev: build
docker-compose up
我的项目结构如下
server
├── app
│ ├── db
│ └── models
├── build
├── docker-compose.yml
├── Dockerfile
├── index.js
├── Makefile
├── package.json
├── package-lock.json
└── README.md
所以我运行make dev
创建docker容器,当我实际启动docker容器时,我不断收到错误消息
npm ERR! path /var/www/server/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/var/www/server/package.json'
但是,我的ls -l在docker容器的项目目录中的输出是
-rwxrwxrwx 1 root root 205 Apr 4 16:39 Dockerfile
-rwxrwxrwx 1 root root 96 Apr 4 06:19 Makefile
-rwxrwxrwx 1 root root 216 Apr 4 04:10 README.md
drwxrwxrwx 4 root root 4096 Apr 4 16:36 app
-rwxrwxrwx 1 root root 0 Apr 4 16:38 build
-rwxrwxrwx 1 root root 561 Apr 4 16:39 docker-compose.yml
-rwxrwxrwx 1 root root 205 Apr 4 16:36 index.js
drwxrwxrwx 176 root root 4096 Apr 4 04:32 node_modules
-rwxrwxrwx 1 root root 24143 Apr 4 04:28 package-lock.json
-rwxrwxrwx 1 root root 676 Apr 4 16:36 package.json
和package.json在那里。我还尝试将CMD设置为仅在Dockerfile中的节点index.js,但这也不起作用,说找不到index.js,这使我相信我可能不在正确的目录中( ?)。