Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.
Docker allows you to build containers using a Dockerfile. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to build an image.
A Dockerfile consists of various commands and arguments listed successively to automatically perform actions on a base image in order to create a new one. It helps us to avoid issuing the command everytime while running container.
You can build the Docker image using one of the following two options:
- Interactively launch a BASH shell under the Ubuntu Base image, install Apache and its dependencies, and then save the image.
- Build the Docker image using Dockerfile with the web site included.
In this tutorial, I will explain how to create a Dockerfile, install the required Apache packages, add the necessary content and then build image.
Docker image for Apache Web Server. Make a new Dockerfile that extends from this image: FROM sismics/apache2:latest Add your documents to the web root: RUN rm -fr /var/www/html/. ADD www /var/www/html Start a container: docker run -d -h apache2 -name apache2 -p 80:80 -restart=always sismics/apache2. See full list on hub.docker.com.
Requirements
Ubuntu Server 14.04 with Docker installed on your system.
Creating a Dockerfile
A Dockerfile is a text file that has a series of instructions to build an image. It supports a set of commands that we need to use in our Dockerfile.
There are several commands supported like FROM, CMD, ENTRYPOINT, VOLUME, ENV and many more. Each and every instruction set in the Dockerfile adds an additional layer to the image and then performs a commit.
Here, we will create a Dockerfile to create an image to install the Apache Web Server container.
To do this, we will need to create a file named Dockerfile using any text editor:
sudo nano Dockerfile
Add the following content which includes the commands and arguments for the Apache Web Server Container.
Now, save and close the file.
In the above Dockerfile, the first parameter FROM
tells Docker what the source of our image is, in this example we're using Ubuntu image.
A RUN
parameter executes a series of commands inside the image to install package. Here we are updating the Ubuntu repository and installing Apache and other dependencies.
The EXPOSE
parameter set's the default Apache port 80
so that the website will be available normally. Then, the ENTRYPOINT
is set to /usr/sbin/apache2ctl so that the Apache Server will execute.
Docker Apache Web Server Tutorial
Building an Image using Dockerfile
Now, after we finish creating our Dockerfile for the Apache container, we are ready to create our first Apache Web Server images with docker.
We'll need to run the following command in our current working base directory to build an image.
sudo docker build -t ubuntu:Apache_Server .
You should see the following output:
Note:
-> The -t
parameter used to tag the Docker image. The .
parameter is used to specify the location of the Dockerfile that we created.
After building Apache_Server image, run docker images command:
sudo docker images | grep -i Apache_Server
You should see the Apache_Server image listed in the output as shown below:
Creating a Docker Container
Using the image we have built, we will now proceed to create a container running an Apache instance inside, using a name of our choice. Here we will use Apache_Instance.
Run following command to create a container:
sudo docker run --name Apache_Instance -p 80:80 -d ubuntu:Apache_Server
Finally, we have created our Apache Container and it is forwarded to port 80
. Now to check if its running properly or not we can run docker ps command.
sudo docker ps
You should see the following output:
After setting everything up, you can verify the Apache Web Server by typing the url http://your.container.ipaddress:80
or http://localhost:80
in your web browser.
Enjoy......
Docker Apache Web Server Example
Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.
Comments are closed.