# Docker 🛳

## Installation :&#x20;

{% hint style="info" %}
<https://docs.docker.com/engine/install/ubuntu/>
{% endhint %}

{% code overflow="wrap" %}

```bash
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the 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 "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
```

{% endcode %}

***

#### 1. **Running a Container**

* **Basic Run Command**:

  ```bash
  docker run <options> <image>
  ```
* **Example**: Run a container in detached mode with port mapping:

  ```bash
  docker run -d -p 443:443 --name <name> <docker_image_name>
  ```

  * `-d`: Run container in detached mode.
  * `-p 443:443`: Map port 443 on the host to port 443 on the container.

#### 2. **List Running Containers**

* **Command**:

  ```bash
  docker ps
  ```

  * Lists all running containers.

#### 3. **List All Containers (Including Stopped)**

* **Command**:

  ```bash
  docker ps -a
  ```

#### 4. **Stopping a Container**

* **Command**:

  ```bash
  docker stop <container_name_or_id>
  ```

  * Stops a running container.
* **Example**:

  ```bash
  docker stop python
  ```

#### 5. **Starting a Container**

* **Command**:

  ```bash
  docker start <container_name_or_id>
  ```

#### 6. **Remove a Container**

* **Command**:

  ```bash
  docker rm <container_name_or_id>
  ```

#### 7. **Remove a Running Container (Force)**

* **Command**:

  ```bash
  docker rm -f <container_name_or_id>
  ```

#### 8. **Viewing Container Logs**

* **Command**:

  ```bash
  docker logs <container_name_or_id>
  ```

#### 9. **Run a Container with a Specific Command**

* **Example**: Running a container with a custom command:

  ```bash
  docker run -d --name mycontainer ubuntu sleep 3600
  ```

  * This runs the Ubuntu image and starts the container with the command `sleep 3600` (for 1 hour).

#### 10. **Building a Docker Image**

* **Command**:

  ```bash
  docker build -t <image_name> <path_to_dockerfile>
  ```
* **Example**:

  ```bash
  docker build -t my_image .
  ```

#### 11. **List All Docker Images**

* **Command**:

  ```bash
  docker images
  ```

#### 12. **Remove a Docker Image**

* **Command**:

  ```bash
  docker rmi <image_name_or_id>
  ```

#### 13. **Viewing Docker Info**

* **Command**:

  ```bash
  docker info
  ```

  * Displays information about your Docker installation and environment.

#### 14. **Accessing a Running Container (Interactive Mode)**

* **Command**:

  ```bash
  docker exec -it <container_name_or_id> /bin/bash
  ```

  * Starts an interactive bash session inside the container.

#### 15. **Viewing Docker Version**

* **Command**:

  ```bash
  docker --version
  ```

#### 16. **Remove Unused Docker Resources (Images, Containers, Volumes)**

* **Command**:

  ```bash
  docker system prune
  ```

#### 17. **Building and Running a Docker Compose Application**

* **Command**:

  ```bash
  docker-compose up
  ```

  * Starts the services defined in `docker-compose.yml`.

#### 18. **Stopping All Containers**

* **Command**:

  ```bash
  docker stop $(docker ps -q)
  ```

#### 19. **Accessing a Container's File System**

* **Command**:

  ```bash
  docker cp <container_name_or_id>:<container_path> <host_path>
  ```

#### 20. **Viewing Docker Container Statistics**

* **Command**:

  ```bash
  docker stats
  ```

  * Displays real-time statistics about containers' CPU and memory usage.

<figure><img src="https://3312899877-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fwq2mVR4al0Q9L9ELuENl%2Fuploads%2F46ul2zVkboGYkY69Y86n%2Fimage.png?alt=media&#x26;token=2487bf70-80a8-494d-a0b5-1fcc35a07457" alt=""><figcaption></figcaption></figure>
