繁体   English   中英

Nodejs 不以 docker-compose 开头

[英]Nodejs doesn't start with docker-compose up

我正在尝试用 docker 和节点做一个非常基本的事情。 我有一个带有 Nginx 的客户端和一个带有 nodejs 的后端。

我的问题是,当我尝试运行docker-compose up -d时,它给了我一个ok但后端没有运行。

我认为这是由于“卷”的一些问题而发生的,因为如果我在 docker-compose 中去掉这部分,那么它就可以工作。 但我确实希望有一个卷来保存和存储所有后端相关文件。

这是我的docker-compose

version: '3.3'

services:
    nodeserver:
        container_name: nodebackend
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - ${APP_PATH}:/usr/src/app
        environment:
            TZ: 'Europe/Madrid'
        ports:
            - "5000:5000"
    nginx:
        container_name: client
        image: nginx:latest
        volumes:
            - ${APP_PATH}:/var/www/api
            - ${NGINX_APIX_LOGS}:/var/log/nginx/
        environment:
            TZ: 'Europe/Madrid'
        ports:
            - '8080:80'

这是我的 Dockerfile (在同一目录中)

# pull the Node.js Docker image
FROM node:12.15.0-alpine

# create the directory inside the container
WORKDIR /usr/src/app

# copy the package.json files from local machine to the workdir in container
COPY package*.json ./

# run npm install in our local machine
RUN npm install --quiet
RUN npm install realm --quiet

# copy the generated modules and all other files to the container
COPY . /usr/src/app

# our app is running on port 5000 within the container, so need to expose it
EXPOSE 5000

# the command that starts our app
CMD ["node", "app.js"]

目录树是这样的:

-rw-r--r--   1 user  staff     27 Dec 29 11:41 .dockerignore
-rw-r--r--@  1 user  staff    231 Dec 29 11:02 .env
-rw-r--r--@  1 user  staff    575 Dec 29 12:46 Dockerfile
drwxr-xr-x   3 user  staff     96 Dec 29 12:03 api.files
-rw-r--r--   1 user  staff    592 Dec 29 12:47 docker-compose.yml
drwxr-xr-x   3 user  staff     96 Dec 29 11:02 logs
drwxr-xr-x  53 user  staff   1696 Dec 29 11:53 node_modules
-rw-r--r--   1 user  staff  31286 Dec 29 11:53 package-lock.json
-rw-r--r--   1 user  staff    338 Dec 29 11:53 package.json

.env 文件是这个:

# Application's path (absolute or relative)
APP_PATH=/Users/user/Development/ContactListWebApp/api.files/

# Logs path
NGINX_APIX_LOGS=/Users/user/Development/ContactListWebApp/logs/nginx

如果我运行docker-compose up -d我会收到以下消息:

Starting client        ... done
Recreating nodebackend ... done

但是如果我运行docker ps -a

这就是我所拥有的:

CONTAINER ID   IMAGE                          COMMAND                  CREATED          STATUS                      PORTS                  NAMES
d55463667a3d   contactlistwebapp_nodeserver   "docker-entrypoint.s…"   24 seconds ago   Exited (1) 23 seconds ago                          nodebackend
4b85504235a3   nginx:latest                   "/docker-entrypoint.…"   18 minutes ago   Up 23 seconds               0.0.0.0:8080->80/tcp   client

package.json文件包含以下信息:

{
  "name": "contactlistwebapp",
  "version": "1.0.0",
  "description": "Test application to list contacts on web app linked to iOS app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Josman Pérez",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1"
  }
}

我做错了什么? 如果我在 docker-compose 中评论卷部分,那么它会运行

谢谢

有两个问题。

第一的

在此命令CMD ["npm", "app.js"]中,它不会执行节点应用程序。您应该使用node来运行您的应用程序,例如CMD ["node", "app.js"]

第二

在您的 docker-compose.yml 中,您使用此卷。

volumes:
    - ${APP_PATH}:/usr/src/app

您的APP_PATHAPP_PATH=/Users/user/Development/ContactListWebApp/api.files/ 在构建 Dockerfile 并启动容器后,您的/usr/src/app将使用volumes config中的${APP_PATH}挂载。 这意味着您的 Dockerfile 生成的其他文件和目录(复制、运行 npm 安装)将消失。 您可以将 CMD 更改为此CMD ["ls", "-al"] ,您会发现您只有app.js 没有node_modules,package.json等。

因此,在您使用docker logs container_id (封闭容器)后,您会发现您的节点找不到 express 模块。

解决方案

如果你真的想使用卷配置,你不应该把它挂载到你的 WORKDIR (/usr/src/app),你应该把它挂载到其他路径。 当您想使用您的卷时,您只需在该路径中找到文件,例如/usr/src/app2

因此,在您的情况下,您可以更改卷路径。 如果您不需要使用${APP_PATH}中的文件,您可以删除您的卷。

# this is just an example
volumes:
    - ${APP_PATH}:/usr/src/app2

然后用CMD ["node", "api.files/app.js"]更改 Dockerfile 中的 CMD 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM