Building a Docker Image with Ubuntu and Apache2
Docker has revolutionized the way we develop, deploy, and manage applications by providing a lightweight and portable containerization platform. In this blog post, we'll walk through the process of creating a Docker image that contains an Ubuntu base with Apache2 installed. We'll ensure that Apache2 starts automatically when the container is launched. Let's dive in!
Step 1: Setting Up Your Environment
Before we begin, make sure you have Docker installed on your system. You can download and install Docker from the official website (docker.com/get-started).
Step 2: Creating the Dockerfile
A Dockerfile is a text file that contains instructions to build a Docker image. Create a new directory for your project and create a file named Dockerfile
inside it. Open the Dockerfile
in a text editor and let's start building!
COPY
FROM ubuntu
RUN apt-get update
RUN DEBIAN_FRONTEND="noninteractive" apt-get install tzdata -y
RUN apt-get install apache2 -y
ENTRYPOINT apachectl -D FOREGROUND
# Automatically start Apache2 when the container starts CMD ["apache2ctl", "-D", "FOREGROUND"]
# Expose port 80 for Apache2
EXPOSE 80
Step 3: Building the Docker Image
Open a terminal, navigate to the directory where your Dockerfile
is located, and run the following command to build the Docker image:
COPY
docker build -t ubuntu-apache2 .
In the above command, -t
specifies the tag for the image (you can replace ubuntu-apache2
with any tag you prefer), and the .
at the end indicates that the build context is the current directory.
Step 4: Running the Docker Container
Once the image is built, you can create and run a container based on it:
COPY
docker run -d -p 8080:80 ubuntu-apache2
In this command, -d
runs the container in detached mode, -p
maps port 8080 on your host machine to port 80 in the container, and ubuntu-apache2
is the name of the image.
Step 5: Accessing Your Apache2 Server
Open your web browser and navigate to http://localhost:8080
. You should see the default Apache2 landing page, confirming that your Apache2 server is up and running inside the Docker container.
Conclusion
Congratulations! You've successfully created a Docker image containing an Ubuntu base with Apache2 installed. By following these steps, you can quickly deploy Apache2 or other services in isolated and consistent environments using Docker. Containerization simplifies the deployment process, enhances portability, and helps you avoid compatibility issues between different environments. Happy containerizing!
DO FOLLOW Kanishthika Singh
๐ Iโm interested in learning and working with the community, open source contributions and giving back to the community.
โท๏ธ I'm currently working with Kubernetes, Docker, AWS, Jenkins, Terraform, CI/CD, Git & GitHub.
๐๏ธ Iโm looking to collaborate on GitHub and open source projects related to Cloud computing,Devops , Data analysis.
๐ I regularly write articles.
๐ฌ Ask me about Data, DevOps, Linux-Ubuntu, Kubernetes, Docker, Ansible, Jenkins, CI/CD, Bash, Shell