Engineering Tools for AI

程式設計

Docker

整理 Docker image、container、Dockerfile、Compose、port、volume、log 與常見問題的搜尋型指南。

33 個項目

核心概念

Docker

Docker

docker --version

意思

A tool for packaging and running applications in isolated containers.

使用時機

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

範例

docker --version

請求範例

Explain whether this project can be run with Docker.

核心概念

Docker daemon

Docker daemon

Cannot connect to the Docker daemon

意思

The background service that builds images and runs containers.

使用時機

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

範例

Cannot connect to the Docker daemon

請求範例

Check why Docker commands cannot connect to the daemon.

核心概念

Image

Image

node:20-alpine

意思

A reusable template used to create containers.

使用時機

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

範例

node:20-alpine

請求範例

Tell me which base image this Dockerfile uses.

核心概念

Container

Container

docker ps

意思

A running or stopped instance created from an image.

使用時機

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

範例

docker ps

請求範例

List running containers and explain what each one does.

核心概念

Registry

Registry

docker pull postgres:16

意思

A server where Docker images are stored and downloaded from.

使用時機

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

範例

docker pull postgres:16

請求範例

Explain where this image will be downloaded from.

Dockerfile

Dockerfile

Dockerfile

Dockerfile

意思

A file that defines how to build a Docker image.

使用時機

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

範例

Dockerfile

請求範例

Read this Dockerfile and summarize the build steps.

Dockerfile

FROM

FROM

FROM node:20-alpine

意思

The Dockerfile instruction that chooses the base image.

使用時機

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

注意

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

範例

FROM node:20-alpine

請求範例

Explain what this FROM image includes.

Dockerfile

WORKDIR

WORKDIR

WORKDIR /app

意思

Sets the folder inside the image where following commands run.

使用時機

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

範例

WORKDIR /app

請求範例

Explain which folder this Dockerfile uses as the app directory.

Dockerfile

COPY

COPY

COPY package.json package-lock.json ./

意思

Copies files from the build context into the image.

使用時機

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

範例

COPY package.json package-lock.json ./

請求範例

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

Dockerfile

RUN

RUN

RUN npm ci

意思

Runs a command while building the image.

使用時機

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

注意

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

範例

RUN npm ci

請求範例

Separate Dockerfile build-time commands from container startup commands.

Dockerfile

CMD

CMD

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

意思

Defines the default command that runs when the container starts.

使用時機

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

範例

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

請求範例

Tell me what command this container starts with.

Dockerfile

EXPOSE

EXPOSE

EXPOSE 3000

意思

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

使用時機

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

注意

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

範例

EXPOSE 3000

請求範例

Explain whether this exposed port is actually published.

Dockerfile

.dockerignore

.dockerignore

node_modules

意思

A file that excludes paths from the Docker build context.

使用時機

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

範例

node_modules

請求範例

Check whether this .dockerignore excludes unsafe or unnecessary files.

Dockerfile

Build context

Build context

docker build -t my-app .

意思

The folder sent to Docker during image build.

使用時機

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

注意

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

範例

docker build -t my-app .

請求範例

Explain what files are available to this Docker build.

Docker Compose

Docker Compose

Docker Compose

docker compose up

意思

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

使用時機

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

範例

docker compose up

請求範例

Explain what services this compose file starts.

Docker Compose

services

services

services: web: db:

意思

Named app parts defined in a compose file.

使用時機

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

範例

services: web: db:

請求範例

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

Docker Compose

docker compose up

docker compose up

docker compose up -d

意思

Starts the services defined in a compose file.

使用時機

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

範例

docker compose up -d

請求範例

Start the compose services and show which ones are running.

Docker Compose

docker compose down

docker compose down

docker compose down

意思

Stops and removes containers created by compose.

使用時機

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

注意

Named volumes usually remain unless you explicitly remove volumes.

範例

docker compose down

請求範例

Stop this compose stack without deleting persistent data.

指令

docker build

docker build

docker build -t my-app .

意思

Builds a Docker image from a Dockerfile and context.

使用時機

Use it after writing a Dockerfile or changing app dependencies.

範例

docker build -t my-app .

請求範例

Build this Docker image and summarize any build errors.

指令

docker run

docker run

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

意思

Creates and starts a container from an image.

使用時機

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

範例

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

請求範例

Run this image and explain the browser URL.

指令

docker ps

docker ps

docker ps

意思

Lists running containers.

使用時機

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

範例

docker ps

請求範例

Show running containers and their published ports.

指令

docker stop

docker stop

docker stop my-app

意思

Stops a running container.

使用時機

Use it before removing a container or freeing a port.

範例

docker stop my-app

請求範例

Stop the container using port 3000.

指令

docker logs

docker logs

docker logs my-app

意思

Prints logs from a container.

使用時機

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

範例

docker logs my-app

請求範例

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

指令

docker exec

docker exec

docker exec -it my-app sh

意思

Runs a command inside an already running container.

使用時機

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

注意

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

範例

docker exec -it my-app sh

請求範例

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

Port 與網路

ports

ports

3000:3000

意思

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

使用時機

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

注意

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

範例

3000:3000

請求範例

Explain which URL I should open for this ports setting.

Port 與網路

localhost in Docker

localhost in Docker

host.docker.internal

意思

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

使用時機

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

範例

host.docker.internal

請求範例

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

檔案與資料

Volume

Volume

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

意思

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

使用時機

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

注意

Removing volumes can delete persistent data.

範例

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

請求範例

Tell me which compose volumes store persistent data.

檔案與資料

Bind mount

Bind mount

./src:/app/src

意思

Connects a host folder directly into a container path.

使用時機

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

注意

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

範例

./src:/app/src

請求範例

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

檔案與資料

environment

environment

DATABASE_URL=postgres://db:5432/app

意思

Configuration values passed into a container.

使用時機

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

注意

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

範例

DATABASE_URL=postgres://db:5432/app

請求範例

Check which environment variables this container needs.

疑難排解

port is already allocated

port is already allocated

Bind for 0.0.0.0:3000 failed: port is already allocated

意思

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

使用時機

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

範例

Bind for 0.0.0.0:3000 failed: port is already allocated

請求範例

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

疑難排解

container name is already in use

container name is already in use

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

意思

A container with the same name already exists.

使用時機

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

範例

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

請求範例

Resolve this container name conflict without deleting data unexpectedly.

疑難排解

Build cache

Build cache

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

意思

Docker can reuse previous build layers to make builds faster.

使用時機

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

注意

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

範例

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

請求範例

Explain whether Docker build cache could be hiding my change.

安全操作

docker system prune

docker system prune

docker system prune

意思

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

使用時機

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

注意

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

範例

docker system prune

請求範例

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