Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# syntax=docker/dockerfile:1
FROM php:7.0-apache
# syntax=docker/dockerfile:1 FROM php:7.0-apache
# 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
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 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
# 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
# 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 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
# 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
# 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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.

0

Leave a Comment