Bird
Raised Fist0
Dockerdevops~5 mins

Building images in Compose in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want to create a custom container image for your app instead of using a ready-made one. Docker Compose lets you build these images automatically from a simple file, so you can run your app with all its settings in one step.
When you have your own app code and want to package it with its environment into a container.
When you want to share your app setup with teammates without manual steps.
When you need to customize an existing image by adding files or changing settings.
When you want to run multiple containers that depend on your custom-built image.
When you want to automate building and running your app with one command.
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  webapp:
    build:
      context: ./app
      dockerfile: Dockerfile
    ports:
      - "8080:80"

version: specifies the Compose file format version.

services: defines the containers to run.

webapp: is the service name.

build: tells Compose to build an image from the Dockerfile in the ./app folder.

ports: maps port 8080 on your computer to port 80 in the container.

Commands
This command builds the Docker image for the service defined in docker-compose.yml using the Dockerfile in the specified context folder.
Terminal
docker-compose build
Expected OutputExpected
Building webapp Step 1/4 : FROM nginx:alpine ---> 3f53bb00af94 Step 2/4 : COPY . /usr/share/nginx/html ---> Using cache ---> 7d9495d03763 Step 3/4 : EXPOSE 80 ---> Using cache ---> 8c2e066076a5 Step 4/4 : CMD ["nginx", "-g", "daemon off;"] ---> Using cache ---> 5d0da3dc9764 Successfully built 5d0da3dc9764 Successfully tagged webapp_webapp:latest
This command starts the container using the image just built and runs it in the foreground so you can see its output.
Terminal
docker-compose up
Expected OutputExpected
Creating network "webapp_default" with the default driver Creating webapp_webapp_1 ... done Attaching to webapp_webapp_1 webapp_webapp_1 | 172.17.0.1 - - [date] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1"
This command shows the status of the containers started by Compose, so you can check if your app is running.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- webapp_webapp_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command stops and removes the containers and network created by docker-compose up, cleaning up your environment.
Terminal
docker-compose down
Expected OutputExpected
Stopping webapp_webapp_1 ... done Removing webapp_webapp_1 ... done Removing network webapp_default
Key Concept

If you remember nothing else from this pattern, remember: Docker Compose can build your app's image from a Dockerfile automatically, so you don't have to build it manually before running.

Common Mistakes
Not specifying the build context or Dockerfile path correctly in docker-compose.yml
Docker Compose won't find the Dockerfile and fails to build the image.
Set the build context to the folder containing your Dockerfile and specify the Dockerfile name if it's not the default.
Running docker-compose up without building first when the image is not built
The container fails to start because the image does not exist.
Run docker-compose build first or use docker-compose up --build to build and start in one step.
Not mapping ports in docker-compose.yml when your app listens on a port
You cannot access your app from outside the container.
Add a ports section to map container ports to your machine's ports.
Summary
Define the build context and Dockerfile in docker-compose.yml under the build key.
Use docker-compose build to create the image from your Dockerfile.
Use docker-compose up to start the container from the built image.
Use docker-compose ps to check running containers and docker-compose down to stop and clean up.

Practice

(1/5)
1. What does the build key do in a Docker Compose file?
easy
A. It sets the network mode for the container.
B. It runs a container from an existing image.
C. It specifies environment variables for the container.
D. It tells Compose how to build a Docker image from a directory or Dockerfile.

Solution

  1. Step 1: Understand the role of build in Compose

    The build key tells Docker Compose to create an image from source files, usually by specifying a directory with a Dockerfile.
  2. Step 2: Compare with other keys

    Other keys like image specify existing images, and environment sets variables, so they do not build images.
  3. Final Answer:

    It tells Compose how to build a Docker image from a directory or Dockerfile. -> Option D
  4. Quick Check:

    build = build image [OK]
Hint: Remember: build = create image from source [OK]
Common Mistakes:
  • Confusing build with image key
  • Thinking build runs containers
  • Mixing build with environment variables
