Engineering Tools for AI

Programmation

Docker

Guide consultable sur les images, conteneurs, Dockerfile, Compose, ports, volumes, logs et problèmes courants Docker.

33 éléments

Concepts de base

Docker

Docker

docker --version

Sens

A tool for packaging and running applications in isolated containers.

Quand l'utiliser

Use it when you want the app to run with the same OS packages, runtime, and dependencies across machines.

Exemple

docker --version

Exemple de demande

Explain whether this project can be run with Docker.

Concepts de base

Docker daemon

Docker daemon

Cannot connect to the Docker daemon

Sens

The background service that builds images and runs containers.

Quand l'utiliser

If Docker commands cannot connect, Docker Desktop or the Docker service may not be running.

Exemple

Cannot connect to the Docker daemon

Exemple de demande

Check why Docker commands cannot connect to the daemon.

Concepts de base

Image

Image

node:20-alpine

Sens

A reusable template used to create containers.

Quand l'utiliser

Images define what OS, runtime, libraries, and app files a container starts with.

Exemple

node:20-alpine

Exemple de demande

Tell me which base image this Dockerfile uses.

Concepts de base

Container

Container

docker ps

Sens

A running or stopped instance created from an image.

Quand l'utiliser

Use containers to run the app, database, cache, or worker in isolated environments.

Exemple

docker ps

Exemple de demande

List running containers and explain what each one does.

Concepts de base

Registry

Registry

docker pull postgres:16

Sens

A server where Docker images are stored and downloaded from.

Quand l'utiliser

Docker Hub and private registries are used to share base images or deployable app images.

Exemple

docker pull postgres:16

Exemple de demande

Explain where this image will be downloaded from.

Dockerfile

Dockerfile

Dockerfile

Dockerfile

Sens

A file that defines how to build a Docker image.

Quand l'utiliser

Use it to install dependencies, copy app files, and define the command that starts the app.

Exemple

Dockerfile

Exemple de demande

Read this Dockerfile and summarize the build steps.

Dockerfile

FROM

FROM

FROM node:20-alpine

Sens

The Dockerfile instruction that chooses the base image.

Quand l'utiliser

It decides the starting OS layer and often the runtime version.

Attention

Changing the base image can affect available commands, security updates, and image size.

Exemple

FROM node:20-alpine

Exemple de demande

Explain what this FROM image includes.

Dockerfile

WORKDIR

WORKDIR

WORKDIR /app

Sens

Sets the folder inside the image where following commands run.

Quand l'utiliser

Use it so COPY, RUN, and CMD commands operate from a predictable app folder.

Exemple

WORKDIR /app

Exemple de demande

Explain which folder this Dockerfile uses as the app directory.

Dockerfile

COPY

COPY

COPY package.json package-lock.json ./

Sens

Copies files from the build context into the image.

Quand l'utiliser

Use it to add dependency files, source code, and static assets to the image.

Exemple

COPY package.json package-lock.json ./

Exemple de demande

Tell me which files this COPY step adds to the image.

Dockerfile

RUN

RUN

RUN npm ci

Sens

Runs a command while building the image.

Quand l'utiliser

Use it to install OS packages, install dependencies, or build app files.

Attention

RUN happens at image build time, not when the container starts.

Exemple

RUN npm ci

Exemple de demande

Separate Dockerfile build-time commands from container startup commands.

Dockerfile

CMD

CMD

CMD ["npm", "run", "start"]

Sens

Defines the default command that runs when the container starts.

Quand l'utiliser

Use it to start the web server, API server, worker, or script.

Exemple

CMD ["npm", "run", "start"]

Exemple de demande

Tell me what command this container starts with.

Dockerfile

EXPOSE

EXPOSE

EXPOSE 3000

Sens

Documents which port the containerized app is expected to listen on.

Quand l'utiliser

Use it as a hint, then publish the port with docker run -p or compose ports.

Attention

EXPOSE alone does not make the app reachable from your browser.

Exemple

EXPOSE 3000

Exemple de demande

Explain whether this exposed port is actually published.

Dockerfile

.dockerignore

.dockerignore

node_modules

Sens

A file that excludes paths from the Docker build context.

Quand l'utiliser

Use it to keep local dependencies, build output, secrets, and temporary files out of the image build.

Exemple

node_modules

Exemple de demande

Check whether this .dockerignore excludes unsafe or unnecessary files.

Dockerfile

Build context

Build context

docker build -t my-app .

Sens

The folder sent to Docker during image build.

Quand l'utiliser

The final dot in docker build usually means the current folder is the build context.

Attention

A large context slows builds and can accidentally include private files.

Exemple

docker build -t my-app .

Exemple de demande

Explain what files are available to this Docker build.

Docker Compose

Docker Compose

Docker Compose

docker compose up

Sens

A tool for running multiple Docker services from one YAML file.

Quand l'utiliser

Use it for app plus database, cache, worker, or local development stacks.

Exemple

docker compose up

Exemple de demande

Explain what services this compose file starts.

Docker Compose

services

services

services: web: db:

Sens

Named app parts defined in a compose file.

Quand l'utiliser

A web service might run the app while a db service runs PostgreSQL.

Exemple

services: web: db:

Exemple de demande

List the services in this compose file and what each does.

Docker Compose

docker compose up

docker compose up

docker compose up -d

Sens

Starts the services defined in a compose file.

Quand l'utiliser

Use -d when you want services to run in the background.

Exemple

docker compose up -d

Exemple de demande

Start the compose services and show which ones are running.

