> For the complete documentation index, see [llms.txt](https://docs.hackerspot.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hackerspot.net/cheatsheets/cloud-tools/docker.md).

# Docker

## Docker CLI

### Basic Commands

* **docker --version**

  * Check Docker version.

  ```sh
  docker --version
  ```
* **docker info**

  * Display system-wide information.

  ```sh
  docker info
  ```
* **docker help**

  * Get help on Docker commands.

  ```sh
  docker help
  ```

### Images

* **docker images**

  * List all Docker images.

  ```sh
  docker images
  ```
* **docker pull \[image]**

  * Pull an image from a registry.

  ```sh
  docker pull ubuntu:latest
  ```
* **docker rmi \[image\_id]**

  * Remove a Docker image.

  ```sh
  docker rmi ubuntu:latest
  ```

### Containers

* **docker ps**

  * List running containers.

  ```sh
  docker ps
  ```
* **docker ps -a**

  * List all containers (running and stopped).

  ```sh
  docker ps -a
  ```
* **docker run \[options] \[image]**

  * Run a command in a new container.

  ```sh
  docker run -it ubuntu:latest /bin/bash
  ```
* **docker stop \[container\_id]**

  * Stop a running container.

  ```sh
  docker stop [container_id]
  ```
* **docker start \[container\_id]**

  * Start a stopped container.

  ```sh
  docker start [container_id]
  ```
* **docker restart \[container\_id]**

  * Restart a container.

  ```sh
  docker restart [container_id]
  ```
* **docker rm \[container\_id]**

  * Remove a stopped container.

  ```sh
  docker rm [container_id]
  ```

### Networks

* **docker network ls**

  * List all networks.

  ```sh
  docker network ls
  ```
* **docker network create \[network\_name]**

  * Create a new network.

  ```sh
  docker network create my_network
  ```
* **docker network rm \[network\_name]**

  * Remove a network.

  ```sh
  docker network rm my_network
  ```

### Volumes

* **docker volume ls**

  * List all volumes.

  ```sh
  docker volume ls
  ```
* **docker volume create \[volume\_name]**

  * Create a new volume.

  ```sh
  docker volume create my_volume
  ```
* **docker volume rm \[volume\_name]**

  * Remove a volume.

  ```sh
  docker volume rm my_volume
  ```

### Docker Compose

* **docker-compose up**

  * Create and start containers.

  ```sh
  docker-compose up
  ```
* **docker-compose down**

  * Stop and remove containers, networks, images, and volumes.

  ```sh
  docker-compose down
  ```
* **docker-compose build**

  * Build or rebuild services.

  ```sh
  docker-compose build
  ```

### Inspect and Logs

* **docker inspect \[container\_id]**

  * Return low-level information on Docker objects.

  ```sh
  docker inspect [container_id]
  ```
* **docker logs \[container\_id]**

  * Fetch the logs of a container.

  ```sh
  docker logs [container_id]
  ```

### Clean Up

* **docker system prune**

  * Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

  ```sh
  docker system prune
  ```
* **docker container prune**

  * Remove all stopped containers.

  ```sh
  docker container prune
  ```
* **docker volume prune**

  * Remove all unused volumes.

  ```sh
  docker volume prune
  ```
* **docker image prune**

  * Remove unused images.

  ```sh
  docker image prune
  ```

This cheat sheet covers the basic and most commonly used Docker commands to get you started. Feel free to ask if you need more details or advanced commands!

## Creating a Dockerfile

A Dockerfile is a text file that contains a series of instructions on how to build a Docker image. Here's a basic overview and examples of common Dockerfile instructions:

### Basic Structure of a Dockerfile

1. **FROM**

   * Specifies the base image.

   ```Dockerfile
   FROM ubuntu:latest
   ```
2. **MAINTAINER**

   * Sets the author field of the generated images.

   ```Dockerfile
   MAINTAINER Your Name <your.email@example.com>
   ```
3. **RUN**

   * Executes commands in a new layer on top of the current image.

   ```Dockerfile
   RUN apt-get update && apt-get install -y nginx
   ```
4. **COPY**

   * Copies files from the host machine to the Docker image.

   ```Dockerfile
   COPY ./localfile /path/in/container
   ```
5. **ADD**

   * Copies files/directories from the host machine to the Docker image, and also supports extracting tar files.

   ```Dockerfile
   ADD ./localfile.tar /path/in/container
   ```
6. **CMD**

   * Specifies the command to run within the container.

   ```Dockerfile
   CMD ["nginx", "-g", "daemon off;"]
   ```
7. **ENTRYPOINT**

   * Sets a default application to be used every time a container is created with the image.

   ```Dockerfile
   ENTRYPOINT ["nginx", "-g", "daemon off;"]
   ```
8. **EXPOSE**

   * Informs Docker that the container listens on the specified network ports at runtime.

   ```Dockerfile
   EXPOSE 80
   ```
9. **ENV**

   * Sets environment variables.

   ```Dockerfile
   ENV ENVIRONMENT production
   ```
10. **VOLUME**

    * Creates a mount point with the specified path and marks it as holding externally mounted volumes from the native host or other containers.

    ```Dockerfile
    VOLUME /data
    ```

### Example Dockerfile

Here's an example Dockerfile for a simple web server using Nginx:

```Dockerfile
# Use the official Nginx image from the Docker Hub
FROM nginx:latest

# Set the maintainer label
MAINTAINER Your Name <your.email@example.com>

# Copy custom configuration file from the host to the container
COPY nginx.conf /etc/nginx/nginx.conf

# Copy the content of the website to the container
COPY ./html /usr/share/nginx/html

# Expose port 80 to the host
EXPOSE 80

# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
```

#### Building and Running the Docker Image

1. **Build the Docker Image**

   * Use the `docker build` command to create an image from the Dockerfile.

   ```sh
   docker build -t my-nginx-image .
   ```
2. **Run the Docker Container**

   * Use the `docker run` command to start a container from the image.

   ```sh
   docker run -d -p 80:80 my-nginx-image
   ```

#### Best Practices

* **Keep Dockerfile Instructions Ordered**: Use a logical order such as `FROM`, `MAINTAINER`, `RUN`, `COPY`, `CMD`.
* **Use .dockerignore**: Create a `.dockerignore` file to exclude files and directories from the build context to reduce the size of the image.
* **Minimize Layers**: Combine multiple `RUN` commands to reduce the number of layers.
* **Leverage Caching**: Order the instructions to leverage Docker’s build cache.

This should give you a good starting point for creating your own Dockerfiles! If you have any specific questions or need further details, feel free to ask.
