# Docker
# Concepts
# 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
# How it 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.
# Workflow
# Use cases
# Reduce Docker Image Size
# 1. Using distroless/minimal base images.
Use Alpine
: Lightweight, Minimalist, Security-focused
# 2. Multistage builds
# 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.
# 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
# Image
# Container
# 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
# Network
Detail (opens new window) Detail (opens new window)
# Other
Info
docker info
docker -v
Help
docker image --help
Search
docker search <keyword>