Saturday 9 May 2020

Docker: ENTRYPOINT

ENTRYPOINT is used to configure a container that will run as an executable.

Step 1: Create a directory ‘sleeper’.

Step 2: Navigate to the directory ‘sleeper’. Create Dockerfile with below content.

Dockerfile
FROM ubuntu

ENTRYPOINT ["sleep"]

Step 3: Go to the directory where Dockerfile is located, execute below command to generate the image.

docker build -t sleep_me .
$docker build -t sleep_me .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/2 : ENTRYPOINT ["sleep"]
 ---> Running in 2564b9880aac
Removing intermediate container 2564b9880aac
 ---> 1e0e1c3cefc8
Successfully built 1e0e1c3cefc8
Successfully tagged sleep_me:latest

Step 4: Run the container.
$docker run sleep_me
sleep: missing operand
Try 'sleep --help' for more information.

As you see the output, sleep command expects an operand. Now pass an integer to the container while running. For example, below statement makes the container sleep for 5 seconds.

docker run sleep_me 5

How to provide a default value to the container ENTRYPOINT?
$docker run sleep_me
sleep: missing operand
Try 'sleep --help' for more information.

As you see above snippet, when I ran the container sleep_me, I end up in error like ‘missing operand’. We can solve this problem either by passing the argument while running the container or put a default value in Dockerfile. You can put the default value to ENTRYPOINT using CMD.

Note: If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

Dockerfile
FROM ubuntu

ENTRYPOINT ["sleep"]

CMD ["5"]

Rebuild the image.
$docker build -t sleep_me .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/3 : ENTRYPOINT ["sleep"]
 ---> Using cache
 ---> 1e0e1c3cefc8
Step 3/3 : CMD ["5"]
 ---> Running in fa704d7cab33
Removing intermediate container fa704d7cab33
 ---> 49913f3eae6d
Successfully built 49913f3eae6d
Successfully tagged sleep_me:latest

Now run the container ‘docker run sleep_me’, you will see container will sleep for 5 seconds (with the default value that we configured).

Previous                                                    Next                                                    Home

No comments:

Post a Comment