Docker Compose

docker compose down

docker compose down

docker compose down

Sens

Stops and removes containers created by compose.

Quand l'utiliser

Use it when you are done with a local development stack.

Attention

Named volumes usually remain unless you explicitly remove volumes.

Exemple

docker compose down

Exemple de demande

Stop this compose stack without deleting persistent data.

Commandes

docker build

docker build

docker build -t my-app .

Sens

Builds a Docker image from a Dockerfile and context.

Quand l'utiliser

Use it after writing a Dockerfile or changing app dependencies.

Exemple

docker build -t my-app .

Exemple de demande

Build this Docker image and summarize any build errors.

Commandes

docker run

docker run

docker run --name my-app -p 3000:3000 my-app

Sens

Creates and starts a container from an image.

Quand l'utiliser

Use it when you want to start one container directly without compose.

Exemple

docker run --name my-app -p 3000:3000 my-app

Exemple de demande

Run this image and explain the browser URL.

Commandes

docker ps

docker ps

docker ps

Sens

Lists running containers.

Quand l'utiliser

Use docker ps -a when you also want stopped containers.

Exemple

docker ps

Exemple de demande

Show running containers and their published ports.

Commandes

docker stop

docker stop

docker stop my-app

Sens

Stops a running container.

Quand l'utiliser

Use it before removing a container or freeing a port.

Exemple

docker stop my-app

Exemple de demande

Stop the container using port 3000.

Commandes

docker logs

docker logs

docker logs my-app

Sens

Prints logs from a container.

Quand l'utiliser

Use it when a container exits, crashes, or the app does not respond.

Exemple

docker logs my-app

Exemple de demande

Read this container's logs and find the first error.

Commandes

docker exec

docker exec

docker exec -it my-app sh

Sens

Runs a command inside an already running container.

Quand l'utiliser

Use it to inspect files, environment variables, or installed tools inside a container.

Attention

Changes made manually inside a container can disappear when the container is recreated.

Exemple

docker exec -it my-app sh

Exemple de demande

Open a shell in this container and check its environment variables.

Ports et réseau

ports

ports

3000:3000

Sens

Maps a port on your machine to a port inside the container.

Quand l'utiliser

Use it so a browser can reach a web app running inside a container.

Attention

The left side is the host port, and the right side is the container port.

Exemple

3000:3000

Exemple de demande

Explain which URL I should open for this ports setting.

Ports et réseau

localhost in Docker

localhost in Docker

host.docker.internal

Sens

localhost can mean the container itself, not your host machine, when code runs inside Docker.

Quand l'utiliser

Use service names in compose networks, or host.docker.internal when a container must reach the host machine.

Exemple

host.docker.internal

Exemple de demande

Explain why localhost does not reach the database from this container.

Fichiers et données

Volume

Volume

db-data:/var/lib/postgresql/data

Sens

Docker-managed storage that can persist data outside a container's lifecycle.

Quand l'utiliser

Use volumes for database data or other files that should survive container recreation.

Attention

Removing volumes can delete persistent data.

Exemple

db-data:/var/lib/postgresql/data

Exemple de demande

Tell me which compose volumes store persistent data.

Fichiers et données

Bind mount

Bind mount

./src:/app/src

Sens

Connects a host folder directly into a container path.

Quand l'utiliser

Use it during development so code edits on the host appear inside the container.

Attention

A bind mount can hide files that were copied into the image at the same path.

Exemple

./src:/app/src

Exemple de demande

Explain whether this volume is a named volume or a bind mount.

Fichiers et données

environment

environment

DATABASE_URL=postgres://db:5432/app

Sens

Configuration values passed into a container.

Quand l'utiliser

Use environment variables for database URLs, ports, modes, feature flags, and runtime settings.

Attention

Do not bake secrets into public images or commit real secret values.

Exemple

DATABASE_URL=postgres://db:5432/app

Exemple de demande

Check which environment variables this container needs.

Dépannage

port is already allocated

port is already allocated

Bind for 0.0.0.0:3000 failed: port is already allocated

Sens

Docker cannot publish a host port because another process or container is already using it.

Quand l'utiliser

Find the process or container using the port, then stop it or choose a different host port.

Exemple

Bind for 0.0.0.0:3000 failed: port is already allocated

Exemple de demande

Find what is using this Docker port and suggest a safe fix.

Dépannage

container name is already in use

container name is already in use

Conflict. The container name "/my-app" is already in use

Sens

A container with the same name already exists.

Quand l'utiliser

List existing containers, then reuse, remove, or rename the old container.

Exemple

Conflict. The container name "/my-app" is already in use

Exemple de demande

Resolve this container name conflict without deleting data unexpectedly.

Dépannage

Build cache

Build cache

docker build --no-cache -t my-app .

Sens

Docker can reuse previous build layers to make builds faster.

Quand l'utiliser

If a change seems ignored, inspect the Dockerfile order or rebuild without cache as a diagnostic step.

Attention

--no-cache can make builds slower, so use it intentionally.

Exemple

docker build --no-cache -t my-app .

Exemple de demande

Explain whether Docker build cache could be hiding my change.

Sécurité

docker system prune

docker system prune

docker system prune

Sens

Removes unused Docker data such as stopped containers, unused networks, and dangling images.

Quand l'utiliser

Use it when Docker uses too much disk space and you understand what will be removed.

Attention

With extra flags, prune can delete images or volumes you still care about.

Exemple

docker system prune

Exemple de demande

Before pruning Docker, show what would be removed and what data is safe.