In this blog we are going to discuss on the day to day common commands that every developer uses in daily life while working with docker.

Setup to Build the image

create docker-compose.yml file as shown in below code

# docker-compose.yml
version: "3.3"
services:
  main_web_server:
    build: ./
    restart: always
    hostname: App001
    deploy:                          <========= only for multiple nodes
      replicas: 6
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 10
        window: 120s
    ports:
      - 80:7500

and Dockerfile

FROM node:latest

WORKDIR /usr/app

COPY package.json .

RUN npm i --quiet

COPY . .

RUN npm install pm2 -g
RUN npm install bcrypt

RUN pwd && git submodule update --init --recursive

CMD ["pm2-runtime", "app.js"]

Explaination of above

In docker-compose.yml file

  • version
    • specify the version of docker-compose file
  • build
    • Path to code folder
  • hostname
    • App hostname which you would like to use for connecting from other services
  • services
    • your services ( your application )
  • ports
    • It is host to container port mapping in the format <host_port>:<container_port>

In Dockerfile

  • FROM node:latest
    • From <image_name>:<image_version>
    • This will be fetched from docker hub
  • WORKDIR /usr/app
    • Your code to be placed here
  • COPY package.json .
    • copy your package.json file and install node_modules before proceeding further
  • RUN npm i –quiet
    • installing the node_modules using package.json
  • COPY . .
    • copy your code to WORKDIR
  • RUN npm install pm2 -g
    • install pm2 package ( please refer to pm2 docs)
  • RUN npm install bcrypt
    • any other package to install, in my case I explicitly install bcrypt
  • RUN pwd && git submodule update –init –recursive
    • in case you are using git submodule, then run the above command to install it.
  • CMD [“pm2-runtime”, “app.js”]
    • the command that needs to run to start the server

Building Image

docker build -t <name_of_image> .

Here, name_of_image is your custom defined name which you want to use. And dot(.) represent the location of the dockerfile

Running the image

docker images

then grap the image id and execute the below command

docker run <image_id>

Running this image with Environment Variables

docker run --env-file <environment_file_with_path> <image_id>

example

docker run --env-file ./.env.test 347cf21330a17

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.