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 platformnatively because it user Linux specific Kernel feature - Need to
run inside Docker-Machine- Install Docker Desktop
- Docker daemon
- 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
- Play with docker without Installation
- A simple, interactive and fun playground to learn Docker
2.1.3 Docker Installation in Ubuntu
# 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
# 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
Install Docker Toolbox
- Deprecated –> Now
Docker Desktopis used - https://github.com/docker-archive/toolbox
- https://www.geeksforgeeks.org/docker-toolbox/
If you install Docker Toolbox on a Windows machine, the installer automatically installs Oracle Virtualbox to run the Docker virtual machine.
The Docker Toolbox installs everything you need to get started with Docker on Mac OS X and Windows. It includes the Docker client, Compose, Machine, Kitematic, and VirtualBox.
Install Docker Desktop
- Docker Desktop WSL 2 backend on Windows: https://docs.docker.com/desktop/wsl/
- Install Docker Desktop on Windows: https://docs.docker.com/desktop/windows/install/
Windows Subsystem for Linux (WSL) 2 is a full Linux kernel built by Microsoft, which lets Linux distributions run without managing virtual machines. With Docker Desktop running on WSL 2, users can leverage Linux workspaces and avoid maintaining both Linux and Windows build scripts. In addition, WSL 2 provides improvements to file system sharing and boot time.
# Check WSL version
wsl --status
# Change WSL version to WSL2
wsl --set-default-version 2
- Install Docker Desktop
# Check installation/version
docker -v
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
2.3 Containers vs Image
Docker Image (DI)
- DI are
read only templatesused 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:
- Own/local registry
- Public registry (DockerHub)
docker login
# <username> & <password>
Client-Server Architecture
- Docker Life cycle

It runs natively on Linux systems and is made up of:
- A Docker Daemon
- Also refer as –>
Docker EngineorDocker 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.
- 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
- Registry
- Repo for Docker Images
Commands-1(Basic)
version, info
Display Docker version and info
# docker version
docker --version
docker -v
# docker packages - version
docker version
docker info
run, start/stop/kill
- Run Docker image
- run = pull + create + start + attach
docker run <image_name>:<tag>
# TEST
docker run hello-world
# busybox --> is a lightweight Linux
docker run busybox
# pull + create + start + attach + EXITED
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
- start –> Run stopped/created container (status-UP/RUNNING)
- stop –> gives time to
shutdown gracefully–> Recommended - kill –>
shutdown immediately–> Not Recommended
docker start/stop/kill <containerName/ID>
docker container stop <containerName/ID>
exec, -it, –rm
exec –> Execute command for the container. To use the docker exec command, you will need a running Docker container.
-it –-> Interactive, pass stdin (-i) as a tty, pass signals (-t)
–rm –-> Remove the container once it is stopped.
- NOTE:
Always use -it with exec- Exception for minimal docker image like: busybox, hello-world
- -it can be used directly only for - minimal docker image
docker run -it busybox
# pull + create + start + attach
docker run -itd busybox ls
docker run -it --rm busybox
- For Other
# run docker in background
docker run -d busybox watch "date >> /var/log/date.log"
docker ps
docker exec -it 5dce543656c9 ls
docker exec -it 5dce543656c9 /bin/sh
docker exec -it 5dce543656c9 sh
exit
# stop
docker stop 8699be9f054d
inspect, attach, Map Port
- 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
Map docker port to system port
docker run -d -p 81:80 httpd
# localhost:81
docker run -d -p 82:80 httpd
# localhost:82
ls, ps
- List all images
docker image ls
docker images
- 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
# All
docker container prune
# Specific
docker rm <containerName/ID>
# Remove dangling images
docker image prune
# This will remove all images without at least one container associated to them
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>
PUSH, PULL docker image
Push the image tp docker hub
# Login to docker hub
docker login
# Change name of the image, if not compatible to syntax
docker image tag <old_name:Version> <user_name>/<old_name>:<version>
docker image tag python-3.6.15 amritkr6/python-3.6.15:v1
# Push
docker push amritkr6/python-3.6.15:v1
Pull an image from hub –> pull
docker pull <image_name>
docker pull <image_name>:<tag>
docker pull hello-world