Update Docker Commands Doc

This commit is contained in:
2024-09-14 16:02:56 +03:30
parent 64fdf25744
commit 180b7f82f4

View File

@@ -1,112 +1,140 @@
# Docker Commands Guide # **Docker Commands Guide**
## Docker Data Directory ## **1. Docker Data Directory**
Docker stores its files, such as images, containers, and volumes, in specific directories. These are essential for Docker's operation. Docker stores its critical files like images, containers, and volumes in specific directories. These are essential for Docker's operation.
- **`/var/lib/docker/`**: This is Docker's primary data directory where all data related to Docker is stored. - **`/var/lib/docker/`**: The main directory where Docker stores all its data.
- **`/var/lib/docker/containers/`**: Contains the configurations and files for individual Docker containers. - **`/var/lib/docker/containers/`**: Contains configuration files for individual containers.
- **`/var/lib/docker/volumes/`**: Stores the data for Docker volumes, which allow persistent data storage outside of containers. - **`/var/lib/docker/volumes/`**: Stores data for Docker volumes, enabling persistent storage beyond container lifetimes.
--- ---
## Installing Docker on Ubuntu ## **2. Installing Docker on Ubuntu**
Heres a step-by-step explanation of the commands used to install Docker on Ubuntu. Follow these steps to install Docker on Ubuntu.
### **Step 1: Update package list and install required packages**
```bash ```bash
# Update the package list and install required packages
sudo apt update && sudo apt install -y ca-certificates curl sudo apt update && sudo apt install -y ca-certificates curl
``` ```
- **`sudo apt update`**: Updates the local package list to ensure you're downloading the latest versions. - **`sudo apt update`**: Refreshes the local package list to ensure you download the latest versions.
- **`sudo apt install -y ca-certificates curl`**: Installs essential packages like `ca-certificates` and `curl` for secure communication and downloading files. - **`sudo apt install -y ca-certificates curl`**: Installs essential tools like `ca-certificates` and `curl` for secure downloads.
### **Step 2: Add Dockers GPG key**
```bash ```bash
# Create a directory for Docker's GPG key and download it
sudo install -m 0755 -d /etc/apt/keyrings sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc
``` ```
- **`install -m 0755 -d /etc/apt/keyrings`**: Creates the directory `/etc/apt/keyrings` with the correct permissions for storing GPG keys. - **`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 and stores it as `docker.asc`. - **`curl -fsSL <url> -o <file>`**: Downloads Dockers GPG key securely.
- **`chmod a+r`**: Gives read permission to all users for the GPG key file. - **`chmod a+r`**: Ensures the GPG key file is readable by all users.
### **Step 3: Add Dockers official repository**
```bash ```bash
# Add Docker's official repository to Apt sources
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 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
``` ```
- **`dpkg --print-architecture`**: Determines the system's architecture (e.g., `amd64`). - This command adds Docker's official repository to your Apt sources list.
- **`os-release`**: Fetches the OS version to ensure the correct Docker version is downloaded for your system.
### **Step 4: Install Docker**
```bash ```bash
# Update the package list and install Docker
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 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`**: Installs Docker Community Edition.
- **`docker-ce-cli`**: Installs the Docker command-line interface. - **`docker-ce-cli`**: The Docker command-line interface.
- **`containerd.io`**: Installs Containerd, a container runtime used by Docker. - **`containerd.io`**: Container runtime.
- **`docker-buildx-plugin`**: Installs Docker Buildx, an advanced image builder. - **`docker-buildx-plugin`**: Advanced build functionality.
- **`docker-compose-plugin`**: Installs Docker Compose, which helps manage multi-container applications. - **`docker-compose-plugin`**: Tool for managing multi-container applications.
--- ---
## Docker CLI Commands ## **3. Docker CLI Commands**
### Authentication
### **3.1 Authentication**
```bash ```bash
docker login docker login
``` ```
- **`docker login`**: Authenticates your Docker CLI with Docker Hub or a private registry by prompting for credentials. - **`docker login`**: Prompts for credentials to log in to Docker Hub or a private registry.
---
### Image Management
Image management commands help you interact with Docker images: downloading, saving, loading, and removing images.
### **3.2 Image Management**
```bash ```bash
docker pull <repo-addr> docker pull <repo-addr>
``` ```
- **`docker pull <repo-addr>`**: Downloads a Docker image from the specified repository (e.g., Docker Hub). - **`docker pull <repo-addr>`**: Downloads an image from the specified repository.
```bash ```bash
docker images docker images
``` ```
- **`docker images`**: Lists all Docker images stored on your system. - **`docker images`**: Lists all images on your system.
```bash ```bash
docker rmi -f <image-id> docker rmi -f <image-id>
``` ```
- **`docker rmi -f <image-id>`**: Forcefully removes a Docker image from your system. Use caution as this will permanently delete the image. - **`docker rmi -f <image-id>`**: Forcefully removes the specified image.
```bash ```bash
docker save -o <file-location-and-name> <image-name> docker save -o <file-location-and-name> <image-name>
``` ```
- **`docker save -o <file-location> <image-name>`**: Saves a Docker image as a tar archive file. Useful for transferring images. - **`docker save -o <file-location>`**: Saves an image as a `.tar` file.
```bash ```bash
docker load -i <file-location> docker load -i <file-location>
``` ```
- **`docker load -i <file-location>`**: Loads a Docker image from a tar archive file. - **`docker load -i <file-location>`**: Loads an image from a `.tar` file.
--- ---
### Container Management ### **3.3 Container Management**
These commands allow you to create, run, manage, and remove containers.
```bash ```bash
docker run <options> <img-name> docker run <options> <img-name>
``` ```
- **`docker run <img-name>`**: Runs a Docker container based on the specified image. Add options for more customization. - **`docker run <img-name>`**: Runs a container from the specified image.
```bash ```bash
docker run -it docker run -it nginx
``` ```
- **`docker run -it`**: Runs a container in interactive mode with a terminal. - **`docker run -it`**: Runs a container interactively with a terminal.
```bash ```bash
docker run -dit docker run -dit nginx
``` ```
- **`docker run -dit`**: Runs a container in detached mode with a terminal, meaning it runs in the background. - **`docker run -dit`**: Runs a container in detached mode (background).
```bash
docker run -dit --name <container-name> nginx
```
- Creates a named container.
```bash
docker run -dit --hostname=<hostname-sv> nginx
```
- Assigns a hostname to the container.
```bash
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.
```bash
docker run -dit -p <sv_port>:<container_port> nginx
```
- Maps a host port to a container port.
```bash
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.
```bash
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.
```bash
docker logs -f
```
- **`docker logs -f`**: Streams logs of a container in real-time.
```bash ```bash
docker exec -it <container-name> docker exec -it <container-name>
@@ -116,7 +144,7 @@ docker exec -it <container-name>
```bash ```bash
docker stop <container-name> docker stop <container-name>
``` ```
- **`docker stop <container-name>`**: Stops a running container gracefully. - **`docker stop <container-name>`**: Stops a running container.
```bash ```bash
docker rm <container-name> docker rm <container-name>
@@ -131,12 +159,12 @@ docker rm -f <container-name>
```bash ```bash
docker ps -aq docker ps -aq
``` ```
- **`docker ps -aq`**: Lists all container IDs, both running and stopped. - **`docker ps -aq`**: Lists all container IDs, including stopped ones.
```bash ```bash
docker ps -aq -f status=exited docker ps -aq -f status=exited
``` ```
- **`docker ps -aq -f status=exited`**: Lists the container IDs of all stopped (exited) containers. - **`docker ps -aq -f status=exited`**: Lists only the stopped container IDs.
```bash ```bash
docker container prune docker container prune
@@ -146,72 +174,70 @@ docker container prune
```bash ```bash
docker commit <container-name> <new-image-name> docker commit <container-name> <new-image-name>
``` ```
- **`docker commit <container-name> <new-image-name>`**: Creates a new Docker image from an existing container. - **`docker commit <container-name> <new-image-name>`**: Creates a new image from a running or stopped container.
```bash ```bash
docker inspect <container-name> docker inspect <container-name>
``` ```
- **`docker inspect <container-name>`**: Displays detailed information about a containers configuration and state. - **`docker inspect <container-name>`**: Displays detailed information about a container.
```bash ```bash
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name> docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>
``` ```
- **`docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>`**: Retrieves the IP address of a specific container. - **`docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>`**: Retrieves the container's IP address.
```bash ```bash
docker cp <file_on_local> <container-name>:/<location> docker cp <file_on_local> <container-name>:/<location>
``` ```
- **`docker cp <file_on_local> <container-name>:/<location>`**: Copies a file from your local system to a Docker container. - **`docker cp <file_on_local> <container-name>:/<location>`**: Copies files from local machine to the container.
```bash ```bash
docker cp <container-name>:/<location> <local-location> docker cp <container-name>:/<location> <local-location>
``` ```
- **`docker cp <container-name>:/<location> <local-location>`**: Copies a file from a Docker container to your local system. - **`docker cp <container-name>:/<location> <local-location>`**: Copies files from the container to the local machine.
```bash ```bash
docker stats docker stats
``` ```
- **`docker stats`**: Displays a live stream of resource usage statistics for all running containers. - **`docker stats`**: Displays a real-time stream of resource usage for all running containers.
```bash ```bash
docker build -t <app-name>:<app-ver> <path-to-dockerfile> docker build -t <app-name>:<app-ver> <path-to-dockerfile>
``` ```
- **`docker build -t <app-name>:<app-ver> <path-to-dockerfile>`**: Builds a Docker image from a Dockerfile located at the specified path. - **`docker build -t <app-name>:<app-ver> <path-to-dockerfile>`**: Builds a Docker image using a Dockerfile.
--- ---
### Volume Management ## **4. Volume Management**
Docker volumes are used to persist data between container restarts. These commands manage those volumes.
```bash ```bash
docker volume ls docker volume ls
``` ```
- **`docker volume ls`**: Lists all Docker volumes available on your system. - **`docker volume ls`**: Lists all Docker volumes.
```bash ```bash
docker volume create <volume-name> docker volume create <volume-name>
``` ```
- **`docker volume create <volume-name>`**: Creates a new volume with the specified name. - **`docker volume create <volume-name>`**: Creates a new volume.
```bash ```bash
docker volume inspect <volume-name> docker volume inspect <volume-name>
``` ```
- **`docker volume inspect <volume-name>`**: Displays detailed information about the specified volume. - **`docker volume inspect <volume-name>`**: Shows detailed information about a volume.
```bash ```bash
docker run -dit --name <container-name> -v <volume-name>:<container-location> <image-name> docker run -dit --name <container-name> -v <volume-name>:<container-location> <image-name>
``` ```
- **`docker run -v <volume-name>:<container-location>`**: Runs a container and mounts the specified volume at the given location inside the container. - **`docker run -v <volume-name>:<container-location>`**: Attaches a volume to a container.
--- ---
### Network Management ## **5. Network Management**
These commands help manage Docker networks, enabling containers to communicate with each other.
```bash ```bash
docker network ls docker network ls
``` ```
- **`docker network ls`**: Lists all Docker networks on your system. - **`docker network ls`**: Lists all Docker networks.
```bash ```bash
docker network create <network-name> docker network create <network-name>
@@ -221,21 +247,27 @@ docker network create <network-name>
```bash ```bash
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>
``` ```
- **`docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <network-name>`**: Creates a custom Docker network with specified settings (subnet, gateway, and driver). - **`docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <network-name>`**: Creates a custom network with specified settings.
```bash ```bash
docker run -dit --name <container-name> --network <network-name> <image-name> docker run -dit --name <container-name> --network <network-name> <image-name>
``` ```
- **`docker run --network <network-name>`**: Runs a container and connects it to the specified network. - **`docker run --network <network-name>`**: Runs a container on a specified network.
```bash ```bash
docker network connect <network-name> <container-name> docker network connect <network-name> <container-name>
``` ```
- **`docker network connect <network-name> <container
-name>`**: Connects an existing container to the specified network.
- **`docker network connect <network-name> <container-name>`**: Connects an existing container to a network.
```bash
docker network inspect <network-name>
```
- **`docker network inspect <network-name>`**: Displays detailed information about a network.
```bash ```bash
docker network disconnect <network-name> <container-name> docker network disconnect <network-name> <container-name>
``` ```
- **`docker network disconnect <network-name> <container-name>`**: Disconnects the specified container from the network. - **`docker network disconnect <network-name> <container-name>`**: Disconnects a container from a network.