# Docker

⚡️ Tags: 📍Tools 📍Overview 📍Docker

# Concepts

Containerization

Docker Concepts

# Main components

Docker Main components

  • Docker Hub: store Docker Image
  • Docker Engine: create Docker Image & run Docker Container
  • Docker Machine: create Docker Engine on Server
  • Docker Compose: run application by defining Docker Container configuration in file
  • Docker Image: (immutable)
  • Docker Container: …

# Benifits

Docker Benifits

# How it works

How docker works

Docker's architecture comprises three main components:

🔹 Docker Client This is the interface through which users interact. It communicates with the Docker daemon.

🔹 Docker Host Here, the Docker daemon listens for Docker API requests and manages various Docker objects, including images, containers, networks, and volumes.

🔹 Docker Registry This is where Docker images are stored. Docker Hub, for instance, is a widely-used public registry.

Read more (opens new window)

# Workflow

Docker Workflow 2

Docker Workflow

# Use cases

Docker use cases

# Reduce Docker Image Size

# 1. Using distroless/minimal base images.

Use Alpine: Lightweight, Minimalist, Security-focused

# 2. Multistage builds

FROM node:16

COPY . .

RUN npm install

EXPOSE 8080

CMD [ "node", "index.js" ]

# 3. Minimizing the number of layers

Docker images work in the following way – each RUN, COPY, FROM Dockerfile instructions add a new layer & each layer adds to the build execution time & increases the storage requirements of the image.

# 4. Understanding caching

As Docker uses layered filesystem, each instruction creates a layer. Due to which, Docker caches the layer and can reuse it if it hasn’t changed. => installing dependencies & packages earlier inside the Dockerfile before the COPY commands.

FROM ubuntu:latest

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update -y && \
    apt-get upgrade -y && \
    apt-get install -y vim net-tools dnsutils

COPY . .

# 5. Keeping data elsewhere

  • As a rule, only the necessary files need to be copied over the docker image. Docker can ignore the files present in the working directory if configured in the .dockerignore file.
  • Storing application data in the image will unnecessarily increase the size of the images.
  • It’s highly recommended to use the volume feature of the container runtimes to keep the image separate from the data.

# Commands

Docker commands

# Image

docker images

# Container

List running container only

docker ps 

All of list, includes hidden ones

docker ps -a 

# Save as Image

Container must be stoped before committing.

docker commit <container-name> <new-image-name:tag>

# Run

# Expose Port

docker run -d -p 5801:5800 --name vnc1 myvnc 

multi ports

docker run -d -p 5801:5801 -p 5802:5802 .....

# Volume

docker run -it -v <path-host>:<container-path> <ID or NAME>
# E.g:
docker run -it -v /Users/nguyenkhank/Desktop/jav:/home/dulieu ubuntu

Use the same data:

docker run -it --name C2 --volumes-from C1 ubuntu:latest

# With Volume

docker run -it --name <container-name> --mount source=<volume-name>,target=<container-path> <image-name:tag>
# E.g:
docker run -it --name C1 --mount source=D1,target=/home/dulieu ubuntu:16.0.4

Case volume mounted with device

docker run -it --name <container-name> --mount source=<volume-name>,target=<container-path> <image-name:tag>
# E.g:
docker run -it --name C1 -v DISK1:/home/dulieu ubuntu:16.0.4

# Exit

# Exit and Stop Docker Container

  • Ctrl+C to send the SIGINT signal and stop the process. Next, press Ctrl+D to exit and stop the container.
  • Alternatively, type exit

# Exit Docker Container without Stopping It

  • press Ctrl+P followed by Ctrl+Q. This operation detaches the container and allows you to return to your system's shell.

# Volume

docker volume ls

# Network

Detail (opens new window) Detail (opens new window)

docker network ls 

# Other

Info

docker info
docker -v

Help

docker image --help

Search

docker search <keyword>

# Simple build

const app = express();

// your code
const PORT = 8080;
const HOST = '0.0.0.0';
app.listen(PORT, HOST);

# Docker vs K8S

Docker vs K8S

  • Concepts
  • Main components
  • Benifits
  • How it works
  • Workflow
  • Use cases
  • Reduce Docker Image Size
  • 1. Using distroless/minimal base images.
  • 2. Multistage builds
  • 3. Minimizing the number of layers
  • 4. Understanding caching
  • 5. Keeping data elsewhere
  • Commands
  • Image
  • Container
  • Save as Image
  • Run
  • Exit
  • Volume
  • Network
  • Other
  • Simple build
  • Docker vs K8S