# Memo - Docker commands 🐳

> Docker cheat sheet: key concepts (containers, images, Dockerfile, Compose, volumes) and the essential commands to create, run, list and remove containers.

- Auteur / Author: Lucas Videlaine, Security Consultant & Auditor
- URL: https://www.videlaine.fr/en/posts/memo-docker-commands/
- Langue / Language: en
- Publié / Published: 2022-07-28
- Tags: tech, cloud, docker

Docker is free software that allows applications to be launched in software containers.
It is not virtualisation, but containerisation, a lighter form that relies on certain parts of the host machine for its operation. This approach increases the flexibility and portability of an application's execution, which will be able to run reliably and predictably on a wide variety of host machines, whether on the local machine, a private or public cloud, a bare machine, etc.
Technically, Docker extends the standard Linux container format, LXC, with a high-level API that provides a practical virtualisation solution that runs processes in isolation. To do this, Docker uses LXC, cgroups, and the Linux kernel itself, among other things. Unlike traditional virtual machines, a Docker container does not include an operating system, but instead relies on the operating system features provided by the host machine.

## Vocabulary definition

### Containers

Unlike traditional virtual machines, a Docker container does not include a guest operating system. Instead, it relies on the host kernel's features and leverages resource isolation (CPU, memory, I/O, network connections, etc.).

### Images

A Docker image represents the file system, without the processes. It contains everything you have decided to install on it, such as a database system, a web server, a Python application, etc. Images are created from configuration files, called ‘Dockerfiles’, which describe exactly what needs to be installed in the container. A container therefore corresponds to the execution of a specific image.

### Dockerfiles

The Dockerfile is your source file that enables the construction of an image. A Dockerfile can be included in other Dockerfiles and serve as the basis for several different images. You can choose to use official images, modified images, which you can find on [Docker Hub](https://hub.docker.com/) or other image repositories, but you can also choose to customise your own images by writing a Dockerfile.

### Compose 

Compose is a tool for defining and running multi-container applications. Since containers are ideal for microservice-based applications, it becomes necessary to manage the various containers and their interconnections.

### Volumes

Volumes are a mechanism that enables the persistence of data generated by and used by Docker containers. Docker volumes offer numerous advantages, such as ease of backup and migration, as well as simplified management thanks to Docker tools and commands.

### Kubernetes

Kubernetes is an open-source system that aims to provide a platform for automating the deployment, scaling, and implementation of containers.

## Essential commands

### Various information about Docker

```
docker info
docker version
```

### Create and start a container

```
docker run [image_name]
```

### Stop a container

```
docker stop [container_id]
```

### Start a container

```
docker start [container_id]
```

### Delete a container

```
docker rm [container_id]
```

### List started containers and list all containers

```
docker ps 
docker ps -a
```

### Stop all containers

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

### Start all containers

```
docker start $(docker ps -a -q)
```

### Delete all containers (after stopping them)

```
docker rm $(docker  ps -a -q)
```

### List local images (pulled or built)

```
docker images
```

### Delete one or more images

```
docker rmi [image_id] [image_id] ...
```

### Building an image from a Dockerfile

```
docker build -t [wanted_image_name] -f [path_to_dockerfile]
```

### Run a docker-compose file in detached mode

```
docker-compose -d -f [path_to_docker-compose-file]
```

### Run a command within a container

```
docker exec -it [container_id] [command]
docker exec -it [container_id] bash
```

### Display the logs of a container

```
docker logs [container_id]
```

## Sources

<a href="https://docs.docker.com/engine/reference/commandline/docker/" target="_blank">Official documentation</a>
<a href="https://lucasvidelaine.wordpress.com/2018/01/26/memo-commandes-docker/" target="_blank">My original article from 2018</a>
