Docker, start to ship · Chapter 9 of 26
CMD vs ENTRYPOINT
By the end of this chapter you can write an image that behaves like a command: a fixed program, default arguments, and arguments you give it on the docker run line. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Images and Dockerfiles
- 5 tasks
- Docker
- In the all labs plan
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.
Why you need this
Ravi wants Tapri's price tool (tool/price.py) in an image, so the counter laptop can price an order without installing Python. Bunty built chai-09-price:0.1, and it ignores every order:
$ docker run --rm chai-09-price:0.1 masala 2
usage: price.py menu | price.py <chai> [cups]
In chapter 5 you replaced an image's arguments from the run line. That only works if the image was written to take them, and Bunty's was not. Rebuilding it for every order is no answer: the whole point is one image that takes the order as arguments.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Fix the price tool's Dockerfile and build chai-09-price:1.0
Fix Bunty's price tool, so the words you type after the image name reach Python.
1. Open tool/Dockerfile. Change its ENTRYPOINT to run python3 price.py in exec form: a JSON list, not the shell form.
2. Add a CMD in exec form too, with menu as the default argument.
3. Build the image chai-09-price:1.0 with the tool folder as the context.
To pass: chai-09-price:1.0 has an ENTRYPOINT that runs price.py, and a CMD. Neither of them is in the shell form.
- Task 2
Check it prints the menu and prices an order
Try your new image both ways.
1. Run a container from chai-09-price:1.0 with nothing after the image name. It prints the Tapri menu.
2. Run it again with ginger 3 after the image name. It prints the price of 3 ginger chai.
To pass: with no words, chai-09-price:1.0 prints the Tapri menu. With ginger 3, it prints 3 x ginger = Rs 60.
- Task 3
Price Meera's order into order.txt
Meera wants the price of her order in a file.
1. Run chai-09-price:1.0 with masala 4 right after the image name.
2. Save what the tool prints in order.txt, in the chapter folder.
3. Run cat order.txt to see what you saved.
To pass: order.txt has only the tool's one line for 4 masala, and nothing else. Your docker run must have masala 4 right after chai-09-price:1.0, typed in the chapter folder. A command with a typing mistake or an error does not count, so type it again.
- Task 4
Run another program in the image with --entrypoint
Run a different program in the same image.
1. Run a container from chai-09-price:1.0 with the --entrypoint option. Use ls as the new program.
2. After the image name, write the folder to list, /app.
3. Add --rm, so the container is deleted when it stops.
To pass: you ran docker run with --entrypoint and a chai-09-price image, in the chapter folder. It must work: a command with a typing mistake or an error does not count, so type it again.
- Task 5 · interview question
Interview: how CMD and ENTRYPOINT differ
Interview question: how are CMD and ENTRYPOINT different?
1. Write one or two sentences in your own words.
2. Say what each one is for.
3. Say what the words after the image name on docker run do to each one.
4. Save the sentences in answer.txt, in the chapter folder.
To pass: answer.txt names both ENTRYPOINT and CMD, and says which one the words after the image name replace.
What you will learn
- Shell form and exec form: the two ways to write CMD and ENTRYPOINT, and why only one passes your arguments on.
- ENTRYPOINT with CMD: a fixed program plus default arguments that the run line replaces.
- --entrypoint: running another program from the same image.
More in Images and Dockerfiles
Build "CMD vs ENTRYPOINT" yourself
Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.