GithubHelp home page GithubHelp logo

gprodev / node-js-docker Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 138 KB

Creating a Docker container based development environment for a Node.js project.

Shell 0.16% Dockerfile 3.96% JavaScript 95.89%

node-js-docker's Introduction

Learning Docker

Basics

Configuration file

Create a file named Dockerfile. This file will contain all the instructions to build our Docker image.

Example content:

FROM node:15
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD [ "node", "index.js" ]

Build Docker image

docker build . -t <image-name> <base-folder>

Example:

docker build -t node-app-image .

Run container

docker run -d -p <local-port>:<app-port> --name <container-name> <docker-image-name>

Example:

docker run -d -p 3000:8080 --name my-app my-app-image

Login into container bash

docker exec -it node-app bash

Sync local folder to container folder

docker run -d -v <local-folder>:<container-folder> -p <local-port>:<app-port> --name <container-name> <docker-image-name>

The -v option can be used multiple times to sync multiple folders. Example:

docker run -d -v $PWD:/app -p 3000:8080 --name my-app my-app-image

On Mac OS, $PWD represent the current folder.

Use environment variables

docker run -d -e <env-var-name>=<env-var-value> -p <local-port>:<app-port> --name <container-name> <docker-image-name>

The -e option is equivalent to --env and can be used multiple times for multiple environment variables

Use env-file

Create a file that will contain environment variables (e.g .env). Example content:

PORT=4000

Then use the option --env-file to pass this file

docker run -d --env-file <env-file-path> -p <local-port>:<container-port> --name <container-name> <docker-image-name>

Example:

docker run -d --env-file ./.env -p 3000:8080 --name node-app node-app-image

Delete a container

docker rm <container-name> -fv

The -f option forces the deletion of the container even if it's running, the option -v deletes the volume associated with the container.

docker-compose

Basically docker-compose allows to manage multiple containers from a single configuration.

Configuration file

Create a file named docker-compose.yml that will contain the configuration instructions.

Example:

version: "3"
services: 
  node-app:
    build: .
    ports: 
      - "3000:3000"
    volumes: 
      - ./:/app
      - /app/node_modules
    environment: 
      - PORT=3000
    # env_file: 
    #   - .env

Run containers

docker-compose up -d --build

The --build option is used to force build.

Stop/delete containers

docker-compose down -v

Differenciation between development and production

Change the content of docker-compose.yml to this:

version: "3"
services: 
  node-app:
    build: .
    ports: 
      - "3000:3000"
    environment: 
      - PORT=3000

Then create 2 files docker-compose.dev.yml and docker-compose.prod.yml respectively for development and production environments.

# docker-compose.dev.yml content
version: "3"
services: 
  node-app:
    build: 
      context: .
      args: 
        NODE_ENV: development
    volumes: 
      - "./:/app"
      - "/app/node_modules"
    environment: 
      - NODE_ENV=development
    command: npm run dev
# docker-compose.prod.yml content
version: "3"
services: 
  node-app:
    build: 
      context: .
      args: 
        NODE_ENV: production
    environment: 
      - NODE_ENV=production
    command: node index.js

Each one of these files overrides parts of the configuration for docker-compose.yml according to the target environment.

Start dev

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

Stop dev

docker-compose -f docker-compose.yml -f docker-compose.dev.yml down -v

Start prod

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

Stop prod

docker-compose -f docker-compose.yml -f docker-compose.dev.yml down -v

The --build option forces image rebuild, the -v option when destroying containers removes the associated volumes

Adding MongoDB

Add MongoDB as a service in docker-compose.yml

version: "3"
services: 
  node-app:
    build: .
    ports: 
      - "3000:3000"
    environment: 
      - PORT=3000
    depends_on: 
      - mongo

  mongo:
    image: mongo
    environment: 
      - MONGO_INITDB_ROOT_USERNAME=gprodev
      - MONGO_INITDB_ROOT_PASSWORD=mypassword
    volumes: 
      - mongo-db:/data/db

volumes: 
  mongo-db:

Using a named volume allows our mongo container to preserve its database between restart.

Caution: At this point avoid using the flag -v when running docker-compose ... down as it will delete volumes including named ones, thus deleting the database.

Deploy to production

Install Docker on Lunix server (the simple way)

  1. Run the following command on the server terminal to download the Docker installation script
curl -fsSL https://get.docker.com -o get-docker.sh
  1. Run the Docker installation script
sh get-docker.sh
  1. Once complete, check the installation with this commande
docker -v

Install docker-compose

  1. Run the following command to get the docker-compose binary
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Then run this command to make docker-compose executable
sudo chmod +x /usr/local/bin/docker-compose
  1. Use this command to check if docker-compose works correctly
docker-compose -v

Configure the environment variables for production

Configure docker-compose.prod.yml's environment variable to equal the environment variables of the same names on the host machine.

# docker-compose.prod.yml
version: "3"
services: 
  nginx:
    ports: 
      - "80:80"
  node-app:
    build: 
      context: .
      args: 
        NODE_ENV: production
    ports: 
      - "80:80"
    environment: 
      - NODE_ENV=production
      # With the following syntax, docker sets the environment variables to their value on the host machine
      - MONGO_USER=${MONGO_USER}
      - MONGO_PASSWORD=${MONGO_PASSWORD}
      - SESSION_SECRET=${SESSION_SECRET}
    command: node index.js

  mongo:
    image: mongo
    environment: 
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}

Set environment variables on the host machine

  1. Create a file (say .env) that contains the environment variables
# .env file
NODE_ENV=production
MONGO_USER=gprodev
MONGO_PASSWORD=mypassword
SESSION_SECRET=secret
MONGO_INITDB_ROOT_USERNAME=gprodev
MONGO_INITDB_ROOT_PASSWORD=mypassword
  1. Open the current user's .profile for edit
vi ~/.profile

and add the following line at the end of the file and save

set -o allexport; source ~/.env; set +o allexport
  1. Exit the SSH session and log in again
  2. Run the following command to verify the that your environment variables have been set
printenv

Run in production

Now you can clone your project on your production host machine using Git.

Then run de following comment to start in production mode

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

node-js-docker's People

Contributors

gprodev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.