Engineering Tools for AI

Lập trình

Docker

Hướng dẫn tra cứu Docker image, container, Dockerfile, Compose, port, volume, log và các lỗi thường gặp.

33 mục

Khái niệm cốt lõi

Docker

Docker

docker --version

Ý nghĩa

A tool for packaging and running applications in isolated containers.

Khi dùng

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

Ví dụ

docker --version

Ví dụ yêu cầu

Explain whether this project can be run with Docker.

Khái niệm cốt lõi

Docker daemon

Docker daemon

Cannot connect to the Docker daemon

Ý nghĩa

The background service that builds images and runs containers.

Khi dùng

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

Ví dụ

Cannot connect to the Docker daemon

Ví dụ yêu cầu

Check why Docker commands cannot connect to the daemon.

Khái niệm cốt lõi

Image

Image

node:20-alpine

Ý nghĩa

A reusable template used to create containers.

Khi dùng

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

Ví dụ

node:20-alpine

Ví dụ yêu cầu

Tell me which base image this Dockerfile uses.

Khái niệm cốt lõi

Container

Container

docker ps

Ý nghĩa

A running or stopped instance created from an image.

Khi dùng

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

Ví dụ

docker ps

Ví dụ yêu cầu

List running containers and explain what each one does.

Khái niệm cốt lõi

Registry

Registry

docker pull postgres:16

Ý nghĩa

A server where Docker images are stored and downloaded from.

Khi dùng

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

Ví dụ

docker pull postgres:16

Ví dụ yêu cầu

Explain where this image will be downloaded from.

Dockerfile

Dockerfile

Dockerfile

Dockerfile

Ý nghĩa

A file that defines how to build a Docker image.

Khi dùng

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

Ví dụ

Dockerfile

Ví dụ yêu cầu

Read this Dockerfile and summarize the build steps.

Dockerfile

FROM

FROM

FROM node:20-alpine

Ý nghĩa

The Dockerfile instruction that chooses the base image.

Khi dùng

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

Lưu ý

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

Ví dụ

FROM node:20-alpine

Ví dụ yêu cầu

Explain what this FROM image includes.

Dockerfile

WORKDIR

WORKDIR

WORKDIR /app

Ý nghĩa

Sets the folder inside the image where following commands run.

Khi dùng

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

Ví dụ

WORKDIR /app

Ví dụ yêu cầu

Explain which folder this Dockerfile uses as the app directory.

Dockerfile

COPY

COPY

COPY package.json package-lock.json ./

Ý nghĩa

Copies files from the build context into the image.

Khi dùng

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

Ví dụ

COPY package.json package-lock.json ./

Ví dụ yêu cầu

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

Dockerfile

RUN

RUN

RUN npm ci

Ý nghĩa

Runs a command while building the image.

Khi dùng

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

Lưu ý

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

Ví dụ

RUN npm ci

Ví dụ yêu cầu

Separate Dockerfile build-time commands from container startup commands.

Dockerfile

CMD

CMD

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

Ý nghĩa

Defines the default command that runs when the container starts.

Khi dùng

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

Ví dụ

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

Ví dụ yêu cầu

Tell me what command this container starts with.

Dockerfile

EXPOSE

EXPOSE

EXPOSE 3000

Ý nghĩa

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

Khi dùng

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

Lưu ý

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

Ví dụ

EXPOSE 3000

Ví dụ yêu cầu

Explain whether this exposed port is actually published.

Dockerfile

.dockerignore

.dockerignore

node_modules

Ý nghĩa

A file that excludes paths from the Docker build context.

Khi dùng

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

Ví dụ

node_modules

Ví dụ yêu cầu

Check whether this .dockerignore excludes unsafe or unnecessary files.

Dockerfile

Build context

Build context

docker build -t my-app .

Ý nghĩa

The folder sent to Docker during image build.

Khi dùng

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

Lưu ý

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

Ví dụ

docker build -t my-app .

Ví dụ yêu cầu

Explain what files are available to this Docker build.

Docker Compose

Docker Compose

Docker Compose

docker compose up

Ý nghĩa

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

Khi dùng

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

Ví dụ

docker compose up

Ví dụ yêu cầu

Explain what services this compose file starts.

Docker Compose

services

services

services: web: db:

Ý nghĩa

Named app parts defined in a compose file.

Khi dùng

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

Ví dụ

services: web: db:

Ví dụ yêu cầu

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

