Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Docker Compose: A Mini Orchestration Tool for Local Development
56%

Using Dockerfile with Docker Compose

In the previous example, we used the wordpress image to create the wordpress service. However, we can use a Dockerfile to build a custom image and use it in Docker Compose.

Let's see an example. Start by creating a folder called $HOME/apache/ and a new file called Dockerfile:

# Create a new folder
mkdir -p $HOME/apache/ && cd $HOME/apache/

# Create a new file called Dockerfile
cat << EOF > Dockerfile
FROM ubuntu:24.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y apache2
RUN echo "Hello World. I'm Apache" > /var/www/html/index.html
EXPOSE 80
CMD apachectl -D FOREGROUND
EOF

Create the Docker Compose file:

cat << EOF > $HOME/apache/docker-compose.yml
services:
  web:
    build: .
    ports:
      - "80:80"
EOF

As you can see, instead of using the image

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More