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?

  1. AAt build: pip install; at run: python app.py
  2. BAt build: pip install and python app.py; at run: python app.py
  3. 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.
Practice this topic Back to topic

Books on Cloud and Container Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Cloud and Container Basics questions

  1. An image named pinger, built from the Dockerfile above, is started with docker run pinger example.com. Which of the following is the command executed in the container?
  2. A container is started with the command above from the image nginx, a web server that listens on port 80. The host's IP address is 192.0.2.10, and there are no restrictions such as a firewall. Which of the following is the URL to use to reach this web server from a browser on another PC on the same network?
  3. Dockerfile (B) above was made from Dockerfile (A), which had RUN npm ci after COPY . ., by changing the order so that only the dependency files are copied first. npm ci takes 120 seconds, npm run build 30 seconds and COPY . . 2 seconds, and the time of the other instructions is negligible. The image is rebuilt 20 times a day after changing only the source code under src (package.json and package-lock.json are unchanged). When the cache from the previous build can be used, which of the following is how much shorter B is than A in total build time per day?
  4. In Docker, which of the following correctly describes named volumes and bind mounts, used to keep data written by a container?
  5. An image is pulled by specifying registry.example.com/team/web:1.4. Which of the following correctly gives the meaning of each part of this reference?