# Topic covered
* Docker Image
* Containers
* Docker Hub
* Docker Client-Server Architecture
* Docker Commands-1(Basic)

Docker Image (DI)

A Docker image is like a blueprint/template for your application.

It’s a read-only package that contains everything your app needs:

  • Source code or binaries
  • Dependencies & libraries
  • Runtime (Python, Node.js, Java, etc.)
  • Environment variables, config, tools
  • You can think of it like an installable app snapshot.
  • Stored in docker-registry(DockerHub)

Containers

A container is a runtime instance of an image build from one or more images.

It adds a thin writable layer on top of the image, so it can store changes (logs, temp files, configs, etc.) while running.

  • 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)

Run Docker image

  • run = pull + create + start + attach
  • docker run <image_name>:<tag>
  • docker container run <image_name>:<tag>
# TEST
docker run hello-world

busybox - a lightweight Linux

# pull + create + start + attach + EXITED

docker run --name bbx1 busybox

httpd - Apache HTTP Server, one of the most widely used web servers

  • By default attached
docker run --name http1 httpd
# pull + create + start + attach

docker run -d --name httpd2 httpd
# pull + create + start + detach

ubuntu

  • By default detached
docker run --name ub1 ubuntu
# pull + create + start + attach + EXITED

docker run -itd --name ub2 ubuntu
# pull + create + start + attach + detach

start/stop/kill

  • 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 start/stop/kill <containerName/ID>

docker stop httpd2
docker start httpd2

# Stop all running containers
docker stop $(docker ps -aq)

exec, -it, –rm

  • -it –> Interactive, pass stdin (-i) as a tty, pass signals (-t)
  • -it can be used directly only for - minimal docker image
  • NOTE: Always use -it with exec
    • Exception for minimal docker image like: busybox, hello-world
docker run -it busybox
# pull + create + start + attach
exit

docker run -itd busybox ls

–rm –> Remove the container once it is stopped.

docker run -it --rm busybox

exec: This command is used to run a new command inside an already running container. Unlike docker run (which creates a new container), docker exec operates in an existing container.

# run docker in background
docker run -d --name bbx1 busybox watch "date >> /var/log/date.log"

docker ps

docker exec -it ub2 ls
docker exec -it ub2 /bin/sh
exit

docker exec -it ub2 sh
exit

docker exec -it ub2 bash
exit

✅ So, docker exec -it = “open an interactive terminal session inside a running container”.

Get inside docker image

attach - it’s like “plugging your terminal back into the container” to see its live output and interact with it.

docker attach ub2

👉 exit - here will stop the container 👉 Ctrl + P then Ctrl + Q → this detaches you (container keeps running in background)

inspect, Map Port

  • inspect - Return low-level information on Docker objects
docker inspect busybox

docker inspect ub2

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 unused Docker objects

# 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

# Remove image from local
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