0
0
Dockerdevops~5 mins

Building images in Compose in Docker - Commands & Configuration

Choose your learning style9 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.