Docker Cheat sheet - Installation and Docker Containers
Tags: docker, containers, bash
Published: 2021-10-02 06:38:00

## Installation

### Install Docker on Linux:

```bash
curl -fsSL https://get.docker.com -o get-docker.sh
```

To use Docker as a non-root user, consider adding the user to the docker group:

```bash
sudo usermod -aG docker <username>
```

### Download docker-compose:

```bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 
sudo chmod +x /usr/local/bin/docker-compose
```

### Check docker-compose version:

```bash
docker-compose version
```

> **TIP**: For auto-completion, `docker` + **Tab** Key

## Versions

Docker versions are `YY.MM` based.

**Edge** -> monthly release and one month support
**Stable** -> quarterly release and 4 months support

## Containers

### Check version of client and server (engine):

```bash
docker version
```

### More information about the client and server:

```bash
docker info
```

### Management commands

```bash
docker <command> <sub-command> (options)
```

### Image vs. Container

**Image** is the app to run
**Container** is an instance of that image running as a process. There can be many containers.

Docker's default image registry is **Docker Hub**.

### Starting an Nginx server

#### Download an image from Docker Hub and start a container from that image:

```bash
docker container run --publish 80:80 nginx
```

The command `docker container run` does the following:

1. Looks for the image locally in the image cache
2. If not found locally, it looks for the same in the remote image repository which defaults to Docker Hub and downloads the latest version
3. Creates a container based on that image and prepares to start
4. Gives the container a virtual IP on a private network inside the Docker engine
5. Opens up port 80 on the host and forwards all requests to port 80 in the container
6. Starts container by using the **CMD** instruction command in the image's Dockerfile

#### :bulb: Instruction commands

There are three types of instructions (commands) that we can use to build and run Dockerfiles:

1. **RUN** - Mainly used to build images and install applications and packages. Builds a new layer over an existing image by committing the results.

2. **CMD** - Sets default parameters that can be overridden from the Docker CLI when a container is running.
    
3. **ENTRYPOINT** - Default parameters that cannot be overridden when Docker Containers run with CLI parameters.

### Running container in the background:

```bash
docker container run --publish <port-host>:<port-container> --detach <image>

# Example
docker container run --publish 80:80 --detach nginx
```

### Give a name to the container:

```bash
docker container run -p 80:80 -d --name webhost nginx
```
> `-p` is short for `--publish` and `-d` is short for `--detach`

### List all containers:

```bash
docker container ls -a
```
*Short version*:
```bash
docker ps -a
```

### List only running containers:

```bash
docker container ls
```
*Short version*:
```bash
docker ps
```

### Stop a container:

```bash
docker container stop <first-3-digits-container-id>
```

### Get logs for a running container:

```bash
docker container logs <container-name>
```

### Display the running processes of a container:

```bash
docker container top <container-name>
```

### Remove one or more containers:

```bash
docker container rm <space-separated-3-digit-container-ids>
```

### Force remove running containers:

```bash
docker container rm -f <space-separated-3-digit-container-ids>
```

### Show all running processes:

```bash
ps aux
```

### Use environment variables:

```bash
docker container run --publish 3307:3307 --detach --name database --env MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
```

### Get details of container config (metadata in JSON format):

```bash
docker container inspect <container-name>
```

### Get live performance stats for all containers:

```bash
docker container stats
```

### Get performance stats for a container:

```bash
docker container stats <container-name>
```

> **TIP** - To see all options for a command, do `docker <command> <sub-command> --help`

### Get a shell inside containers

#### Start a new container interactively:

```bash
docker container run -it --name <container-name> <image> <cmd>

# Example
docker container run -it --name nginx nginx bash
```

```bash
# Default CMD for Ubuntu image is bash
docker container run -it --name ubuntu ubuntu
```

> `-it` is two different options: 
`-t` -> allocates Pseudo-TTY, simulates a real terminal
`-i` -> keeps **interactive** session open to receive terminal input

#### Start an existing container (stopped earlier) interactively:

```bash
docker container start -ai <container-name>
```

> `-a` -> attach
`-i` -> interactive shell

#### Run additional command in an existing running container:

```bash
docker container exec -it <container-name> <cmd>
```

### Download an image:

```bash
docker pull <image>
```

### List all images:

```bash
docker image ls
```