Show List

Docker File Commands

Here are the common Docker file commands:

FROM

Used to set the base image for the subsequent instructions. Examples:
  • FROM openjdk:8-alpine
  • FROM debian:sid-slim
  • FROM nginx:1.19.0-alpine
  • FROM openjdk:11.0.2-jre-slim-stretch
  • FROM node:16
  • FROM tomcat:8.0.43-jre8
FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another

MAINTAINER

To identify the maintainer person/team for the docker image. Example:
  • MAINTAINER itcodescanner.com

LABEL

Used to add meta data to the image. Example:
  • LABEL "info"="This build is for a Spring Boot Rest service"

USER

To set the user name and user group to use for running the image.
RUN useradd springuser
USER springuser

ARG

Used to pass some arguments to consecutive instructions. Example:
ARG JDK_VERSION=8-jdk-alpine
FROM openjdk:$JDK_VERSION

ENV

Used to set the environment variables. Example:
  • ENV USER_HOME=/usr/app/

Environment variable defined using ENV command can be used in the docker file itself and will also be available when the container is loaded with the image.

RUN

Run command is used to execute a command during image build. Examples:
  • RUN apk --update add openjdk8-jre : This would update the image base image provided and install JDK
  • RUN npm install

CMD

Sets the command to run when the container starts with the image. Examples:
  • CMD [ "node", "server.js" ] : This would run "node server.js" command to start the server in node.js environment.
  • CMD [ "node", "app.js" ]

If we specify executable in the ENTRYPOINT then we can use CMD to pass default parameters.  For example below command will print node version.
CMD ["-v"]
ENTRYPOINT ["node"]
If we are not using ENTRYPOINT then we can pass the executable and default parameters in the CMD command itself.

COPY

This will copy files from the folder where Dockerfile is placed to the docker image. Examples
  • COPY package*.json ./

File copy can be using COPY or ADD command. COPY is recommended because ADD has extra features and may result in some unexpected behavior. 

ADD

Used to add files from host file system to container file system. Examples
  • ADD sample.war /usr/local/tomcat/webapps/
  • ADD server.xml /usr/local/tomcat/conf/

WORKDIR

This is to set the working directory so that any ENTRYPOINT command will execute from here. Example:
COPY my-app.jar /dir/app/
WORKDIR /dir/app/
ENTRYPOINT exec java -jar my-app.jar
There can be multiple WORKDIR commands in the docker file. Subsequent WORKDIR gets appended to the path. For example after the two commands below the work directory would be /dir/app/user.
WORKDIR /dir/app
WORKDIR user

ENTRYPOINT

This is the executable tp start when the container boots. Example:
  • ENTRYPOINT exec java -jar my-app.jar
  • ENTRYPOINT ["java","-jar","my-app.jar"]


VOLUME

Used to create a mount on the host file system (not the container file system). So the information stored in this volume will be available even after the container is destroyed. Example:
  • VOLUME /filelocation

Comments

Comments in the docker file start with #. Example:
FROM adoptopenjdk/openjdk15:ubi
COPY target/*.jar app.jar
Expose 8080
# Run the Jar
CMD ["java", "-jar", "app.jar"]

    Leave a Comment


  • captcha text