Notes

Docker

Docker is an open source platform that enables developers to build, deploy, run, update and manage containerized applications.

Commands

Docker Networking

By default 3 networks

  1. Bridge: ports are attached to default bridge network (usually 172.17.00 series).
  2. None: no port mapping.
  3. Host: no need of mapping; ports are directly attached to the Host network.

Docker has a built-in embedded DNS server that maps the containers’ name to its ip address


Dockerfile

# Start with a base image containing Go runtime
FROM golang:latest

# Set the Current Working Directory inside the container
WORKDIR /go/src/app

# Copy everything from the current directory to the PWD (Present Working Directory) inside the container
COPY . .

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Build the Go app
RUN go build -o main .

# Expose port 8080 to the outside world
EXPOSE 8180

# Command to run the executable
CMD ["./main"]

ENTRYPOINT & CMD --> used for commands; entrypoint is startup command & cmd is appended to it if no command is given in
command-line (like default)

Docker Compose

Docker Compose is a tool for defining and running multi-container applications. It is the key to unlock a streamlined
and efficient development and deployment experience.

version: '3.8'

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - backend
    networks:
      - app-network

  backend:
    image: node:14
    volumes:
      - ./backend:/app
    working_dir: /app
    command: "npm start"
    env_file:
      - .env
    depends_on:
      - db
    networks:
      - app-network

  db:
    image: postgres:13
    volumes:
      - db-data:/var/lib/postgresql/data
    env_file:
      - .env
    networks:
      - app-network

volumes:
  db-data:

networks:
  app-network: