Docker

2.1 Docker installation

# Check docker
docker version
sudo docker run hello-world
  • On Linux
    • Docker client, Docker daemon and Container runs on the same Host
  • On Window or MacOSX
    • Docker daemon cannot be run directly on Non-Linux platform natively because it user Linux specific Kernel feature
    • Need to run inside Docker-Machine
      • Install Docker Desktop
  • Docker client –> docker commands, docker CLI
  • Docker server –> dockerd(docker daemon)

2.1.1 Docker Engine vs Docker Desktop

Docker engine is the layer on which Docker runs. It’s a lightweight runtime and tooling that manages containers, images, builds, and more.

https://docs.docker.com/engine/install/ubuntu/

Docker Desktop provides GUI to manage containers, applications, and images directly from your machine. It includes Docker Engine along with additional features.

Docker Desktop on Linux runs a Virtual Machine (VM) which creates and uses a custom docker context, desktop-linux, on startup.

This means images and containers deployed on the Linux Docker Engine (before installation) are not available in Docker Desktop for Linux.

Commercial use of Docker Desktop in larger enterprises requires a paid subscription.

https://docs.docker.com/desktop/install/linux-install/

2.1.2 Try docker on Web

2.1.3 Docker Installation in Ubuntu

Uninstall old versions

# Uninstall- unofficial packages:
sudo apt-get remove docker docker-engine docker.io

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

Installation

# Set up Docker's apt repository.

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

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 the Docker packages.
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Verify installation
sudo docker run hello-world

2.1.4 Docker Installation in Windows

2.1.5 Docker Installation in Mac

2.2 Post-installation steps for Linux

Manage Docker as a non-root user

Want to avoid typing sudo for docker command

# Create the docker group.
sudo groupadd docker

# Add your user to the docker group.
sudo usermod -aG docker $USER

# activate the changes to groups
newgrp docker

# Check
docker run hello-world
  • Later
sudo usermod -aG docker ${USER} && su - ${USER} && id -nG
sudo usermod -a -G docker ${USER} 

Configure Docker to start on boot

  • Automatically start Docker and Containerd on boot
sudo systemctl status containerd.service

# Enable
sudo systemctl enable docker.service
sudo systemctl enable containerd.service

# Disable
sudo systemctl disable docker.service
sudo systemctl disable containerd.service

Docker/Container Status

# Check docker status
sudo systemctl status docker

# Stop
sudo systemctl stop docker
sudo service docker stop

# Start
sudo service docker start

# Restart
sudo service docker restart
sudo systemctl restart docker.service

Containers vs Image

Docker Image (DI)

  • DI are read only templates used to create containers.
  • DI are created with the docker build command, either by us or by other docker users
  • DI are composed of layers of other images.
  • Stored in docker-registry(DockerHub)

Containers

A container is a runtime instance of an image build from one or more images. Run docker image, to make a docker container

Docker Hub

Docker-registry: A registry is where we store docker images, Inside a registry, images are stored in repositories.

Docker repository is a collection of different docker images with the same name, that have different tags, each tag usually represents a different version of the image.

Types of registry:

  1. Own/local registry
  2. Public registry (DockerHub)
docker login
# <username> & <password>

Client-Server Architecture

  • Docker Life cycle Docker Life cycle

It runs natively on Linux systems and is made up of:

  1. A Docker Daemon
  • Also refer as –> Docker Engine or Docker Server
  • It runs in the host computer.
  • The Docker daemon is what actually executes commands sent to the Docker Client like building, running, and distributing your containers.
  1. A Docker Client
  • Command line interface (CLI) –> act as client
  • It accepts commands from user, to communicate to daemon –> to execute commands
  • Commands –> docker build, docker pull, docker run
  1. Registry
  • Repo for Docker Images

Commands-1(Basic)

version, info, pull

Display Docker version and info

# docker version
docker --version
docker -v

# docker packages - version
docker version

docker info

Pull an image from hub –> pull

docker pull <image_name>
docker pull busybox

run, create, start/stop/kill, exec

  • Run Docker image
  • rum = pull + create + start + attach
# TEST
docker run hello-world

docker run busybox
# pull + create + start + attach + EXITED

docker run -it busybox
# pull + create + start + attach

docker run -itd busybox ls
# pull + create + start + do not attach

docker run httpd
# pull + create + start + attach

docker run -d httpd
# pull + create + start + do not attach

docker run ububtu
# pull + create + start + attach + EXITED

docker run -itd ububtu
# pull + create + start + attach

map docker port to system port

docker run -d -p 81:80 httpd
# localhost:81

docker run -d -p 82:80 httpd
# localhost:82

Run docker image in Interactive mode

docker run -it --rm busybox

# docker run – Start a docker container.
# -it – Interactive, pass stdin (-i) as a tty, pass signals (-t).
# –rm – Remove the container once it is stopped.
# busybox – A minimal docker image
  • create –> Create container from image (status-CREATED)
  • start –> Run stopped/created container (status-UP/RUNNING)
  • stop –> gives time to shutdown gracefully –> Recommended
  • kill –> shutdown immediately –> Not Recommended
  • exec –> execute command for the container
docker start/stop/kill <containerName/ID>
docker container stop <containerName/ID>

# create
docker create httpd
# start
docker start 8699be9f054d

# shell
docker exec -it 8699be9f054d /bin/sh
# bash
docker exec -it 8699be9f054d /bin/bash

# stop
docker stop 8699be9f054d

inspect, attach

  • inspect - Return low-level information on Docker objects
  • attach - Go inside container
docker inspect 8699be9f054d
docker run -it --rm busybox 
# CID - a07f1a4b716d

# detach
ctrl+p, ctrl+q

docker attach a07f1a4b716d

ls, ps

ls & ps

# List all images
docker image ls

# List all running containers
docker ps
docker container ls

# List all containers, including stopped --> *
docker ps --all
docker container ls -a

Running an image relatedly will create multiple copy of the image with different container id and this may consume more memory in the system

docker run busybox ls     # <cid-1>
docker run busybox whoami # <cid-2>
docker run busybox ls     # <cid-3>

# Check copies of containers
docker ps --all

Remove stopped containers

docker container prune        # All
docker rm <containerName/ID>  # Specific

Remove unused Docker objects

# Remove dangling images
docker image prune

# Remove images and stopped containers --> if has no container associated
docker image prune -a

# Prune everything --> prunes images, containers, and networks
docker system prune
docker system prune -a

Command-2

docker pause <containerName/ID>
docker unpause  <containerName/ID>
docker top <containerName/ID>
docker stats <containerName/ID>

docker history <imageName/ID>
docker images --help
docker images -f “dangling=false”
docker images -f “dangling=false” -q

docker rmi -f <id>

Reference