0
0
Dockerdevops~30 mins

Building images in Compose in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Building images in Compose
📖 Scenario: You are working on a small web project that uses Docker Compose to manage services. You want to build a custom Docker image for your web service using a Dockerfile.
🎯 Goal: Learn how to define a Docker Compose file that builds a Docker image from a Dockerfile and runs a container from that image.
📋 What You'll Learn
Create a Dockerfile with a simple base image
Create a docker-compose.yml file that builds the image from the Dockerfile
Define a service in Compose that uses the built image
Run the Compose setup and verify the container runs
💡 Why This Matters
🌍 Real World
Building images in Compose is common when you want to customize your app environment and easily run multiple services together.
💼 Career
DevOps engineers and developers use Docker Compose to manage multi-container applications and automate image builds.
Progress0 / 4 steps
1
Create a Dockerfile
Create a file named Dockerfile with these exact contents:
FROM alpine:latest
CMD ["echo", "Hello from custom image"]
Docker
Need a hint?

The Dockerfile must start with FROM alpine:latest and end with CMD ["echo", "Hello from custom image"].

2
Create docker-compose.yml with build config
Create a file named docker-compose.yml with a service called web that builds the image from the current directory using the build key.
Docker
Need a hint?

The Compose file must have services: with a web: service that uses build: . to build from the current folder.

3
Add ports mapping to the service
In the docker-compose.yml file, add a ports section under the web service that maps port 8080 on the host to port 80 in the container.
Docker
Need a hint?

Indent the ports: section under web: and add - "8080:80" to map ports.

4
Run docker-compose and show container output
Run docker-compose up --build --abort-on-container-exit in the terminal to build the image and start the container. Then, write a command to display the container logs using docker-compose logs web.
Docker
Need a hint?

Use docker-compose up --build --abort-on-container-exit to build and run, then docker-compose logs web to see the output.