Cloud and Container Basics
FROM python:slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]For the Dockerfile above, which of the following correctly pairs the command executed when the image is built with docker build and the command executed when a container is started with docker run?
- AAt build: pip install; at run: python app.py
- BAt build: pip install and python app.py; at run: python app.py
- CAt build: nothing; at run: pip install and python app.py
Show answer and explanation
Answer: A. At build: pip install; at run: python app.py
RUN executes a command while the image is being built and saves the result (here, the installed libraries) in the image as a new layer. CMD only records, as part of the image configuration, the default command to execute when a container starts; it is not executed during the build. The libraries are therefore installed once, at build time, and python app.py runs every time a container starts. If a command is written after the image name in docker run, it replaces the contents of CMD.
- A. Correct. RUN executes at build time and CMD at start-up.
- B. CMD is not executed during the build. It only records the default command for start-up.
- C. RUN executes during the build and its result is saved in the image. Nothing is reinstalled each time a container starts.