Dockerfile Reference Guide

Dockerfile Commands Overview

Dockerfiles are text-based scripts that define how to build Docker images. Below is a comprehensive reference of essential Dockerfile instructions.

Core Instructions

ARG variable_name[=default_value]
# Defines build-time variables accessible via --build-arg during docker build

FROM base_image[:tag] [@digest] [AS build_stage]
# Specifies the base image for your build

LABEL maintainer="name" version="1.0"
# Adds metadata labels to the generated image

EXPOSE 8080 [8081/tcp]
# Documents which ports the container listens on

ENV APP_HOME=/app
# Sets environment variables in the container

ENTRYPOINT ["executable", "param1", "param2"]
# Exec form - process runs directly with PID 1
ENTRYPOINT command param1 param2
# Shell form - executed within /bin/sh

VOLUME ["/data", "/logs"]
# Creates mount points for external storage

USER username_or_uid
# Sets the runtime user for subsequent commands

WORKDIR /path/to/directory
# Establishes the working directory for commands

ONBUILD RUN npm install
# Triggers when this image is used as a base image

STOPSIGNAL SIGTERM
# Defines the signal sent to stop the container

HEALTHCHECK CMD curl --fail http://localhost/ || exit 1
# Configures container health verification

SHELL ["/bin/bash", "-c"]
# Overrides default shell for RUN, CMD, ENTRYPOINT

RUN apt-get update && apt-get install -y curl
# Executes commands during image build

CMD ["java", "-jar", "app.jar"]
# Default command executed when container starts
CMD command param1 param2
CMD ["param1", "param2"]

ADD source destination
# Copies files with URL/tar extraction support

COPY source destination
# Simple file copy instruction

Practical Example: Spring Boot Application

Pull Base Image

docker pull openjdk:8-jre

Method 1: Using ADD Instruction

FROM openjdk:8-jre
LABEL maintainer="developer"
VOLUME /tmp
ADD application-1.0.0.jar app.jar
RUN touch /app.jar
EXPOSE 8080
ENV JAVA_OPTS="-Xmx512m"
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar"]

Method 2: Using COPY Instruction

FROM openjdk:8-jre
LABEL author="developer"
VOLUME /tmp
RUN mkdir -p /app
COPY application-1.0.0.jar /app/application.jar
EXPOSE 8080
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/application.jar"]

The EXPOSE directive allows Docker to map container ports to host ports when using the -P flag for random port assignment.

Build Commands

Build Image Syntax

docker build [OPTIONS] PATH

Build Using Current Directory

docker build -t myapp/demo:1.0 .

Build Using Specified Dockerfile

docker build -t myapp/demo:1.0 -f /path/to/Dockerfile .

Run Container

docker run -d -p 0.0.0.0::8080 --name myapp myapp/demo:1.0

This command runs the container in detached mode, mapping port 8080 to a random avialable host port.

Tags: docker dockerfile containerization OpenJDK spring-boot

Posted on Thu, 13 Aug 2026 16:45:24 +0000 by watts