Build the service images
In the previous step, you defined your services using existing Docker images in the docker-compose.yaml
file. In this step, you will learn how to build custom Docker images for your services using Dockerfiles and then integrate them into your docker-compose.yaml
file.
First, let's create a simple directory for our web service and a Dockerfile within it. Navigate to the project directory if you are not already there:
cd ~/project
Create a directory named web
and navigate into it:
mkdir web
cd web
Now, create a Dockerfile named Dockerfile
inside the web
directory using the nano
editor:
nano Dockerfile
Paste the following content into the Dockerfile
:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile does the following:
FROM ubuntu:latest
: Starts from the latest Ubuntu base image.
RUN apt-get update && apt-get install -y nginx
: Updates the package list and installs Nginx.
COPY index.html /var/www/html/
: Copies an index.html
file (which we will create next) into the Nginx webroot directory.
EXPOSE 80
: Exposes port 80 on the container.
CMD ["nginx", "-g", "daemon off;"]
: Specifies the command to run when the container starts, which is to start Nginx in the foreground.
Save the Dockerfile by pressing Ctrl + X
, then Y
, and Enter
.
Now, let's create the index.html
file that the Dockerfile copies. Stay in the ~/project/web
directory and create the file:
nano index.html
Paste the following simple HTML content into index.html
:
<!doctype html>
<html>
<head>
<title>Hello from Docker!</title>
</head>
<body>
<h1>Welcome to my Dockerized Nginx!</h1>
<p>This page is served from a custom Docker image.</p>
</body>
</html>
Save the index.html
file by pressing Ctrl + X
, then Y
, and Enter
.
Now, navigate back to the project root directory where your docker-compose.yaml
file is located:
cd ~/project
We need to modify the docker-compose.yaml
file to build the web
service from the Dockerfile we just created instead of using a pre-built image. Open the docker-compose.yaml
file for editing:
nano docker-compose.yaml
Modify the web
service definition to use the build
instruction instead of image
:
version: "3.8"
services:
web:
build: ./web
ports:
- "80:80"
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: mysecretpassword
Here, build: ./web
tells Docker Compose to build the image for the web
service using the Dockerfile located in the ./web
directory relative to the docker-compose.yaml
file.
Save the modified docker-compose.yaml
file by pressing Ctrl + X
, then Y
, and Enter
.
Now, you can build the images defined in your docker-compose.yaml
file using the docker-compose build
command. Since Docker Compose is not pre-installed, you need to install it first.
sudo apt-get update
sudo apt-get install docker-compose-plugin -y
After installing the Docker Compose plugin, you can use the docker compose
command (note the space instead of a hyphen in newer versions).
docker compose build
This command will read your docker-compose.yaml
file and build the image for the web
service based on the Dockerfile
in the ./web
directory. It will also pull the postgres:latest
image for the db
service if it's not already present. You will see output indicating the build process for the web
image.
After the build is complete, you can list the Docker images on your system to see the newly built image.
docker images
You should see an image with a name related to your project directory and the service name (e.g., project-web
) and the postgres
image.