Dockerfile

Dockerfile reference

FROM ubuntu:2404
MAINTAINER "Roman Akchurin <[email protected]>"
RUN /bin/bash -c "apt-get update"
RUN ["apt-get", "-y", "upgrade"]
RUN /bin/bash -c "apt-get -y dist-upgrade"
docker image build --help # Show help for docker image build
docker image build -t ubuntu-demo --no-cache . # Re-execute commands again

LABEL

The LABEL instruction in a Dockerfile is used for adding metadata to your Docker images. This metadata can provide information about the image, such as the author, version, description, and usage instructions. Follow a consistent naming convention for your labels. The common format is com.companyname.projectname.labelname.

LABEL com.example.version="1.0" com.example.maintainer="[email protected]"

Use standardized labels where applicable, such as those suggested by the OCI Image Specification. Ensure important labels, such as those for versioning, maintainer contact, and description, are well-documented for future reference and usage. For readability and to minimize the number of layers in the final image, combine multiple LABEL instructions into one. Use the backslash (\) character to break up lines.

LABEL org.opencontainers.image.title="My App" \
      org.opencontainers.image.description="A detailed description of my app" \
      org.opencontainers.image.version="1.0.0" \
      org.opencontainers.image.vendor="Example Inc." \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.authors="[email protected]" \
      org.opencontainers.image.url="https://example.com/myapp" \
      org.opencontainers.image.source="https://github.com/example/myapp"

Use build tools or scripts to automate the insertion and updating of labels, especially those that change frequently like version or build date.

sed -i "s/^LABEL version=.*\\(/LABEL version=\"\\)(git describe --tags --always)\"/" Dockerfile

Add labels that provide useful build and usage information, such as the build date, base image version, or any custom configurations. Do not include sensitive information such as passwords, tokens, or other credentials in labels.

LABEL build_date="2024-06-04" \
      base_image="python:3.9-alpine" \
      custom_config="enabled"

You can inspect the image after building it to view the labels.

docker image build -t test-image .
docker image inspect --format='{{json .Config.Labels}}' test-image

ENV

The environment variables are set using ENV instruction which persist across different runs from the resulting image. To view the values use docker inspect command, and to modify the variables use docker run --env key=value.

ENV MYSQL_ROOT_PASSWORD=test1234
docker image build -t test-image .
docker container run --rm test-image env

ARG

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get -qq install {your-package}