Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node + Docker Compose: Development and production setup

I'm looking for a solution to have both a development and a production environment in my project using docker, docker-compose and nodejs.

How do I approach this?

Basically what I want is a command to start my docker production environment, and a command to start my development environment (which could use nodemon for example).

Here is my Dockerfile

FROM node:13-alpine

RUN mkdir /app

WORKDIR /app

COPY . /app

RUN npm install

RUN npm run build

EXPOSE 1234

CMD ["npm", "run", "prod"] # <--- Have a possibility to run something like "npm run dev" here instead

docker-compose.yml

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

package.json

// ...
    "scripts": {
        "build": "tsc",
        "dev": "nodemon source/index.ts",
        "prod": "node build/index.js"
    },
// ...
like image 883
Sir hennihau Avatar asked May 31 '20 20:05

Sir hennihau


People also ask

Is Docker compose good for production?

Docker Compose is an excellent tool for optimizing the process of creating development, testing, staging, and production environments. With Docker Compose, you'll use a single file to build your environment instead of several files with complex scripts with a lot of branching logic.

What are the three main steps of Docker compose?

Using Compose is basically a three-step process: Define your app's environment with a Dockerfile so it can be reproduced anywhere. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.


2 Answers

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.

like image 193
Michał Krzywański Avatar answered Sep 28 '22 03:09

Michał Krzywański


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
like image 34
Adi Dembak Avatar answered Sep 28 '22 04:09

Adi Dembak