Home > Net >  docker-compose always exec using nodejs
docker-compose always exec using nodejs

Time:10-28

I am pulling my hair on this.. I don't know where it went wrong, but it seems that the docker container always execute whatever command I put with nodejs. The only way it result not with an error, is when I put "index.js" as single command at docker-compose.yml

enter image description here

I am new with docker, but is there some place I should look at?

My dockerfile:

FROM node:17
WORKDIR /opt/application
COPY ./certificate/server* ./certificate/
COPY package.json .
COPY config.json .
COPY tsconfig.json .
COPY ./src/ ./src
RUN npm install nodemon typescript -g
RUN npm install
RUN tsc -p .

docker-compose.yml

version: "3.9"
services:
  web:
    build: .
    ports: 
      - "80:3000"
    container_name: nodejs
    volumes:
      - "./src:/opt/application/src"
    depends_on:
      - "mongo"
    command:
      - "tsc -w"
      - "nodemon"
  mongo:
    image: "mongo:latest"
    ports:
      - "27017:27017"
      

I don't know where the configuration to add nodejs is.

Any help is appreciated. Thank you

CodePudding user response:

Presumed that you are fully aware that you have set working directory to /opt/application in the container. Try:

version: "3.9"
services:
  web:
    build: .
    ports: 
      - "80:3000"
    container_name: nodejs
    volumes:
      - "./src:/opt/application/src"
    depends_on:
      - "mongo"
    command: ["tsc","-w","nodemon"]
  mongo:
    image: "mongo:latest"
    ports:
      - "27017:27017"

CodePudding user response:

Well, of course it's running every command with node, it's explicitly for what that image is done.

When we perform docker history node:17 --no-trunc, we can see that the last two layers of the base image are:

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node"]

If we get the contents of the docker-entrypoint.sh we can see the following script:

#!/bin/sh
set -e

# Run command with node if the first argument contains a "-" or is not a system command. The last
# part inside the "{}" is a workaround for the following bug in ash/dash:
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=874264
if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ] || { [ -f "${1}" ] && ! [ -x "${1}" ]; }; then
  set -- node "$@"
fi

exec "$@"

So since you're using tsc -w as first command, it recognizes the dash and the command runs with node (as expected).

Also, it seems you misunderstood the functionality of command in a Compose file: you cannot add more than one command.

So basically what you're forcing is the following:

CMD ["tsc -w", "nodemon"]

I suppose what you're trying to achieve is the following:

package.json

"start": "tsc && concurrently \"tsc -w\" \"nodemon\" "

docker-compose.yml

 version: "3.9"
services:
  web:
    build: .
    ports: 
      - "80:3000"
    container_name: nodejs
    volumes:
      - "./src:/opt/application/src"
    depends_on:
      - "mongo"
    command: npm run start
  mongo:
    image: "mongo:latest"
    ports:
      - "27017:27017"
  • Related