Fermer

Quelques commandes Docker

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker --version
docker --version
docker --version

Images

Lister les images locales

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker images
docker images
docker images

Récupérer une image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker pull <image_name>
docker pull <image_name>
docker pull <image_name>

Supprimer une image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker rmi <image_name>
docker rmi <image_name>
docker rmi <image_name>

Containers

Lister les containers

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker ps
docker ps
docker ps

Lister tous les containers

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker ps -a
docker ps -a
docker ps -a

Démarrer un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker stop <container_name>
docker stop <container_name>
docker stop <container_name>

Démarrer un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker start <container_name>
docker start <container_name>
docker start <container_name>

Supprimer un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker rm <container_name>
docker rm <container_name>
docker rm <container_name>

Les logs d’un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker logs <container_name>
docker logs <container_name>
docker logs <container_name>

Les volumes

Lister les volumes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker volume ls
docker volume ls
docker volume ls

Créer un volume

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker volume create <volume_name>
docker volume create <volume_name>
docker volume create <volume_name>

Supprimer un volume

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker volume rm <volume_name>
docker volume rm <volume_name>
docker volume rm <volume_name>

Monter un volume dans un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker-compose --version
docker-compose --version
docker-compose --version

Lancer un Docker Compose

Se déplacer dans le dossier contenant le fichier docker-compose.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd /folder/to/docker-compose
docker-compose up -d
cd /folder/to/docker-compose docker-compose up -d
cd /folder/to/docker-compose
docker-compose up -d

Arrêter le Docker Compose

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker-compose down
docker-compose down
docker-compose down

Exemple de fichier docker-compose.yml

Issu de PrestaShop : https://github.com/PrestaShop/PrestaShop/blob/develop/docker-compose.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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" ]
FROM node:20
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]

Construire une image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker build -t <image_name> .
docker build -t <image_name> .
docker build -t <image_name> .

Autres commandes

Inspecter un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker inspect <container_name>
docker inspect <container_name>
docker inspect <container_name>

Inspecter une image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker inspect <image_name>
docker inspect <image_name>
docker inspect <image_name>

Évènements

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker events
docker events
docker events

Statistiques

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker stats <container_name>
docker stats <container_name>
docker stats <container_name>

Exporter un container

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker import <file_name>.tar
docker import <file_name>.tar
docker import <file_name>.tar

Sauver une image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker load -i <file_name>.tar
docker load -i <file_name>.tar
docker load -i <file_name>.tar

Laisser une réponse

Votre adresse email ne sera pas publiée. Les champs obligatoires sont indiqués par *