×
☰ See All Chapters

Docker ENTRYPOINT command

After docker image is created you will have to run image. You should define what should happen when you run your image. For example when you run your image it should start application servers, it should start Database services. Sometimes you may need to set environment specific settings before starting any services... so on. To execute your desired tasks when docker image is run you have to set your executable instructions from either CMD command or from ENTRYPOINT command. In our previous chapter Docker CMD Command we studied about CMD command. In this chapter we learn ENTRYPOINT command.

To run you image you should execute below docker command.

docker run <imagename>

Now when you image is run if you want to start your spring boot application you may have to create your dockerfile as below:

FROM ubuntu:latest

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

When docker run command is executed it runs the command java –jar /app.jar command on Ubuntu terminal.

 Below is another simple example, we just echo a message to console when docker image is run.

FROM ubuntu:latest

ENTRYPOINT ["echo", "Hello World"]

docker-entrypoint-command-0
 

Instructions set from ENTRYPOINT command cannot be overridden by providing instructions to docker run command. If any parameters are provided in docker run command, all those are appended to ENTRYPOINT command parameters.

docker-entrypoint-command-1
 

Docker ENTRYPOINT command syntax

The ENTRYPOINT command has three types of syntax, as shown below:

Shell Syntax

ENTRYPOINT <command>

Example:

ENTRYPOINT echo "Hello World"

JSON Array Syntax

ENTRYPOINT ["executablecommand", "argument1", " argument2", ...]

executablecommand : This is the executable command should be supported form the base image.

argument1, argument2…: Arguments for the executablecommand

Example:

ENTRYPOINT ["echo", "Hello World"]

JSON Array Syntax with default parameters from CMD instruction

The third type of syntax is similar to the previous type. However, in this type we set the default parameters from CMD instruction. Please read Docker CMD command chapter from here.

CMD ["argument1", " argument2", ...]

ENTRYPOINT ["executablecommand"]

Example:

FROM ubuntu:latest

CMD "Hello World"

ENTRYPOINT ["echo"]

docker-entrypoint-command-2
 

All Chapters
Author