Docker Compose

docker compose up

docker compose up

docker compose up -d

Ý nghĩa

Starts the services defined in a compose file.

Khi dùng

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

Ví dụ

docker compose up -d

Ví dụ yêu cầu

Start the compose services and show which ones are running.

Docker Compose

docker compose down

docker compose down

docker compose down

Ý nghĩa

Stops and removes containers created by compose.

Khi dùng

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

Lưu ý

Named volumes usually remain unless you explicitly remove volumes.

Ví dụ

docker compose down

Ví dụ yêu cầu

Stop this compose stack without deleting persistent data.

Lệnh

docker build

docker build

docker build -t my-app .

Ý nghĩa

Builds a Docker image from a Dockerfile and context.

Khi dùng

Use it after writing a Dockerfile or changing app dependencies.

Ví dụ

docker build -t my-app .

Ví dụ yêu cầu

Build this Docker image and summarize any build errors.

Lệnh

docker run

docker run

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

Ý nghĩa

Creates and starts a container from an image.

Khi dùng

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

Ví dụ

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

Ví dụ yêu cầu

Run this image and explain the browser URL.

Lệnh

docker ps

docker ps

docker ps

Ý nghĩa

Lists running containers.

Khi dùng

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

Ví dụ

docker ps

Ví dụ yêu cầu

Show running containers and their published ports.

Lệnh

docker stop

docker stop

docker stop my-app

Ý nghĩa

Stops a running container.

Khi dùng

Use it before removing a container or freeing a port.

Ví dụ

docker stop my-app

Ví dụ yêu cầu

Stop the container using port 3000.

Lệnh

docker logs

docker logs

docker logs my-app

Ý nghĩa

Prints logs from a container.

Khi dùng

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

Ví dụ

docker logs my-app

Ví dụ yêu cầu

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

Lệnh

docker exec

docker exec

docker exec -it my-app sh

Ý nghĩa

Runs a command inside an already running container.

Khi dùng

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

Lưu ý

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

Ví dụ

docker exec -it my-app sh

Ví dụ yêu cầu

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

Port và mạng

ports

ports

3000:3000

Ý nghĩa

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

Khi dùng

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

Lưu ý

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

Ví dụ

3000:3000

Ví dụ yêu cầu

Explain which URL I should open for this ports setting.

Port và mạng

localhost in Docker

localhost in Docker

host.docker.internal

Ý nghĩa

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

Khi dùng

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

Ví dụ

host.docker.internal

Ví dụ yêu cầu

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

Tệp và dữ liệu

Volume

Volume

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

Ý nghĩa

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

Khi dùng

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

Lưu ý

Removing volumes can delete persistent data.

Ví dụ

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

Ví dụ yêu cầu

Tell me which compose volumes store persistent data.

Tệp và dữ liệu

Bind mount

Bind mount

./src:/app/src

Ý nghĩa

Connects a host folder directly into a container path.

Khi dùng

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

Lưu ý

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

Ví dụ

./src:/app/src

Ví dụ yêu cầu

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

Tệp và dữ liệu

environment

environment

DATABASE_URL=postgres://db:5432/app

Ý nghĩa

Configuration values passed into a container.

Khi dùng

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

Lưu ý

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

Ví dụ

DATABASE_URL=postgres://db:5432/app

Ví dụ yêu cầu

Check which environment variables this container needs.

Gỡ lỗi

port is already allocated

port is already allocated

Bind for 0.0.0.0:3000 failed: port is already allocated

Ý nghĩa

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

Khi dùng

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

Ví dụ

Bind for 0.0.0.0:3000 failed: port is already allocated

Ví dụ yêu cầu

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

Gỡ lỗi

container name is already in use

container name is already in use

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

Ý nghĩa

A container with the same name already exists.

Khi dùng

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

Ví dụ

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

Ví dụ yêu cầu

Resolve this container name conflict without deleting data unexpectedly.

Gỡ lỗi

Build cache

Build cache

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

Ý nghĩa

Docker can reuse previous build layers to make builds faster.

Khi dùng

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

Lưu ý

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

Ví dụ

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

Ví dụ yêu cầu

Explain whether Docker build cache could be hiding my change.

An toàn

docker system prune

docker system prune

docker system prune

Ý nghĩa

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

Khi dùng

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

Lưu ý

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

Ví dụ

docker system prune

Ví dụ yêu cầu

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