Docker est une plateforme pour développer, livrer et exécuter des applications à l’intérieur de conteneurs. Elle fournit un environnement léger, portable et cohérent pour le développement et le déploiement de logiciels. Ci-dessous, les principales commandes Docker, pour apprendre à gérer efficacement les applications conteneurisées.
Installation
Vérifier la version
docker --version
Images
Lister les images locales
docker images
Récupérer une image
docker pull <image_name>
Supprimer une image
docker rmi <image_name>
Containers
Lister les containers
docker ps
Lister tous les containers
docker ps -a
Démarrer un container
docker run -d --name <container_name> <image_name>
docker run -d --name <container_name> <image_name>
docker run -d --name <container_name> <image_name>
-d
: démarrer en tâche de fond
Stopper un container
docker stop <container_name>
docker stop <container_name>
docker stop <container_name>
Démarrer un container
docker start <container_name>
docker start <container_name>
docker start <container_name>
Supprimer un container
docker rm <container_name>
docker rm <container_name>
docker rm <container_name>
Les logs d’un container
docker logs <container_name>
docker logs <container_name>
docker logs <container_name>
Les volumes
Lister les volumes
docker volume ls
Créer un volume
docker volume create <volume_name>
docker volume create <volume_name>
docker volume create <volume_name>
Supprimer un volume
docker volume rm <volume_name>
docker volume rm <volume_name>
docker volume rm <volume_name>
Monter un volume dans un container
docker run -d --name <container_name> -v <volume_name>:/path/in/container <image_name>
docker run -d --name <container_name> -v <volume_name>:/path/in/container <image_name>
docker run -d --name <container_name> -v <volume_name>:/path/in/container <image_name>
Docker Compose
Vérifier l’installation
docker-compose --version
Lancer un Docker Compose
Se déplacer dans le dossier contenant le fichier docker-compose.yml
cd /folder/to/docker-compose
cd /folder/to/docker-compose
docker-compose up -d
cd /folder/to/docker-compose
docker-compose up -d
Arrêter le Docker Compose
docker-compose down
Exemple de fichier docker-compose.yml
Issu de PrestaShop : https://github.com/PrestaShop/PrestaShop/blob/develop/docker-compose.yml
MYSQL_ROOT_PASSWORD: ${DB_PASSWD:-prestashop}
MYSQL_DATABASE: ${DB_NAME:-prestashop}
hostname: ${PS_HOSTNAME:-localhost}
dockerfile: .docker/Dockerfile
- VERSION=${VERSION:-8.1-apache}
- USER_ID=${USER_ID:-1000}
- GROUP_ID=${GROUP_ID:-1000}
- NODE_VERSION=${NODE_VERSION:-16.20.1}
- INSTALL_XDEBUG=${INSTALL_XDEBUG:-false}
DISABLE_MAKE: ${DISABLE_MAKE:-0}
PS_INSTALL_AUTO: ${PS_INSTALL_AUTO:-1}
DB_PASSWD: ${DB_PASSWD:-prestashop}
DB_NAME: ${DB_NAME:-prestashop}
DB_SERVER: ${DB_SERVER:-mysql}
DB_PREFIX: ${DB_PREFIX:-ps_}
PS_DOMAIN: ${PS_DOMAIN:-localhost:8001}
PS_FOLDER_INSTALL: ${PS_FOLDER_INSTALL:-install-dev}
PS_FOLDER_ADMIN: ${PS_FOLDER_ADMIN:-admin-dev}
PS_COUNTRY: ${PS_COUNTRY:-fr}
PS_LANGUAGE: ${PS_LANGUAGE:-en}
PS_DEV_MODE: ${PS_DEV_MODE:-1}
PS_ENABLE_SSL: ${PS_ENABLE_SSL:-0}
PS_ERASE_DB: ${PS_ERASE_DB:-0}
PS_USE_DOCKER_MAILDEV: ${PS_USE_DOCKER_MAILDEV:-1}
ADMIN_MAIL: ${ADMIN_MAIL:-demo@prestashop.com}
ADMIN_PASSWD: ${ADMIN_PASSWD:-Correct Horse Battery Staple}
BLACKFIRE_ENABLE: ${BLACKFIRE_ENABLE:-0}
BLACKFIRE_SERVER_ID: ${BLACKFIRE_SERVER_ID:-0}
BLACKFIRE_SERVER_TOKEN: ${BLACKFIRE_SERVER_TOKEN:-0}
command: /tmp/wait-for-it.sh --timeout=60 --strict mysql:3306 -- /tmp/docker_run_git.sh
- ./:/var/www/html:delegated
version: '2.1'
volumes:
db-data:
services:
mysql:
image: mysql:8
ports:
- "3306:3306"
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWD:-prestashop}
MYSQL_DATABASE: ${DB_NAME:-prestashop}
restart: unless-stopped
networks:
- prestashop-network
prestashop-git:
hostname: ${PS_HOSTNAME:-localhost}
build:
dockerfile: .docker/Dockerfile
context: .
args:
- VERSION=${VERSION:-8.1-apache}
- USER_ID=${USER_ID:-1000}
- GROUP_ID=${GROUP_ID:-1000}
- NODE_VERSION=${NODE_VERSION:-16.20.1}
- INSTALL_XDEBUG=${INSTALL_XDEBUG:-false}
environment:
DISABLE_MAKE: ${DISABLE_MAKE:-0}
PS_INSTALL_AUTO: ${PS_INSTALL_AUTO:-1}
DB_PASSWD: ${DB_PASSWD:-prestashop}
DB_NAME: ${DB_NAME:-prestashop}
DB_SERVER: ${DB_SERVER:-mysql}
DB_PREFIX: ${DB_PREFIX:-ps_}
PS_DOMAIN: ${PS_DOMAIN:-localhost:8001}
PS_FOLDER_INSTALL: ${PS_FOLDER_INSTALL:-install-dev}
PS_FOLDER_ADMIN: ${PS_FOLDER_ADMIN:-admin-dev}
PS_COUNTRY: ${PS_COUNTRY:-fr}
PS_LANGUAGE: ${PS_LANGUAGE:-en}
PS_DEV_MODE: ${PS_DEV_MODE:-1}
PS_ENABLE_SSL: ${PS_ENABLE_SSL:-0}
PS_ERASE_DB: ${PS_ERASE_DB:-0}
PS_USE_DOCKER_MAILDEV: ${PS_USE_DOCKER_MAILDEV:-1}
ADMIN_MAIL: ${ADMIN_MAIL:-demo@prestashop.com}
ADMIN_PASSWD: ${ADMIN_PASSWD:-Correct Horse Battery Staple}
BLACKFIRE_ENABLE: ${BLACKFIRE_ENABLE:-0}
BLACKFIRE_SERVER_ID: ${BLACKFIRE_SERVER_ID:-0}
BLACKFIRE_SERVER_TOKEN: ${BLACKFIRE_SERVER_TOKEN:-0}
command: /tmp/wait-for-it.sh --timeout=60 --strict mysql:3306 -- /tmp/docker_run_git.sh
ports:
- "8001:80"
- "8002:443"
depends_on:
- mysql
volumes:
- ./:/var/www/html:delegated
networks:
- prestashop-network
maildev:
image: 'maildev/maildev'
ports:
- "1080:1080"
networks:
- prestashop-network
networks:
prestashop-network:
name: prestashop-network
version: '2.1'
volumes:
db-data:
services:
mysql:
image: mysql:8
ports:
- "3306:3306"
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWD:-prestashop}
MYSQL_DATABASE: ${DB_NAME:-prestashop}
restart: unless-stopped
networks:
- prestashop-network
prestashop-git:
hostname: ${PS_HOSTNAME:-localhost}
build:
dockerfile: .docker/Dockerfile
context: .
args:
- VERSION=${VERSION:-8.1-apache}
- USER_ID=${USER_ID:-1000}
- GROUP_ID=${GROUP_ID:-1000}
- NODE_VERSION=${NODE_VERSION:-16.20.1}
- INSTALL_XDEBUG=${INSTALL_XDEBUG:-false}
environment:
DISABLE_MAKE: ${DISABLE_MAKE:-0}
PS_INSTALL_AUTO: ${PS_INSTALL_AUTO:-1}
DB_PASSWD: ${DB_PASSWD:-prestashop}
DB_NAME: ${DB_NAME:-prestashop}
DB_SERVER: ${DB_SERVER:-mysql}
DB_PREFIX: ${DB_PREFIX:-ps_}
PS_DOMAIN: ${PS_DOMAIN:-localhost:8001}
PS_FOLDER_INSTALL: ${PS_FOLDER_INSTALL:-install-dev}
PS_FOLDER_ADMIN: ${PS_FOLDER_ADMIN:-admin-dev}
PS_COUNTRY: ${PS_COUNTRY:-fr}
PS_LANGUAGE: ${PS_LANGUAGE:-en}
PS_DEV_MODE: ${PS_DEV_MODE:-1}
PS_ENABLE_SSL: ${PS_ENABLE_SSL:-0}
PS_ERASE_DB: ${PS_ERASE_DB:-0}
PS_USE_DOCKER_MAILDEV: ${PS_USE_DOCKER_MAILDEV:-1}
ADMIN_MAIL: ${ADMIN_MAIL:-demo@prestashop.com}
ADMIN_PASSWD: ${ADMIN_PASSWD:-Correct Horse Battery Staple}
BLACKFIRE_ENABLE: ${BLACKFIRE_ENABLE:-0}
BLACKFIRE_SERVER_ID: ${BLACKFIRE_SERVER_ID:-0}
BLACKFIRE_SERVER_TOKEN: ${BLACKFIRE_SERVER_TOKEN:-0}
command: /tmp/wait-for-it.sh --timeout=60 --strict mysql:3306 -- /tmp/docker_run_git.sh
ports:
- "8001:80"
- "8002:443"
depends_on:
- mysql
volumes:
- ./:/var/www/html:delegated
networks:
- prestashop-network
maildev:
image: 'maildev/maildev'
ports:
- "1080:1080"
networks:
- prestashop-network
networks:
prestashop-network:
name: prestashop-network
Dockerfile
Un Dockerfile est un fichier contenant une liste de commandes de ligne de commande que Docker exécute une à une dans le but de monter une image Docker pour faire tourner votre application.
Exemple de fichier
CMD [ "node", "server.js" ]
FROM node:20
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
FROM node:20
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
Construire une image
docker build -t <image_name> .
docker build -t <image_name> .
docker build -t <image_name> .
Autres commandes
Inspecter un container
docker inspect <container_name>
docker inspect <container_name>
docker inspect <container_name>
Inspecter une image
docker inspect <image_name>
docker inspect <image_name>
docker inspect <image_name>
Évènements
docker events
Statistiques
docker stats <container_name>
docker stats <container_name>
docker stats <container_name>
Exporter un container
docker export <container_name> -o <file_name>.tar
docker export <container_name> -o <file_name>.tar
docker export <container_name> -o <file_name>.tar
Importer un container
docker import <file_name>.tar
docker import <file_name>.tar
docker import <file_name>.tar
Sauver une image
docker save -o <file_name>.tar <image_name>
docker save -o <file_name>.tar <image_name>
docker save -o <file_name>.tar <image_name>
Charger une image
docker load -i <file_name>.tar
docker load -i <file_name>.tar
docker load -i <file_name>.tar