Docker Basics

Jun 10, 2023

#docker #tools


Docker is a software platform for delivering applications in container run by docker engine.

Installation

Follow the official documentation for installing the docker engine in your system.

Basic Commands

Pull an image from Docker Hub

docker pull <image>  # <image> = alpine

List docker images

docker images ls

Run docker image in a detach container

docker container run -d -t --name <name> <container_name>

List the running containers

docker ps  # docker ps -a  <-- for all containers

Get the shell

docer exec -it <name> bash
exit

Stop the container

docker stop <name>

Start the container

docker start <name>

start an existing container

docker restart <container_name>

Delete a container

docker rm <name>

Remove an image

docker rmi <name>

Management

Docker has many built-in tools for managing the docker container

Dockerfile

Dockerfile is text document for assembling a docker image for the productions

FROM debian:bookworm

RUN \
  apt-get update && \
  apt-get -y install apt-utils zsh zplug \
  htop \
  python3-pip python3-virtualenv \
  curl

RUN useradd -ms /bin/zsh sanatan
RUN chsh -s /bin/zsh
USER sanatan
WORKDIR /home/sanatan
RUN python3 -m virtualenv .venv
RUN source .venv/bin/activate
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
RUN python app.py
FILE requirements.txt
numpy

Build docker image from Dockerfile

docker build --tag sanatan-docker .

Docker volume

docker volume ls
docker volume create `vol_name`

Docker network

Docker supports several types of network adapter: bridge, host, none, macvlan, ipvlan etc.

docker network ls

Docker compose

Docker compose is a tool for managing and defining multicontainer application in a single compose file.

Create docker-compose.yml file (example from https://linuxserver.io )

---
version: "2.1"
services:
  calibre-web:
    image: lscr.io/linuxserver/calibre-web:latest
    container_name: calibre-web
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Etc/UTC
      - DOCKER_MODS=linuxserver/mods:universal-calibre #optional
      - OAUTHLIB_RELAX_TOKEN_SCOPE=1 #optional
    volumes:
      - /path/to/data:/config
      - /path/to/calibre/library:/books
    ports:
      - 8083:8083
    restart: unless-stopped

then run in detach mode

docker-compose up -d

to get the log

docker-compose logs -f

Misc

To remove all the containers

docker rm $(docker ps -aq)

To remove all the images

docker rmi $(docker image ls)