Build a custom Docker image with Apache2

Build a custom Docker image with Apache2

·

2 min read

In this tutorial, we'll guide you through the process of creating a Dockerfile on App Server in the DC to build a custom Docker image with Apache2 installed and configured to work on port 8086.

Prerequisites:

  • Access to App Server.

  • Basic knowledge of Docker and Dockerfile syntax.

Task Overview:

The task involves creating a Dockerfile named Dockerfile in the directory /opt/docker on App Server. The Dockerfile should use the Ubuntu base image, install Apache2, and configure it to work on port 8086.

1. SSH into the Host Server:

First, establish an SSH connection to App Server where you'll create the Dockerfile.

ssh user@app_server_ip

Replace user with your username and app_server_ip with the IP address of App Server.

2. Create the Dockerfile:

Navigate to the /opt/docker directory and create a file named Dockerfile using a text editor like vi or nano.

cd /opt/docker
vi Dockerfile

Paste the following content into the Dockerfile:

FROM ubuntu

# Set non-interactive mode during package installation
ARG DEBIAN_FRONTEND=noninteractive

# Update package lists and install Apache2
RUN apt-get update -y && apt-get install -y apache2

# Configure Apache2 to work on port 8086
RUN sed -i "s/80/8086/g" /etc/apache2/ports.conf

# Expose port 8086 to the host
EXPOSE 8086

# Start Apache2 service in the foreground
CMD ["apachectl", "-D", "FOREGROUND"]

This Dockerfile specifies to use the Ubuntu base image, updates package lists, installs Apache2, configures it to work on port 8086, exposes port 8086 to the host, and starts the Apache2 service in the foreground.

3. Build the Docker Image:

Once you've saved the Dockerfile, you can build the Docker image named apache2 using the following command:

docker build -t apache2 /opt/docker

This command builds the Docker image based on the Dockerfile in the /opt/docker directory and tags it as apache2.

Conclusion:

In this tutorial, you learned how to write a Dockerfile to create a custom Docker image with Apache2 installed and configured to work on port 8086. By following the provided steps, you can efficiently build Docker images tailored to specific project requirements, facilitating easier deployment and management of applications in Docker containers.