# Quick Reference

## <mark style="color:red;">**Setting Up Docker**</mark>

### <mark style="color:yellow;">Install Docker</mark>

{% hint style="info" %}
Refer to the official Docker documentation for OS-specific installation instructions.
{% endhint %}

[Install docker on windows](https://docs.docker.com/desktop/install/windows-install/)

[Install docker on linux](https://docs.docker.com/engine/install/ubuntu/)

#### <mark style="color:purple;">Verify Docker installation:</mark>

```bash
docker --version
```

***

## <mark style="color:red;">**Docker Images**</mark>

### <mark style="color:yellow;">**Building an Image**</mark>

#### <mark style="color:purple;">To create a Docker image from a Dockerfile:</mark>

```bash
docker build -t your-image-name:tag .
```

* `your-image-name`: Name of the image.
* `tag`: Version tag (commonly `latest` for the most recent version).

### <mark style="color:yellow;">**Listing Images**</mark>

#### <mark style="color:purple;">To view all Docker images on your system:</mark>

```bash
docker images
```

### <mark style="color:yellow;">**Removing an Image**</mark>

#### <mark style="color:purple;">To remove a specific Docker image:</mark>

```bash
docker rmi your-image-name:tag
```

### <mark style="color:yellow;">**Removing All Images**</mark>

#### <mark style="color:purple;">To remove all Docker images:</mark>

```bash
docker rmi $(docker images -q)
```

## <mark style="color:red;">**Docker Containers**</mark>

### <mark style="color:yellow;">**Running a Container**</mark>

#### <mark style="color:purple;">To run a Docker container from an image:</mark>

```bash
docker run -p host-port:container-port your-image-name:tag
```

### <mark style="color:yellow;">**Listing Running Containers**</mark>

#### <mark style="color:purple;">To view all running containers:</mark>

```bash
docker ps
```

### <mark style="color:yellow;">**Listing All Containers**</mark>

#### <mark style="color:purple;">To see all containers, including stopped ones:</mark>

```bash
docker ps -a
```

### <mark style="color:yellow;">**Stopping a Container**</mark>

#### <mark style="color:purple;">To stop a specific running container:</mark>

```bash
docker stop container-id-or-name
```

### <mark style="color:yellow;">**Removing a Container**</mark>

#### <mark style="color:purple;">To remove a stopped container:</mark>

```bash
docker rm container-id-or-name
```

### <mark style="color:yellow;">**Removing All Containers**</mark>

#### <mark style="color:purple;">To remove all containers:</mark>

```bash
docker rm $(docker ps -aq)
```

## <mark style="color:red;">**Debugging and Interactivity**</mark>

### <mark style="color:yellow;">**Running a Container Interactively**</mark>

#### <mark style="color:purple;">To run a container in interactive mode (often used with a shell):</mark>

```bash
docker run -it your-image-name:tag /bin/sh
```

### <mark style="color:yellow;">**Bash Into a Running Container**</mark>

#### <mark style="color:purple;">To enter the shell of a running container:</mark>

```bash
docker exec -it container-id-or-name /bin/sh
```

## <mark style="color:red;">**Maintenance and Cleanup**</mark>

### <mark style="color:yellow;">**Removing Stopped Containers, Unused Volumes, and Networks**</mark>

#### <mark style="color:purple;">To perform a cleanup:</mark>

```bash
docker system prune
```

### <mark style="color:yellow;">**Removing Unused Images**</mark>

#### <mark style="color:purple;">To remove dangling (unused) images:</mark>

```bash
docker system prune -a
```

## <mark style="color:red;">**Other Handy Commands**</mark>

### <mark style="color:yellow;">**Viewing Logs of a Container**</mark>

#### <mark style="color:purple;">To view the logs of a running or stopped container:</mark>

```bash
docker logs container-id-or-name
```

### <mark style="color:yellow;">**Copying Files from/to a Container**</mark>

#### <mark style="color:purple;">To copy files between your host and a container:</mark>

```bash
# From container to host
docker cp container-id-or-name:/path/in/container /path/on/host

# From host to container
docker cp /path/on/host container-id-or-name:/path/in/container
```

## <mark style="color:red;">**Debugging and Interactivity with Docker Containers**</mark>

Debugging containers often requires entering the container's environment, executing commands, or inspecting files. Here's how to navigate these tasks.

### <mark style="color:yellow;">**Entering a Container's Shell**</mark>

#### <mark style="color:purple;">Sometimes, to debug or inspect the container's internals, you may want to get a shell inside a running container. Here's how you can do that:</mark>

```bash
docker exec -it container-id-or-name /bin/sh
```

* If the container is using a different shell, you might need to replace `/bin/sh` with `/bin/bash` or another appropriate shell.

### <mark style="color:yellow;">**Running a Container with an Interactive Shell**</mark>

#### <mark style="color:purple;">If you are just starting a container and want it to give you a shell immediately, use:</mark>

```bash
docker run -it your-image-name:tag /bin/sh
```

{% hint style="info" %}
Again, replace `/bin/sh` if a different shell is used in the container.
{% endhint %}

### <mark style="color:yellow;">**Viewing Container Logs**</mark>

#### <mark style="color:purple;">To view the logs produced by a running or a stopped container:</mark>

```bash
docker logs container-id-or-name
```

### <mark style="color:yellow;">**Inspecting Container Metadata**</mark>

#### <mark style="color:purple;">For a detailed report on a container (like IP addresses, volumes, environment variables, etc.):</mark>

```bash
docker inspect container-id-or-name
```

### <mark style="color:yellow;">**Checking Container Processes**</mark>

#### <mark style="color:purple;">To see which processes are running inside a container:</mark>

```bash
docker top container-id-or-name
```

### <mark style="color:yellow;">**Debugging a Container That Won't Start**</mark>

#### <mark style="color:purple;">If a container is failing to start, it's often useful to override its entrypoint to keep it running and then bash into it to inspect:</mark>

```bash
docker run -it --entrypoint /bin/sh your-image-name:tag
```

{% hint style="info" %}
This will override the container's default entry point with a shell, allowing you to start the intended command manually or inspect why it might be failing.
{% endhint %}
