Bird
Raised Fist0
Dockerdevops~5 mins

Building images with docker build - Commands & Configuration

Choose your learning style10 modes available
Introduction
Building a Docker image packages your app and its environment into a single file. This lets you run your app anywhere without setup problems.
When you want to package your web app with all its software so it runs the same on any computer.
When you need to share your app with teammates or deploy it to a server easily.
When you want to test your app in a clean environment without installing anything on your machine.
When you want to update your app and create a new version to deploy.
When you want to automate app setup in continuous integration pipelines.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

FROM sets the base image with Python 3.11 slim version.

WORKDIR sets the working directory inside the image.

COPY requirements.txt . copies the file with dependencies.

RUN pip install installs the dependencies.

COPY . . copies all app files into the image.

CMD defines the command to run the app.

Commands
This command builds a Docker image named 'my-app-image' with tag '1.0' using the Dockerfile in the current directory. It packages your app and its environment.
Terminal
docker build -t my-app-image:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 4.096kB Step 1/6 : FROM python:3.11-slim ---> 123abc456def Step 2/6 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/6 : COPY requirements.txt . ---> Using cache ---> 345ghi678jkl Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in abc123def456 Collecting flask Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container abc123def456 ---> 901mno234pqr Step 5/6 : COPY . . ---> 567stu890vwx Step 6/6 : CMD ["python", "app.py"] ---> Running in def789ghi012 Removing intermediate container def789ghi012 ---> 345yz123abc Successfully built 345yz123abc Successfully tagged my-app-image:1.0
→
-t - Assigns a name and tag to the image for easy reference
Lists all Docker images on your machine so you can verify your new image was created.
Terminal
docker images
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app-image 1.0 345yz123abc 10 seconds ago 120MB
Runs a container from your newly built image to check that your app starts correctly. The --rm flag removes the container after it stops.
Terminal
docker run --rm my-app-image:1.0
Expected OutputExpected
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
→
--rm - Automatically removes the container after it stops to keep your system clean
Key Concept

If you remember nothing else from this pattern, remember: docker build creates a reusable image from your app and environment using a Dockerfile.

Common Mistakes
Not running the docker build command in the directory containing the Dockerfile.
Docker cannot find the Dockerfile and fails to build the image.
Always run docker build in the folder where your Dockerfile is located or specify the correct path.
Forgetting to tag the image with -t, resulting in an image with a random ID only.
It becomes hard to find and run the image later without a meaningful name.
Use the -t flag to give your image a clear name and version tag.
Not copying all necessary files into the image, causing runtime errors.
Your app may fail because required files or dependencies are missing inside the image.
Use COPY commands in the Dockerfile to include all needed files.
Summary
Write a Dockerfile to define your app environment and files.
Use docker build -t name:tag . to create an image from the Dockerfile.
Check your image with docker images and run it with docker run.

Practice

(1/5)
1. What does the docker build command do?
easy
A. It creates a Docker image from a Dockerfile and context files.
B. It runs a Docker container from an image.
C. It deletes unused Docker images.
D. It lists all running Docker containers.

Solution

  1. Step 1: Understand the purpose of docker build

    The docker build command reads instructions from a Dockerfile and packages files from the build context into a new image.
  2. Step 2: Differentiate from other Docker commands

    Running containers, deleting images, or listing containers are done by other commands like docker run, docker rmi, and docker ps.
  3. Final Answer:

    It creates a Docker image from a Dockerfile and context files. -> Option A
  4. Quick Check:

    docker build = create image [OK]
Hint: Remember: build = create image from Dockerfile [OK]
Common Mistakes:
  • Confusing build with run command
  • Thinking build deletes images
  • Mixing build with container listing
2. Which of the following is the correct syntax to build a Docker image named myapp from the current directory?
easy
A. docker build . -name myapp
B. docker build myapp -t .
C. docker build -t myapp .
D. docker build --image myapp .

