Docker

Docker installation

  • Check docker
    • docker version
    • sudo docker run hello-world
  • On Linux
    • Docker client, Docker deamon and Container runs on the same Host
  • On Window or MacOSX
    • Docker deamon cannot be run directly on Non-Linxu platform natively because it user Linux specific Kernal feature
    • Need to run inside Docker-Machine
      • Install Docker Desktop

Docker Engine

  • Docker engine is the layer on which Docker runs.
  • It’s a lightweight runtime and tooling that manages containers, images, builds, and more.
  • Get installed alongwith –> Docker installation

Try docker on Web

Docker Installation in Ubuntu

# Uninstall old version
sudo apt-get remove docker docker-engine docker.io

# Install
sudo apt install docker.io

Docker Installation in Windows

Docker Installation in Mac

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 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
  • Start/stop containser service
sudo systemctl status containerd.service
sudo systemctl enable containerd.service

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

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:
    • Own/local registry
    • Public registry
      • Also called as 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 deamon –> to execute commands
  • Commands –> docker build, docker pull, docker run
  1. Registry
  • Repo for Docker Images

Containers

  • A container is a runtime instance of an image
  • Build from one or more images
    • Docker image of different micro-services together can run complete project
  • Create docker image or download from DockerHub
    • Run it, to make container

Commands-1(Basic)

  • Display Docker version and info
docker --version
docker version
docker -v

docker info
  • Pull an image from hub –> pull
docker pull <image_name>
docker pull busybox
  • Execute/Run Docker image –> run
sudo docker run hello-world
docker run busybox
docker run busybox ls
  • List images/container –> ps
# Show images
docker image ls


# Show running containers
docker ps
docker container ls

# Show running/stopped containers
docker ps --all
docker container ls -a

NOTE:

  • Running an image reteteadly will create multiple copy of the image with different container id
    • 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>
  • Above commands created 3 diffrernt copies of images
# Check
docker ps --all
  • Run without creating new copy –> start
docker start <cid-1>
docker start -a <cid-1>

Command-2

# Run and copy ID
docker run mongo
docker ps --all

# Start docker
docker start ID
docker exec -it ID

# ALT --> Not recommended
docker run -it mongo bash
  • stop/kill container
    • stop –> gives time to shutdown gracefully –> Recommended
    • kill –> shutdown immediately –> Not Recommended
docker start <containerName/ID>
docker stop <containerName/ID>
docker kill <containerName/ID>
docker container stop <containerName/ID>
# Remove dangling images
docker image prune

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

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


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

Command-3

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

# Go inside container
docker attach <containerName/ID>

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

docker rmi -f <id>
  • docker run --rm busybox sleep 1
    • Remove docker container
    • It will run and also get removed
    • Will not find this container in docker ps -a command

Reference