☰ See All Chapters |
Docker RUN command
To set up a complete enterprise application you should install softwares like Java, Database, Queue systems, Servers, etc… After you pull base image, let’s say Ubuntu, you should install all necessary softwares to the base image Ubuntu. To install softwares RUN command can be used. Let us use Ubuntu as base image, install java using RUN command and set JAVA_HOME using ENV command. In this now we will run our spring boot app.
FROM ubuntu:latest # install packages RUN apt-get update && \ apt-get install -y curl \ wget \ openjdk-8-jdk ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"] |
Not just installing softwares, RUN command can be used to run any command. Suppose if you are setting up your application in linux OS, you do execute many commands from terminal, those all commands can be executed using RUN command. Hence RUN command is the real workhorse to setup docker image. In real time we use RUN command extensively to create any enterprise application images.
Each RUN command creates a layer on the existing image. Hence it is recommended to use as many as possible commands in one RUN command. If you are installing Java and Database try to install both in single RUN command. If you use two separate RUN commands each to install Java and Database, then it adds two layers on your image.
Docker RUN command syntax
The RUN command has two types of syntax, as shown below:
Shell Syntax
RUN <command> |
When this is executed in shell form it calls /bin/sh -c <command>
Example:
RUN apt-get install python3 |
This will be executed as below:
/bin/sh -c apt-get install python3
JSON Array Syntax
RUN ["executablecommand", "argument1", " argument2", ...] |
executablecommand : This is the executable command should be supported form the base image.
argument1, argument2…: Arguments for the executablecommand
Unlike the shell syntax, this type does not invoke /bin/sh -c.
Example:
RUN ["apt-get", "install", "python3"] |
All Chapters