Skip to content

Docker Dockerfile API

Docker 命令行界面,用于构建、传输和运行容器及镜像。

1 class · 8 methods

Commands

8 methods

用于容器和镜像生命周期的顶层 docker 子命令。

FROM image[:tag] [AS stage]

从镜像创建新容器并启动,运行给定命令。

Parameters

NameTypeDescription
imagestringBase image name.
tagstringImage tag (defaults to latest).
stagestringOptional name for a multi-stage build stage.

Returns

void

Example

docker
FROM node:18-alpine AS builder
RUN <command> | RUN ["exec", "arg1", ...]

从给定路径或 URL 中的 Dockerfile 构建镜像。

Parameters

NameTypeDescription
commandstringShell command (shell form) or JSON array (exec form).

Returns

void

Example

docker
RUN apt-get update && apt-get install -y curl git
COPY [--chown=user:group] src... dest

从注册表下载镜像并本地存储。

Parameters

NameTypeDescription
srcstring | string[]Source path(s) in the build context.
deststringAbsolute or relative destination inside the container.
--chownstringOptional user:group ownership for the copied files.

Returns

void

Example

docker
COPY --chown=node:node ./src ./app/src
WORKDIR path

将本地镜像上传到注册表。

Parameters

NameTypeDescription
pathstringAbsolute path; created if it does not exist.

Returns

void

Example

docker
WORKDIR /app
RUN pwd   # /app
ENV KEY=VALUE

列出容器。默认仅显示运行中的容器。

Parameters

NameTypeDescription
KEYstringVariable name.
VALUEstringVariable value.

Returns

void

Example

docker
ENV NODE_ENV=production
EXPOSE port[/protocol]

在运行中的容器内运行新命令。

Parameters

NameTypeDescription
portnumberTCP/UDP port number.
protocolstringOptional 'tcp' (default) or 'udp'.

Returns

void

Example

docker
EXPOSE 8080/tcp
CMD ["exec", "arg1", ...] | CMD command arg1 ...

打印容器的 stdout/stderr 日志。

Parameters

NameTypeDescription
execstringExecutable to run.
argsstring[]Arguments passed to the executable.

Returns

void

Example

docker
CMD ["node", "server.js"]
ENTRYPOINT ["exec", "arg1", ...]

Configures the executable that always runs when the container starts. CMD arguments are appended.

Parameters

NameTypeDescription
execstringExecutable to run.
argsstring[]Fixed arguments appended to the executable.

Returns

void

Example

docker
ENTRYPOINT ["/usr/bin/node", "server.js"]