Source

What is Docker

  • A way to package software so it can run on any hardware
  • Solve problems like it works on my machine but it does not on others, by reproducing environments

Docker File

  • A blueprint for building a docker image
  • Defines the environment
  • Can rebuild the environment, saved as an immutable snapshot known as a docker image

Docker Image

  • Template for running docker containers
  • Can be uploaded to the cloud in both public and private registries
  • People who want to run the software can pull the image to create a container

Docker Container

  • A running process of the image
  • Useful for creating multiple instances of the same software, specifically for Kubernates and swarm

What to install

  • Docker GUI for Mac or Windows
  • Docker extension for VSCode for language support while writing docker files

How to dockerise the app (node.js app)

  1. Create Dockerfile in the root of the project
// Tells docker to use the NodeJS image
FROM node:12 
 
// Add the apps source code's directory, any subsequent instructions will start from this directory
WORKDIR /app 
 
// Install dependecies first so it can be cached
COPY package*.json ./
 
RUN npm install
 
// Copy over source code
COPY . .
 
// Set environment variable
ENV PORT=8080
 
// Listen to port 8080
EXPOSE 8080
 
// Only one of this layer in a dockerfile
// Tells the container how to run the actual application
CMD ["npm", "start"]
- Note that every line in the Dockerfile is considered as a step/layer
- Docker caches layers if nothing is changed

2. Create .dockerignore to ignore node_modules

node_modules
  1. Build the docker image docker build -t name/demoapp:1.0 .
    • -t is for tag to give it a name tag to access it easily later
    • 1.0 is the version number
  2. Publish with docker push and retrieve with docker pull
  3. To build the docker container from the image, docker run -p 5000:8080 [id/tagname]
    • 5000 is the local port we are opening, and maps it to the docker port 8080

Volumes

  • A dedicated folder on the host machine
  • In this folder, we can create files that can be remounted into future containers or multiple containers at the same time docker volume create shared-stuff docker run — mount source=shared-stuff, target=/stuff`

Debugging

  • Use docker GUI to see the logs of the container
  • Or use docker exec for docker cli

How to keep containers healthy

  • Write simple maintainable micro services

1 process per container

  • Each container should only run one process
  • Docker helps with ensuring different containers for different processes with docker compose, to run multiple docker containers at the same time

docker-compose.yml

version: '3'
services:
	web:
		build: .
		ports:
			- 8080:8080
 
	db:
		image: 'mysql'
		environment:
			- MYSQL_ROOT_PASSWORD: password
		volumes: 
			- db-data:/foo
 
volumes:
	db-data:
  • Each key in the services object represents a service that we want to run
  • build is to point to the directory
  • ports is for port forwarding configuration docker compose up to find this file and run all the containers together docker compose down to shut down all containers together