Docker Basic Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# View list of Docker images on your system
docker images

# Pull an image from Docker Hub
docker pull image_name

# Run a container from an image
docker run image_name

# View running containers
docker ps

# Kill a running container
docker kill container_id  # Use 'docker ps' to get the container ID

# Advanced useful commands
docker run --name my_container -d image_name  # Run container as daemon with a custom name
docker run --rm -it image_name /bin/bash      # Run container interactively with terminal access

Dockerfile Components

FROM

Specifies the base image to use for building your container.

ENV

Environment variables that will be available during container runtime.

RUN

Commands to execute during image build.

EXPOSE

Declares the ports that the container will listen on at runtime.

CMD

Defines the default command to run when starting the container.

COPY

Copies files from the build context into the image. Supports various formats including tar.gz and tar files.

ARG

Build-time variables used during image construction.

Docker Compose

Example with Health Check

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
version: '3'
services:

  mariadb-test:
    image: mariadb:10.6
    ports:
    # - "3306:3306" (uncomment to test DB with external client)
    volumes:
      - my-volume:/var/lib/mysql
    environment:
      MARIADB_USER: squash-user
      MARIADB_PASSWORD: password
      MARIADB_DATABASE: squash
      MARIADB_ROOT_PASSWORD: azerty

    healthcheck:
      test: ["CMD", "healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized"]
      interval: 5s
      #timeout: 30s
      #retries: 3
      #start_period: 0s

  squash-tm:
    image: squash-xzhao:1.0.2
    ports:
    - "8080:8080"
    environment:
      MY_DB_URL: "jdbc:mysql://mariadb-test:3306/squash"
      MY_DB_TYPE: mariadb
      MY_DB_USERNAME: squash-user
      MY_DB_PASSWORD: password
      
    depends_on:
      mariadb-test:
        condition: service_healthy

volumes:
  my-volume: {}

Docker Networks

Software-Defined Networking (SDN)

Docker uses SDN for container networking and communication.

Docker Cleanup Commands

Check Space Usage

1
docker system df 

Cleanup Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Remove all stopped containers
docker container prune

# Remove all unused volumes
docker volume prune

# Remove unused images
docker image prune

# Remove unused networks
docker network prune

# Remove all unused containers, volumes, images (in this order)
docker system prune

# Clean up unused build cache generated by Docker Buildx
docker buildx prune

# More aggressive cleanup of build cache
docker buildx prune --all

Accessing Running Containers

1
2
3
4
5
# Access container with bash shell
docker exec -it <container_name> /bin/bash

# Access container with basic shell (if bash is not available)
docker exec -it <container_name> /bin/sh