Node + Docker Compose: Development and production setup

You can create a structure like this:

docker-compose.yml -->
      docker-compose.dev.yml
      docker-compose.prod.yml

Where the base configuration resides in docker-compose.yml, while environment-specific info such as ports or user credentials would be in docker-compose.dev.yml or docker-compose.prod.yml

And then you can run the dev environment with:

docker-compose \
    -f docker-compose.yml \
    -f docker-compose.dev.yml \
    up -d

Or the prod environment with:

docker-compose \
    -f docker-compose.yml \
    -f docker-compose.prod.yml \
    up -d

You can make use of entrypoint and pass the command to the docker container. Then you can use docker-compose inharitance to launch compose for the environment that you want and append command to the entrypoint.

Dockerfile :

FROM node:13-alpine

RUN mkdir /app

WORKDIR /app

COPY . /app

RUN npm install

RUN npm run build

EXPOSE 1234

ENTRYPOINT ["npm", "run"]

Main docker-compose.yml :

version: "3"
services:
    findus:
        build: .
        ports:
            - "1234:1234"
        links:
            - mongo
        container_name: myapp
    mongo:
        image: mongo
        restart: always
        ports:
            - "4444:4444"

And then have two docker-compose files to append the command passed to the image entry point. For development - docker-compose.dev.yml :

version: "3"
services:
    findus:
        command: dev

and docker-compose.prod.yml :

version: "3"
services:
    findus:
        command: prod

Then to launch dev environment :

docker-compose  -f docker-compose.yml -f docker-compose.dev.yml up    

and for prod environment :

docker-compose  -f docker-compose.yml -f docker-compose.prod.yml up   

So the command will be appended to the ENTRYPOINT instruction.


Also this approach could work with enviroment variables if you wanted to pass the command as environment variable. You can find more information in the docs.