Skip to content

Docker Dockerfile API

Dockerfile instructions for building reproducible container images layer by layer.

1 class · 8 methods

Instructions

8 methods

Declarative directives parsed top-to-bottom to assemble a container image.

FROM image[:tag] [AS stage]

Sets the base image for subsequent instructions. Must be the first non-arg instruction.

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", ...]

Executes a command in a new layer on top of the current image and commits the result.

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

Copies new files or directories from the build context into the image filesystem.

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

Sets the working directory for RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it.

Parameters

NameTypeDescription
pathstringAbsolute path; created if it does not exist.

Returns

void

Example

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

Sets an environment variable that persists in the built image and containers run from it.

Parameters

NameTypeDescription
KEYstringVariable name.
VALUEstringVariable value.

Returns

void

Example

docker
ENV NODE_ENV=production
EXPOSE port[/protocol]

Documents which ports the container listens on at runtime. Does not publish the port.

Parameters

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

Returns

void

Example

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

Provides default executable and arguments for the container. Overridable at runtime. Only the last CMD takes effect.

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"]