Docker Commands

Basic Docker Commands

  • docker pull IMAGE_NAME:TAG: Pull an image from a registry.

  • docker run [OPTIONS] IMAGE [COMMAND] [ARG…​]: Run a container from an image.

  • docker ps [OPTIONS]: List running containers.

  • docker ps -a: List all containers (including stopped ones).

  • docker stop CONTAINER_ID: Stop a running container.

  • docker start CONTAINER_ID: Start a stopped container.

  • docker restart CONTAINER_ID: Restart a container.

  • docker rm CONTAINER_ID: Remove a stopped container.

  • docker exec -it CONTAINER_ID COMMAND: Run a command inside a running container.

  • docker logs CONTAINER_ID: Display the logs of a container.

  • docker images [OPTIONS]: List available images.

  • docker rmi IMAGE_ID: Remove an image.

Managing Containers

  • docker-compose up: Start services defined in a Compose file.

  • docker-compose down: Stop and remove containers, networks, and volumes defined in a Compose file.

  • docker-compose ps: List containers in the current Compose project.

Networking

  • docker network ls: List networks.

  • docker network create NETWORK_NAME: Create a new network.

  • docker network connect NETWORK_NAME CONTAINER_NAME: Connect a container to a network.

  • docker network disconnect NETWORK_NAME CONTAINER_NAME: Disconnect a container from a network.

Volume Management

  • docker volume ls: List volumes.

  • docker volume create VOLUME_NAME: Create a new volume.

  • docker volume inspect VOLUME_NAME: Display detailed information about a volume.

Image and Container Cleanup

  • docker system df: Show disk usage by Docker.

  • docker system prune: Remove unused data (containers, networks, images, volumes).

  • docker image prune: Remove unused images.

  • docker container prune: Remove stopped containers.

  • docker volume prune: Remove unused volumes.

  • docker network prune: Remove unused networks.

Building Custom Images

  • docker build [OPTIONS] PATH: Build an image from a Dockerfile.

Docker Compose

  • docker-compose up: Start services defined in a Compose file.

  • docker-compose down: Stop and remove containers, networks, and volumes defined in a Compose file.

  • docker-compose ps: List containers in the current Compose project.

  • docker-compose exec SERVICE_NAME COMMAND: Run a command in a specific service container.

23-08 Docker - Practical Guide for Developers and Devops Teams

Commands - Running MariaDB

3.5 Data Storage in Volumes

docker run -d --name mariadb-test -e MYSQL_ROOT_PASSWORD=secret mariadb

docker ps

docker stop mariadb-test

docker logs mariadb-test

3.5.2 Analyzing Volumes

docker inspect mariadb-test

docker inspect -f '{{ .Mounts }}' mariadb-test

docker volume inspect d19f5b86...

docker system df -v

3.5.3 Calling the MariaDB Client

docker exec -it mariadb-test mysql -u root -p

OCI runtime exec failed: exec failed: unable to start container process: exec: "mysql": executable file not found in $PATH: unknown

For newer MariaDB container versions, the mysql executable does not exist any more. Replace mysql with mariadb to fix the issue

docker exec -it mariadb-test mariadb -u root -p

3.5.4 A Look Inside the Container

docker exec -it mariadb-test /bin/bash

mariadb --version