2. Which of the following is the correct syntax to specify a custom Dockerfile in a Compose service?
easy
A. build: ./app dockerfile: Dockerfile.custom
B. build: path: ./app file: Dockerfile.custom
C. build: context: ./app dockerfile: Dockerfile.custom
D. build: source: ./app file: Dockerfile.custom

Solution

  1. Step 1: Recall correct keys under build

    The correct keys are context for directory and dockerfile for custom Dockerfile name.
  2. Step 2: Check each option

    build: context: ./app dockerfile: Dockerfile.custom uses context and dockerfile correctly. Others use invalid keys like path, file, or source.
  3. Final Answer:

    build: context: ./app dockerfile: Dockerfile.custom -> Option C
  4. Quick Check:

    Use context and dockerfile keys [OK]
Hint: Use 'context' and 'dockerfile' keys exactly [OK]
Common Mistakes:
  • Using 'path' or 'file' instead of 'context' and 'dockerfile'
  • Indentation errors under build
  • Putting dockerfile outside build block
3. Given this Compose snippet:
services:
  web:
    build:
      context: ./webapp
    image: mywebapp:latest
What happens when you run docker compose up --build?
medium
A. It builds the image from ./webapp and tags it as mywebapp:latest, then starts the container.
B. It pulls the image mywebapp:latest from Docker Hub and runs it.
C. It builds the image but does not tag it as mywebapp:latest.
D. It starts the container without building or pulling any image.

Solution

  1. Step 1: Understand build and image keys together

    The build key builds an image from the given context. The image key tags the built image with the given name.
  2. Step 2: Effect of --build flag

    The --build flag forces Compose to build the image before starting containers.
  3. Final Answer:

    It builds the image from ./webapp and tags it as mywebapp:latest, then starts the container. -> Option A
  4. Quick Check:

    build + image = build and tag image [OK]
Hint: build + image tags image before running [OK]
Common Mistakes:
  • Thinking image pulls from Docker Hub automatically
  • Assuming build does not tag image
  • Ignoring --build flag effect
4. You wrote this Compose service:
services:
  api:
    build: ./api
    dockerfile: Dockerfile.api
When running docker compose build, you get an error. What is wrong?
medium
A. You must specify context instead of build.
B. The dockerfile key must be inside the build block, not at the same level.
C. The build key cannot be a string; it must be a dictionary.
D. Docker Compose does not support custom Dockerfile names.

Solution

  1. Step 1: Check Compose syntax for build and dockerfile

    The dockerfile key must be nested inside the build dictionary, not at the same level as build.
  2. Step 2: Identify the error cause

    Here, build is a string, and dockerfile is separate, causing Compose to fail parsing.
  3. Final Answer:

    The dockerfile key must be inside the build block, not at the same level. -> Option B
  4. Quick Check:

    dockerfile inside build block [OK]
Hint: Put dockerfile under build, not beside it [OK]
Common Mistakes:
  • Placing dockerfile outside build block
  • Using build as string with dockerfile key separately
  • Assuming dockerfile key is global
5. You want to build two services in Compose, frontend and backend, each with different Dockerfiles in the same directory ./app. How do you configure the build section for both services correctly?
hard
A. Use context: ./app for both, and specify different dockerfile names under each service's build.
B. Use different context paths for each service pointing to the same directory.
C. Use one service with build and the other with image only.
D. You cannot build two images from the same directory with different Dockerfiles.

Solution

  1. Step 1: Understand build context and dockerfile usage

    You can use the same context directory but specify different dockerfile names to build different images from the same folder.
  2. Step 2: Check options for multiple services

    Use context: ./app for both, and specify different dockerfile names under each service's build. correctly uses the same context with different Dockerfiles. Use different context paths for each service pointing to the same directory. is redundant, C limits building, and D is false.
  3. Final Answer:

    Use context: ./app for both, and specify different dockerfile names under each service's build. -> Option A
  4. Quick Check:

    Same context + different dockerfile names [OK]
Hint: Same folder, different dockerfile names per service [OK]
Common Mistakes:
  • Trying to use different contexts for same folder
  • Thinking multiple Dockerfiles in one folder is unsupported
  • Not specifying dockerfile key for each service
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