Introduction

Docker is a platform for developing, shipping, and running applications in containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code. Official Documentation

It should be the default choice for deploying applications in a reliable and repeatable way, because all the dependencies are packaged in the container, and the container can be run in any machine that has Docker installed.

Installation

Official Installation Guide

ubuntu

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
 
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
 
# Install Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Test the installation
sudo docker run hello-world

Docker Images

Containers are designed to run applications or services, requiring their images to include all necessary OS and application files. These images are typically small and stripped of non-essential components to maintain speed and efficiency. Images are composed of independent layers, with the image itself being a configuration object listing these layers and metadata. Each image and layer is identified by a unique crypto ID, which is a hash of their respective contents. Any change to the image or its layers alters these hashes, making images and layers immutable.

You can find pre-built images on the Docker Hub, a cloud-based registry service that allows you to download images and upload your own. Docker Hub is the default registry for Docker, but you can also use other registries.

Data Persistance

Docker Volumes

Volumes are directories that are outside of the container and exist as normal directories and files on the host filesystem. They are used to persist data generated by and used by Docker containers. Volumes are the preferred way to persist data generated by and used by Docker containers, because they are completely managed by Docker . Example

Bind Mounts

Bind mounts link a directory on the host machine to a directory in the container. Bind mounts are useful for sharing files between the host machine and the container, and for persisting data generated by and used by Docker containers. Example

Essential Commands

Pull image

Will download the image from the repository to the local machine

Pull image from official repo
docker image pull mongo:3.3.11
Pull image from unnofficial repo
docker image pull nigelpoulton/tu-demo:v2

This will pull the image tagged as v2 from the tu-demo repository within the namespace of ‘nigelpoulton’ Docker Hub account.

List images

Will list all images in the local machine

docker image ls
Output
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
teste        latest    0a019530d7f8   2 hours ago    94.9MB
alpine       latest    324bc02ae123   5 days ago     7.8MB
ubuntu       latest    35a88802559d   7 weeks ago    78.1MB
postgres     latest    f23dc7cd74bd   2 months ago   432MB

Remove image

docker rm ubuntu:latest

Inspect image

Will show the metadata of the image

docker inspect ubuntu:latest

Run Container

iteractively
docker run -it ubuntu:latest
detached
docker run -d teste
interactively and detached
docker run -itd teste
run command
docker run alpine:latest sleep 10
forward port
docker run -p 3000:8080 teste

port 3000 of the host machine will be forwarded to port 8080 of the container

set name
docker run --name web1 teste

Exit the container terminal without terminating

Ctrl-PQ

List Containers

Will list all containers in the local machine

Running
docker ps
All
docker ps -la
Output
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         PORTS     NAMES
96138d70f4ec   ubuntu:latest   "/bin/bash"   6 minutes ago   Up 6 minutes             hardcore_jepsen

Stop Container

Does not destroy data stored in the container

docker stop hardcore_jepsen

Resume

docker start hardcore_jepsen

Kill Container

Destroys data stored in the container

docker rm hardcore_jepsen

Manage volumes

# Create volume
docker volume create my-vol
# List volumes
docker volume ls
# remove volume
docker volume rm my-vol

Use volume

docker run -v my-vol:/path/to/container

Use bind mount

docker run -v /path/to/host:/path/to/container