Dockerfile - Entrypoint

ENTRYPOINT

The Entrypoint instruction allows us to configure a container to run as executable.

Let's see an example.

gaurav@learning-ocean:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04
LABEL name="learning-ocean"
LABEL email="[email protected]"
RUN apt-get update && apt-get install -y python tree
ENTRYPOINT ["tree"]

In this docker file, we are installing the tree package and in the last step we are using the ENTRYPOINT instruction to define the tree command as an entry point.

So, when we build this image and run this container, the tree command will get triggered.

 docker container run -it myubuntu:81 .

Running the container:

 docker container run -it myubuntu:11

As soon as we run the container we see the output of the tree command:

Similarly, we can use ENTRYPOINT for other uses such as running a shell script when the container starts, let's see that as an example.

In the image below, we have a shell script file 'test.sh' which will print a couple of lines when executed, so let us try to execute this file from a container.

gaurav@learning-ocean:~/dockerfiles$ ls
Dockerfile  testproject  testproject.tar.gz  test.py  test.sh
gaurav@learning-ocean:~/dockerfiles$ cat test.py
print("Welcome to learning-ocean.com")
gaurav@learning-ocean:~/dockerfiles$ cat test.sh
#!/bin/bash
echo "this is first line"
echo "Welcome to $1"
gaurav@learning-ocean:~/dockerfiles$

Dockerfile:

gaurav@learning-ocean:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04
LABEL name="learning-ocean"
LABEL email="[email protected]"
RUN apt-get update && apt-get install -y python tree
COPY test.sh /tmp/
ENTRYPOINT ["/tmp/test.sh"]

In the above dockerfile we are copying the test.sh file into the container and defining this file as an entrypoint.

Now we will build this image and run the container to see what will happen.

docker container run -it myubuntu:81 .

Verify:

gaurav@learning-ocean:~/dockerfiles$ docker container run -it myubuntu:81
this is first line
Welcome to
gaurav@learning-ocean:~/dockerfiles$ docker container run -it myubuntu:81 learning-ocean.com
this is first line
Welcome to learning-ocean.com
gaurav@learning-ocean:~/dockerfiles$

In the image above we can see, as soon as we ran the container the script was executed and the lines were printed as expected.

We can also pass arguments to the script while running the container which is required by the script.

Similarly, the ENTRYPOINT instruction can be used for various purposes based on our needs.