Skip to content

Docker

Docker is a runtime for create and manage containers.

Docker images

Images are defined by a text file call Dockerfile.

Dockerfile Template

Docker
 FROM node:lts-alpine3.14

 RUN apk add --no-cache python g++ make
 WORKDIR /app
 COPY . .

 RUN yarn install --production

 CMD ["node", "src/index.js"]

Commands

  • FROM select base image. Use scratch if you want to begin with nothing.
  • RUN launch a cli command.
  • WORKDIR change workdir. Clean version of the command cd.
  • COPY copy files inside a container. Clean version of the command copy.
  • CMD latest argument of a dockerfile. This is the cli command to launch when the container start.

Docker operations

Launch a container

Bash
docker run <nom image> <args>

Start/Stop/Kill a container

Bash
# Start
docker start <container>
# Stop
docker stop <container>
# Kill
docker kill <container>

See statistics about running containers

Bash
docker stats

Tip for watching in direct containers status watch docker container ps. Very usefull when your containers constantly rebooting

Freeze / Unfreeze a container

Bash
# Freeze
docker container pause <container>
# Unfreeze
docker container unpause <container>

Docker networks

Create/Delete a network

Bash
# Create
docker network create <myBridge>
# Remove
docker network remove <myBridge>

Docker storage

Check/Clean docker disk occupation

Bash
# Check
docker system df
# Clean
docker image prune
# Delete all images on my 
docker rmi $(docker images -a -q)

Docker Compose

docker compose is a tool for creating multi images stack.

docker compose operations

Test a docker compose stash

Bash
docker compose up

Build a docker compose

Bash
docker compose up --build

Launch/Shutdown a docker compose stack

Bash
# Launch the complete docker compose
docker compose start
# Launch just a service from docker compose
docker compose start <service>

# Shutdown the complete docker compose
docker compose stop
# Shutdown just a service from docker compose
docker compose stop <service>

# Restart the complete docker compose
docker compose restart
# Restart just a service from docker compose
docker compose restart <service>

Connect to a bash inside container

Bash
docker exec -it <container> bash

Docker Compose network

To create a network beetween severals docker compose file

YAML
# Add this network
networks:
  YOUR_NETWORK_NAME:
    external:
      name: YOUR_NETWORK_NAME

Tools

  • Dive est un outil permettant de naviguer dans les images docker à chaque instruction et de voir les fichiers évolués.
  • Portainer est un outil permettant d'obtenir une interface web pour gérer Docker et docker compose.
  • Docker Bench for Security est script bash permettant de vérifier que docker est correctement installé sous linux d'un point de vue sécurité

Sources