简体   繁体   中英

Nodejs doesn't start with docker-compose up

I am trying to do a very basic thing with docker and node. I have a client with Nginx and a backend with nodejs.

My problem is that when I tried to run docker-compose up -d it gives me an ok but the backend is not running.

I think this is happening due to some problem with the "volumes", because if I get rid of this part in the docker-compose then it works. But I do want to have a volume to save and store all the backend related files.

This is my 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'

And this is my Dockerfile (in the same directory)

# 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"]

The directory tree is this:

-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

The.env file is this one:

# 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

If I run docker-compose up -d I get this message:

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

But if I run docker ps -a

This is what I have:

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

The package.json files have this information:

{
  "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"
  }
}

What I am doing wrong? If I comment the volume part in the docker-compose then it runs

Thank you

There are two problems.

First

In this command CMD ["npm", "app.js"] , it will not execute the node application.You should use node to run your application, such as CMD ["node", "app.js"] .

Second

In you docker-compose.yml, you use this volumes.

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

Your APP_PATH is APP_PATH=/Users/user/Development/ContactListWebApp/api.files/ . After Dockerfile is built and container start, your /usr/src/app will be mounted with your ${APP_PATH} in volumes config . That means your other files and directories which are generated (COPY, RUN npm install) by your Dockerfile will disappear. You could change your CMD to this CMD ["ls", "-al"] , you'll find you only have app.js . There are no node_modules, package.json etc.

So, after you use docker logs container_id (closed container), you'll find your node couldn't find the express modules.

Solution

If you really want to use volumes config, you should not mount it to your WORKDIR (/usr/src/app), you should mount it to other path. When you want to use your volumes, you just find the files in that path, such as /usr/src/app2 .

So, in your case, you could change the volumes path. If you don't need to use files in ${APP_PATH} , you could just remove your volumes.

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

Then change your CMD in Dockerfile with CMD ["node", "api.files/app.js"] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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