Lesson 1 · Unit 02
The Dockerfile
FROM, WORKDIR, COPY, RUN, ENV, EXPOSE, CMD and ENTRYPOINT
What you practise
FROM,WORKDIR,COPY,RUN,ENV,EXPOSE,CMDagainstENTRYPOINT, build context
Try a question from this lesson
Pick one
This recipe builds an image for a web server that runs until stopped. Which line cannot be something the build itself carries out?
FROM python:3-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]Hint
A server never finishes. Which step would keep a build waiting for ever?
Correct answer
CMD ["python", "app.py"]
Why
If the build started the server, it would never end. CMD only records the command a container will start with. The other lines do their work at build time, and their results are saved in the image.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A recipe, top to bottom
FROM names the image to start from. WORKDIR sets the directory for later steps and creates it if needed. COPY brings files from your project into the image. RUN executes a command during the build.
docker build -t api . reads the Dockerfile and builds an image named api. The final . is the build context: the directory whose files COPY can read.
Concept
Shell form and exec form
A command can be written two ways.
Shell form is plain text. Docker hands it to /bin/sh -c, so $VARIABLES expand, but a shell now sits between Docker and your app.
Exec form is a JSON array with double quotes. The program is started directly, with exactly those arguments and no shell.
# shell form
CMD python app.py
# exec form
CMD ["python", "app.py"]Concept
ENTRYPOINT and CMD
ENTRYPOINT is the fixed part of the start command. CMD supplies default arguments after it.
Anything written after the image name in docker run replaces CMD. ENTRYPOINT stays.
With only a CMD, those arguments replace the whole command.
ENTRYPOINT ["python", "tool.py"]
CMD ["--help"]
# docker run tool -> --help
# docker run tool --fast -> --fast9 more exercises in the app
- 1× match the pairs
- 2× type the answer
- 3× multiple choice
- 1× true or false
- 1× fill the blanks
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.