Update Docker Command Doc

This commit is contained in:
2024-09-21 18:27:04 +03:30
parent 840f0b47f3
commit e877fc0b1a
2 changed files with 187 additions and 203 deletions

View File

@@ -1,50 +1,48 @@
# **Docker Commands Guide** # **Docker Commands Guide**
## **1. Docker Data Directory** ## **1. Docker Data Directories**
Docker stores its critical files like images, containers, and volumes in specific directories. These are essential for Docker's operation. Docker stores essential data, including images, containers, and volumes, in specific directories.
- **`/var/lib/docker/`**: The main directory where Docker stores all its data. - **`/var/lib/docker/`**: Main directory for Docker's data, including images, containers, and volumes.
- **`/var/lib/docker/containers/`**: Contains configuration files for individual containers. - **`/var/lib/docker/containers/`**: Stores configuration files for individual containers.
- **`/var/lib/docker/volumes/`**: Stores data for Docker volumes, enabling persistent storage beyond container lifetimes. - **`/var/lib/docker/volumes/`**: Stores data for Docker volumes, which persist beyond the containers lifecycle.
--- ---
## **2. Installing Docker on Ubuntu** ## **2. Installing Docker on Ubuntu**
Follow these steps to install Docker on Ubuntu. ### **Step 1: Update Package List and Install Dependencies**
### **Step 1: Update package list and install required packages**
```bash ```bash
sudo apt update && sudo apt install -y ca-certificates curl 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 update`**: Refreshes the package list.
- **`sudo apt install -y ca-certificates curl`**: Installs essential tools like `ca-certificates` and `curl` for secure downloads. - **`sudo apt install -y ca-certificates curl`**: Installs certificates and `curl` to securely download Docker packages.
### **Step 2: Add Dockers GPG key** ### **Step 2: Add Dockers GPG Key**
```bash ```bash
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 `/etc/apt/keyrings` directory with the appropriate permissions. - **`install -m 0755 -d /etc/apt/keyrings`**: Creates the `/etc/apt/keyrings` directory with appropriate permissions.
- **`curl -fsSL <url> -o <file>`**: Downloads Dockers GPG key securely. - **`curl -fsSL <url> -o <file>`**: Downloads Dockers GPG key.
- **`chmod a+r`**: Ensures the GPG key file is readable by all users. - **`chmod a+r`**: Grants read permissions for all users to the GPG key.
### **Step 3: Add Dockers official repository** ### **Step 3: Add Dockers Official Repository**
```bash ```bash
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
``` ```
- This command adds Docker's official repository to your Apt sources list. - Adds Dockers repository to the Apt sources list.
### **Step 4: Install Docker** ### **Step 4: Install Docker**
```bash ```bash
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`**: The Docker command-line interface. - **`docker-ce-cli`**: Docker command-line interface.
- **`containerd.io`**: Container runtime. - **`containerd.io`**: Container runtime.
- **`docker-buildx-plugin`**: Advanced build functionality. - **`docker-buildx-plugin`**: Provides advanced build functionality.
- **`docker-compose-plugin`**: Tool for managing multi-container applications. - **`docker-compose-plugin`**: Manages multi-container applications.
--- ---
@@ -54,235 +52,219 @@ sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io doc
```bash ```bash
docker login docker login
``` ```
- **`docker login`**: Prompts for credentials to log in to Docker Hub or a private registry. - **`docker login`**: Logs into Docker Hub or a private registry by prompting for credentials.
### **3.2 Image Management** ### **3.2 Image Management**
```bash - **Download an image:**
docker pull <repo-addr> ```bash
``` docker pull <repo-addr>
- **`docker pull <repo-addr>`**: Downloads an image from the specified repository. ```
- Downloads an image from a repository.
```bash - **List images:**
docker images ```bash
``` docker images
- **`docker images`**: Lists all images on your system. ```
- Displays all available images.
```bash - **Remove an image:**
docker rmi -f <image-id> ```bash
``` docker rmi -f <image-id>
- **`docker rmi -f <image-id>`**: Forcefully removes the specified image. ```
- Forcefully removes a specific image.
```bash - **Save an image as a `.tar` file:**
docker save -o <file-location-and-name> <image-name> ```bash
``` docker save -o <file-location-and-name> <image-name>
- **`docker save -o <file-location>`**: Saves an image as a `.tar` file. ```
```bash - **Load an image from a `.tar` file:**
docker load -i <file-location> ```bash
``` docker load -i <file-location>
- **`docker load -i <file-location>`**: Loads an image from a `.tar` file. ```
---
### **3.3 Container Management** ### **3.3 Container Management**
- **Run a container:**
```bash
docker run <options> <image-name>
```
```bash - **Run an interactive container with a terminal:**
docker run <options> <img-name> ```bash
``` docker run -it <image-name>
- **`docker run <img-name>`**: Runs a container from the specified image. ```
```bash - **Run a container in detached mode:**
docker run -it nginx ```bash
``` docker run -dit <image-name>
- **`docker run -it`**: Runs a container interactively with a terminal. ```
```bash - **Set a container to always restart:**
docker run -dit nginx ```bash
``` docker run -dit --restart=always <image-name>
- **`docker run -dit`**: Runs a container in detached mode (background). ```
```bash - **Name a container:**
docker run -dit --restart=always nginx ```bash
``` docker run -dit --name <container-name> <image-name>
```
```bash - **Assign a hostname:**
docker run -dit --restart=always nginx ```bash
``` docker run -dit --hostname=<hostname> <image-name>
```
```bash - **Set environment variables:**
docker run -dit --name <container-name> nginx ```bash
``` docker run -dit -e var1=data --name <container-name> --hostname=<hostname> <image-name>
- Creates a named container. ```
```bash - **Map host and container ports:**
docker run -dit --hostname=<hostname-sv> nginx ```bash
``` docker run -dit -p <host-port>:<container-port> <image-name>
- Assigns a hostname to the container. ```
```bash - **Run a container with memory and CPU limits:**
docker run -dit -e var1=data --name <container-name> --hostname=<hostname-sv> nginx ```bash
``` docker run -dit --name nginx --memory-reservation 150m --memory 500m nginx
- Sets environment variables and runs a container with a custom name and hostname. ```
- Limits memory reservation to 150MB and usage to a maximum of 500MB.
```bash ```bash
docker run -dit -p <sv_port>:<container_port> nginx docker run -dit --name nginx --cpus 2 --cpu-shares 100 nginx
``` ```
- Maps a host port to a container port. - Limits the container to 2 CPUs.
```bash - **Stream container logs in real-time:**
docker run -dit -p <sv_port_from>-<sv_port_to>:<container_port_from>-<container_port_to> nginx ```bash
``` docker logs -f <container-name>
- Maps a range of ports between host and container. ```
```bash - **Access a containers terminal:**
docker run -dit -p 127.0.0.1:<sv_port_from>-<sv_port_to>:<container_port_from>-<container_port_to> nginx ```bash
``` docker exec -it <container-name> /bin/bash
- Binds container ports to specific IP addresses on the host. ```
```bash - **Stop a container:**
docker logs -f ```bash
``` docker stop <container-name>
- **`docker logs -f`**: Streams logs of a container in real-time. ```
- **Remove a container:**
```bash
docker rm <container-name>
```
```bash - **Forcefully remove a running container:**
docker events ```bash
``` docker rm -f <container-name>
```
- **List all container IDs (including stopped):**
```bash
docker ps -aq
```
```bash - **Prune stopped containers:**
docker exec -it <container-name> ```bash
``` docker container prune
- **`docker exec -it <container-name>`**: Opens an interactive terminal inside a running container. ```
```bash - **Commit a container to an image:**
docker stop <container-name> ```bash
``` docker commit <container-name> <new-image-name>
- **`docker stop <container-name>`**: Stops a running container. ```
```bash - **Inspect container details:**
docker rm <container-name> ```bash
``` docker inspect <container-name>
- **`docker rm <container-name>`**: Removes a stopped container. ```
```bash - **Copy files between host and container:**
docker rm -f <container-name> ```bash
``` docker cp <file-on-local> <container-name>:/<container-path>
- **`docker rm -f <container-name>`**: Forcefully stops and removes a running container. ```
```bash ```bash
docker ps -aq docker cp <container-name>:/<container-path> <local-path>
``` ```
- **`docker ps -aq`**: Lists all container IDs, including stopped ones.
```bash - **View real-time container resource usage:**
docker ps -aq -f status=exited ```bash
``` docker stats
- **`docker ps -aq -f status=exited`**: Lists only the stopped container IDs. ```
```bash - **Build an image from a Dockerfile:**
docker container prune ```bash
``` docker build -t <app-name>:<app-version> <path-to-dockerfile>
- **`docker container prune`**: Removes all stopped containers. ```
```bash
docker commit <container-name> <new-image-name>
```
- **`docker commit <container-name> <new-image-name>`**: Creates a new image from a running or stopped container.
```bash
docker inspect <container-name>
```
- **`docker inspect <container-name>`**: Displays detailed information about a container.
```bash
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>
```
- **`docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>`**: Retrieves the container's IP address.
```bash
docker cp <file_on_local> <container-name>:/<location>
```
`-A` : use for archive mode (keep permmision and )
- **`docker cp <file_on_local> <container-name>:/<location>`**: Copies files from local machine to the container.
```bash
docker cp <container-name>:/<location> <local-location>
```
- **`docker cp <container-name>:/<location> <local-location>`**: Copies files from the container to the local machine.
```bash
docker stats
```
- **`docker stats`**: Displays a real-time stream of resource usage for all running containers.
```bash
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** ## **4. Volume Management**
Volumes store data that persists even when a container is deleted.
```bash - **List all volumes:**
docker volume ls ```bash
``` docker volume ls
- **`docker volume ls`**: Lists all Docker volumes. ```
```bash - **Create a volume:**
docker volume create <volume-name> ```bash
``` docker volume create <volume-name>
- **`docker volume create <volume-name>`**: Creates a new volume. ```
```bash - **Inspect a volume:**
docker volume inspect <volume-name> ```bash
``` docker volume inspect <volume-name>
- **`docker volume inspect <volume-name>`**: Shows detailed information about a volume. ```
```bash - **Mount a volume to a container:**
docker run -dit --name <container-name> -v <volume-name>:<container-location> <image-name> ```bash
``` docker run -dit --name <container-name> -v <volume-name>:<container-path> <image-name>
- **`docker run -v <volume-name>:<container-location>`**: Attaches a volume to a container. ```
- **Mount a file with read-only access:**
```bash
docker run -dit --name nginx -v /etc/resolv.conf:/etc/resolv.conf:ro nginx
```
- **Mount temporary filesystem in memory:**
```bash
docker run -dit --name nginx --tmpfs /opt:100M nginx
```
--- ---
## **5. Network Management** ## **5. Network Management**
Docker networks allow communication between containers.
```bash - **List all networks:**
docker network ls ```bash
``` docker network ls
- **`docker network ls`**: Lists all Docker networks. ```
```bash - **Create a network:**
docker network create <network-name> ```bash
``` docker network create <network-name>
- **`docker network create <network-name>`**: Creates a new Docker network. ```
```bash - **Create a custom network with subnet and gateway:**
docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <network-name> ```bash
``` docker network create --subnet <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. ```
```bash - **Run a container on a specific network:**
docker run -dit --name <container-name> --network <network-name> <image-name> ```bash
``` docker run -dit --name <container-name> --network <network-name> <image-name>
- **`docker run --network <network-name>`**: Runs a container on a specified network. ```
```bash - **Connect a running container to a network:**
docker network connect <network-name> <container-name> ```bash
``` docker network connect <network-name> <container-name>
```
- **`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
docker network disconnect <network-name> <container-name>
```
- **`docker network disconnect <network-name> <container-name>`**: Disconnects a container from a network.
- **Disconnect a container from a network:**
```bash
docker network disconnect <network-name> <container-name>
```

View File

@@ -0,0 +1,2 @@
# **Docker Compose Guide**