Files
my-docs/Containerization & Orchestration/Docker/2-Commands.md

7.9 KiB
Executable File
Raw Blame History

Docker Commands Guide

1. Docker Data Directory

Docker stores its critical files like images, containers, and volumes in specific directories. These are essential for Docker's operation.

  • /var/lib/docker/: The main directory where Docker stores all its data.
  • /var/lib/docker/containers/: Contains configuration files for individual containers.
  • /var/lib/docker/volumes/: Stores data for Docker volumes, enabling persistent storage beyond container lifetimes.

2. Installing Docker on Ubuntu

Follow these steps to install Docker on Ubuntu.

Step 1: Update package list and install required packages

sudo apt update && sudo apt install -y ca-certificates curl
  • sudo apt update: Refreshes the local package list to ensure you download the latest versions.
  • sudo apt install -y ca-certificates curl: Installs essential tools like ca-certificates and curl for secure downloads.

Step 2: Add Dockers GPG key

sudo install -m 0755 -d /etc/apt/keyrings 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc 
sudo chmod a+r /etc/apt/keyrings/docker.asc
  • install -m 0755 -d /etc/apt/keyrings: Creates the /etc/apt/keyrings directory with the appropriate permissions.
  • curl -fsSL <url> -o <file>: Downloads Dockers GPG key securely.
  • chmod a+r: Ensures the GPG key file is readable by all users.

Step 3: Add Dockers official repository

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • This command adds Docker's official repository to your Apt sources list.

Step 4: Install Docker

sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • docker-ce: Installs Docker Community Edition.
  • docker-ce-cli: The Docker command-line interface.
  • containerd.io: Container runtime.
  • docker-buildx-plugin: Advanced build functionality.
  • docker-compose-plugin: Tool for managing multi-container applications.

3. Docker CLI Commands

3.1 Authentication

docker login
  • docker login: Prompts for credentials to log in to Docker Hub or a private registry.

3.2 Image Management

docker pull <repo-addr>
  • docker pull <repo-addr>: Downloads an image from the specified repository.
docker images
  • docker images: Lists all images on your system.
docker rmi -f <image-id>
  • docker rmi -f <image-id>: Forcefully removes the specified image.
docker save -o <file-location-and-name> <image-name>
  • docker save -o <file-location>: Saves an image as a .tar file.
docker load -i <file-location>
  • docker load -i <file-location>: Loads an image from a .tar file.

3.3 Container Management

docker run <options> <img-name>
  • docker run <img-name>: Runs a container from the specified image.
docker run -it nginx
  • docker run -it: Runs a container interactively with a terminal.
docker run -dit nginx
  • docker run -dit: Runs a container in detached mode (background).
docker run -dit --name <container-name> nginx
  • Creates a named container.
docker run -dit --hostname=<hostname-sv> nginx
  • Assigns a hostname to the container.
docker run -dit -e var1=data --name <container-name> --hostname=<hostname-sv> nginx
  • Sets environment variables and runs a container with a custom name and hostname.
docker run -dit -p <sv_port>:<container_port>  nginx
  • Maps a host port to a container port.
docker run -dit -p <sv_port_from>-<sv_port_to>:<container_port_from>-<container_port_to>  nginx
  • Maps a range of ports between host and container.
docker run -dit -p 127.0.0.1:<sv_port_from>-<sv_port_to>:<container_port_from>-<container_port_to>  nginx
  • Binds container ports to specific IP addresses on the host.
docker logs -f
  • docker logs -f: Streams logs of a container in real-time.
docker exec -it <container-name>
  • docker exec -it <container-name>: Opens an interactive terminal inside a running container.
docker stop <container-name>
  • docker stop <container-name>: Stops a running container.
docker rm <container-name>
  • docker rm <container-name>: Removes a stopped container.
docker rm -f <container-name>
  • docker rm -f <container-name>: Forcefully stops and removes a running container.
docker ps -aq
  • docker ps -aq: Lists all container IDs, including stopped ones.
docker ps -aq -f status=exited
  • docker ps -aq -f status=exited: Lists only the stopped container IDs.
docker container prune
  • docker container prune: Removes all stopped containers.
docker commit <container-name> <new-image-name>
  • docker commit <container-name> <new-image-name>: Creates a new image from a running or stopped container.
docker inspect <container-name>
  • docker inspect <container-name>: Displays detailed information about a container.
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>
  • docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>: Retrieves the container's IP address.
docker cp <file_on_local> <container-name>:/<location>
  • docker cp <file_on_local> <container-name>:/<location>: Copies files from local machine to the container.
docker cp <container-name>:/<location> <local-location>
  • docker cp <container-name>:/<location> <local-location>: Copies files from the container to the local machine.
docker stats
  • docker stats: Displays a real-time stream of resource usage for all running containers.
docker build -t <app-name>:<app-ver> <path-to-dockerfile>
  • docker build -t <app-name>:<app-ver> <path-to-dockerfile>: Builds a Docker image using a Dockerfile.

4. Volume Management

docker volume ls
  • docker volume ls: Lists all Docker volumes.
docker volume create <volume-name>
  • docker volume create <volume-name>: Creates a new volume.
docker volume inspect <volume-name>
  • docker volume inspect <volume-name>: Shows detailed information about a volume.
docker run -dit --name <container-name> -v <volume-name>:<container-location> <image-name>
  • docker run -v <volume-name>:<container-location>: Attaches a volume to a container.

5. Network Management

docker network ls
  • docker network ls: Lists all Docker networks.
docker network create <network-name>
  • docker network create <network-name>: Creates a new Docker network.
docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <network-name>
  • docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <network-name>: Creates a custom network with specified settings.
docker run -dit --name <container-name> --network <network-name> <image-name>
  • docker run --network <network-name>: Runs a container on a specified network.
docker network connect <network-name> <container-name>
  • docker network connect <network-name> <container-name>: Connects an existing container to a network.
docker network inspect <network-name>
  • docker network inspect <network-name>: Displays detailed information about a network.
docker network disconnect <network-name> <container-name>
  • docker network disconnect <network-name> <container-name>: Disconnects a container from a network.