Associate a Docker file to have a custom image (Apache + PHP + Python) inside Docker Compose

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Continuing with the post Deploy Apache Web Server with PHP using Docker Compose, we are going to learn how to associate a Dockerfile with a container defined in Docker Compose.

A Dockerfile allows us to have a customized image by including commands within a Docker image. This allows us, for example, to install multiple environments in a single image or simply configure our Docker image the way we want.

We will take the file from the previous post:

version: "3"
# Indicate the Docker Compose version we are using

# Then the services we are going to implement
services:

# Here we indicate the name we want
  my_apache_service:
    # APACHE image with PHP
    image: php:7.0-apache
    container_name: apache-container
    volumes:
      # Folder where we will store the web files: Docker's internal folder
      - ./www/:/var/www/html
    expose:
      # Port we want to expose to share it with other containers
      - 80
    ports:
      # Port on our machine : Port inside Docker (always 80 for Apache or 443 for SSL)
      - 80:80

We are going to install MySQLi to be able to use it within PHP and activate different PHP modules.

To do this, we are going to create a new folder within the project, and we will call it Dockerfile. Inside Dockerfile/, we are going to create the file apache_file so that it becomes Dockerfile/apache_file, and there is no need to add an extension to this file.

Now add the following to the apache_file file:

# syntax=docker/dockerfile:1
FROM php:7.0-apache

We start by including the syntax configuration used. Then we add the Docker image on which we want to install our environment, in this case, we use the one we indicated in our Docker Compose image: php:7.0-apache and add it like this: FROM php:7.0-apache.

Now we continue by indicating that we want to install MySQLi and activate the PHP header and rewrite modules to be able to use .htaccess.

# syntax=docker/dockerfile:1
FROM php:7.0-apache

# Install mysqli
RUN docker-php-ext-install mysqli

# Activate Apache modules:
RUN a2enmod headers
RUN a2enmod rewrite

# Restart Apache
RUN /etc/init.d/apache2 restart

As you can see, if we want to run a Bash command within the Dockerfile, we have to use the RUN keyword followed by the command we want to execute.

And now, for example, we can also install Python in our Docker image:

# syntax=docker/dockerfile:1
FROM php:7.0-apache

# Install mysqli
RUN docker-php-ext-install mysqli

# Activate Apache modules:
RUN a2enmod headers
RUN a2enmod rewrite

# Restart Apache
RUN /etc/init.d/apache2 restart

# INSTALL PYTHON:
RUN apt update
RUN apt install -y python-pip

As you can see, it’s easy to add new environments or configurations to a Docker image.

Now we just have to associate this file with Docker Compose. To do this, we are going to modify a few things to leave the docker-compose.yml file as follows:

version: "3"
# Indicate the Docker Compose version we are using

# Then the services we are going to implement
services:

# Here we indicate the name we want
  my_apache_service:
    # Indicate the folder where we keep the Dockerfile
    build: 
      context: ./Dockerfile
      # Indicate the dockerfile of this container
      dockerfile: apache_file
    # Remove this line that is replaced by the dockerfile
    # image: php:7.0-apache
   container_name: apache-container
volumes:
# Folder where we will store the web files: Docker's internal folder
- ./www/:/var/www/html
expose:
# Port we want to expose to share it with other containers
- 80
ports:
# Port on our machine : Port inside Docker (always 80 for Apache or 443 for SSL)
- 80:80

In this case, we remove the line #image: php:7.0-apache that defined our image and associate the Dockerfile by adding the build section.

This way, it will first create the image and execute the commands.

Here is the code to download from GitHub.

Leave a Comment