Solution

  1. Step 1: Identify the correct order and options for docker build

    The correct syntax uses -t to tag the image name, followed by the build context (here, current directory .).
  2. Step 2: Check each option for syntax correctness

    docker build -t myapp . correctly places -t myapp before the context .. Options A, B, and D misuse flags or order.
  3. Final Answer:

    docker build -t myapp . -> Option C
  4. Quick Check:

    docker build -t name context [OK]
Hint: Use -t before context folder to name image [OK]
Common Mistakes:
  • Placing image name after context
  • Using wrong flags like --image or -name
  • Omitting the build context folder
3. Given this Dockerfile in the current directory:
FROM alpine
RUN echo "Hello Docker" > /hello.txt

What will be the output of docker build -t testimage . if successful?
medium
A. An image named testimage is created with a file /hello.txt containing 'Hello Docker'.
B. A container runs and prints 'Hello Docker' to the terminal.
C. The build fails because echo is not a valid Docker command.
D. An image is created but /hello.txt is empty.

Solution

  1. Step 1: Understand Dockerfile instructions

    FROM alpine sets base image; RUN echo "Hello Docker" > /hello.txt creates a file inside the image.
  2. Step 2: Interpret the build command output

    The build creates an image named testimage with the file /hello.txt containing the text 'Hello Docker'. No container runs or prints output during build.
  3. Final Answer:

    An image named testimage is created with a file /hello.txt containing 'Hello Docker'. -> Option A
  4. Quick Check:

    RUN creates files in image, no output printed [OK]
Hint: RUN commands modify image, they don't print output during build [OK]
Common Mistakes:
  • Thinking RUN prints to terminal during build
  • Confusing image creation with container run
  • Assuming build fails due to shell commands
4. You run docker build -t myapp . but get the error: failed to read dockerfile: open Dockerfile: no such file or directory. What is the most likely cause?
medium
A. The build context folder is empty.
B. The -t option is used incorrectly.
C. Docker daemon is not running.
D. The Dockerfile is missing from the current directory.

Solution

  1. Step 1: Analyze the error message

    The error says Docker cannot find the Dockerfile in the build context folder (current directory).
  2. Step 2: Identify the cause

    If the Dockerfile is missing, Docker cannot build the image. The -t option and daemon status do not cause this specific error.
  3. Final Answer:

    The Dockerfile is missing from the current directory. -> Option D
  4. Quick Check:

    Missing Dockerfile = build fails with file not found [OK]
Hint: Check Dockerfile exists in build folder if build fails [OK]
Common Mistakes:
  • Assuming wrong flag causes error
  • Not verifying Dockerfile presence
  • Ignoring daemon status for this error
5. You want to build a Docker image named webapp:v1 using a Dockerfile located in ./app folder, but your current directory is the parent folder. Which command correctly builds the image?
hard
A. docker build -t webapp:v1 ./app
B. docker build -t webapp:v1 -f ./app/Dockerfile ./app
C. docker build ./app -t webapp:v1
D. docker build -f app/Dockerfile -t webapp:v1 ./app

Solution

  1. Step 1: Understand build context and Dockerfile location

    The build context is the folder with the Dockerfile and files needed. Here, it's ./app.
  2. Step 2: Choose correct command syntax

    docker build -t webapp:v1 -f ./app/Dockerfile ./app uses -t webapp:v1 to tag, -f ./app/Dockerfile to specify Dockerfile path, and ./app as context, which is correct. docker build -t webapp:v1 ./app assumes Dockerfile is in context folder but does not specify path explicitly. docker build ./app -t webapp:v1 has wrong order. docker build -f app/Dockerfile -t webapp:v1 ./app has incomplete Dockerfile path.
  3. Final Answer:

    docker build -t webapp:v1 -f ./app/Dockerfile ./app -> Option B
  4. Quick Check:

    Tag with -t, specify Dockerfile with -f, context last [OK]
Hint: Tag with -t, specify Dockerfile path with -f, context last [OK]
Common Mistakes:
  • Wrong order of options and context
  • Incorrect Dockerfile path with -f
  • Using current folder when Dockerfile is elsewhere
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard