- If you want to build a docker container which takes argument or needs an argument at the startup , how do you do that ?
- Example: We have a java program , which adds two number and we want users to pass arguments when they do docker run.. so how do we accomplish that when we containerize a program given below?
public static void main(String[] args) {
if (args.length == 2) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("Sum of "+args[0] +" & "+ args[1]+" is : " + sum);
}
}
- Let us first create the Dockerfile for this
FROM openjdk:8-jre-alpine
COPY out/production/DockerExampleArgsJava/ /tmp
WORKDIR /tmp
ENTRYPOINT ["java", "AddTwoNumber"]
- ENTRYPOINT in the above file is used to specify the command that you want to run at container startup, in this case it starts the main method of AddTwoNumber program by issuing
java AddTwoNumber
command - When you run the container with
docker run raje/add2number
command , it will give a message – Please input 2 integers - You can pass an argument by issuing the same command and passing argument, those will be passed to entry point example
docker run raje/add2number 10 100
- If you want to pass default values to the container at startup, you can do that by mentioning it in the Dockerfile like below
FROM openjdk:8-jre-alpine
COPY out/production/DockerExampleArgsJava/ /tmp
WORKDIR /tmp
ENTRYPOINT ["java", "AddTwoNumber","5","15"]
- If you run the program now with
docker run raje/add2number
command, it will show the sum - However, if you try to pass new arguments, that will not work. try d
ocker run raje/add2number 10 100
and the arguments will not be considered - This happens because ENTRYPOINT instructions that we provided in dockerfile are not ignored but instead are appended to the original instruction, so in this case program will be invoked like
java AddTwoNumber 5 15 10 100
- CMD is another option to provide instructions on what should be executed when we do a docker run.
- If we use the below instructions in Dockerfile
FROM openjdk:8-alpine
COPY out/production/DockerExampleArgsJava/ /tmp
WORKDIR /tmp
CMD ["java", "AddTwoNumber", "10","100"]
- Upon executing
docker run raje/add2number
- We will see the output:
Sum of 10 & 100 is : 110
- With ENTRYPOINT, when we passed new arguments, it got appended to the command , but with CMD you can overwrite the command , example
- if you want to pass new arguments, you can run :
docker run raje/add2number java AddTwoNumber 300 40
- You can completely replace the command by something like
docker run raje/add2number echo This is Test
- if you want to pass new arguments, you can run :
- If you have a scenario, where you want the default command to be the program that you mentioned and want uses to overwrite the arguments, then you can use a combination of ENTRYPOINT & CMD
FROM openjdk:8-alpine
COPY out/production/DockerExampleArgsJava/ /tmp
WORKDIR /tmp
ENTRYPOINT ["java", "AddTwoNumber"]
CMD ["10","200"]
- With the above docker file, when you issue command
docker run raje/add2number
it will outputSum of 10 & 200 is : 210
- You can overwrite the defaulr argument by issuing a command like
docker run raje/add2number 40 50
and it will outputSum of 40 & 50 is : 90
Hope this explains, the use of ENTRYPOINT & CMD in Docker.
Thanks !!