App Deployment With Docker

DEPLOYING WITH DOCKER

Serikiayodele
4 min readFeb 7, 2023

Prerequisites

  • Node.js Application
  • Docker

A little intro…

Part 1 of this article goes over how to containerise a simple node.js application and deploy it using docker.

The importance/point of docker is the ability to put all of your application and it’s dependencies in on place and move it around easily. Here’s an article that breaks it down better. Click

Let’s get into it…

Step 1- prepare your node.js

I’ve prepared node.js application in the server.js file which can be found here server.js

Step 2-Create Docker Compose File

#docker-compose.yaml

version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=pass
volumes:
- mongo-data:/data/db
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
mongo-data:
driver: local
  • In the docker-compose file we can take all of our docker commands and it’s configuration and map it into a file, services is where the container list goes, here we have the mongodb and mongo-express containers.

Mongo

a) Image: Specifies the name of the image the container will be built from.

b) Ports: Specifies what ports the container will open.

c) Environment: variables can be specified in the docker compose file, here the user name and password will be mapped to monogo-express, and this is how mongo-express will be able to authenticate to mongodb.

Mongo-Express

a) Basically, same configuration with mongo. Although, an extra environment variable ME_CONFIG_MONGODB_SERVER which is the container name express will use to connect to docker, because they’re in the same network.

  • Start mongodb and mongo-express using docker-compose -f docker-compose.yaml up note: you have to be in the same directory as your docker-compose file

Step 3- Dockerfile For The Application

#Dockerfile

FROM node:13-alpine

RUN mkdir -p /home/app

# set default dir so that next commands executes in /home/app dir
WORKDIR /home/app

COPY ./app/package*.json /home/app

# will execute npm install in /home/app because of WORKDIR
RUN npm install

# no need for /home/app/server.js because of WORKDIR
CMD ["node", "server.js"] a) FROM : This is the first line in a docker file and it specifies the image i will be basing my application on.

b) RUN: Executes a linux command, the directory being created here is going to live in the container and not the host machine.

c) COPY ./app /home/app: Copies’s the content of ./app/package*.json which is the source into /home/app which is the destination.

d) WORKDIR: Set’s the working directory

e) CMD: Is always in the Dockerfile and it executes an entry point linux command, here the command translates to node server.js, which starts up the application.

note: Difference between CMD and RUN is we can have only one CMD command, but we can have several RUN commands

  • Build the an application image from the Dockerfile docker build -t <repository-name>/my-app:1.0 . where -t means tag, and the my-app application is given the 1.0 tag, if this is not specified it defaults to latest

note: you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web

here’s my image after building

Step 4- Push The Image To A Registry

note: I’ll be using a public registry here

a) log into dockerhub and create a public repository my-app, remember to make it public, by choosing the public option.

public-repository

b) Push the image to docker hub.

layers being pushed
Image pushed to repository

c) Add the app build to docker compose and run the docker compose command once again, so we can have the app container.

#docker-compose.yaml with the app

version: '3'
services:
my-app:
build: .
ports:
- 3000:3000
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=pass
volumes:
- mongo-data:/data/db
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
mongo-data:
driver: local

see also: App Deployment With Gitlab-CI, see also: App Deployment With Kubernetes

--

--

Serikiayodele
Serikiayodele

Written by Serikiayodele

Sharing as i learn about the cloud

Responses (1)