container
Installing NGINX on Docker
Installing NGINX on Docker
What is Docker?
Docker provides the capability to package a utility and its dependencies in a file that is called a container. Containers can be easily installed and ran on other machines without having to worry about differences between their machine and yours. This also bring the benefit of allowing more apps to run on a single server since the dependencies for the utility are contained in the container.
The Dockerfile
Dockerfiles are text files that tell the Docker tool how to build the image. The files could contain the commands to install and configure Nginx for example.
Setting up the Dockerfile
We first specify what we want to base our image off of, which the first line From ubuntu, specifies. This tells the Docker system to use the base Ubuntu image from Dockerhub. It will download the base Ubuntu image and layer the following commands on top of the base image.
From ubuntu
The next thing that we will want to do is to install Nginx. We will use APT to install Nginx. The next commands will be to install Nginx.
#Lets install NGINX
RUN apt-get -y update && apt-get -y install nginx
Creating index.html and default page
I have created a basic index.html file that we will to use to load as the default page. Create an index.html file in the same directory as the Dockerfile.
<b>Copied From physical machine.</b>
<h1> Hello, How are you?</h1>
Now lets create a default file for the Nginx configuration. The following file tells Nginx to use the /tmp directory to load the files. Create a file named default in the same directory as the Dockerfile.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /tmp;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
Copying Index.html and default to the container
Add the following lines to the DockerFile. This will tell the docker system to copy the index.html and default file into the container.
#Lets copy the local index.html to /tmp
COPY index.html /tmp/index.html
COPY default /etc/nginx/sites-available/default
Exposing Ports
We need to inform the system which ports we want to expose. In this case we will want to expose port 80 using the TCP protocol.
#lets expose port 80
EXPOSE 80/tcp
Starting Nginx and Keeping the container running
Next we will want to launch Nginx at launch, which can be accomplished with the CMD command. We also append tail -f /dev/null to keep the container alive.
CMD /usr/sbin/nginx && tail -f /dev/null
Here is the full Dockerfile document.
#Author: Michael Harrison
#Purpose: Showing how to create a docker image and manually install nginx.
#Pulls the base Ubuntu Image from Docker Hub
From ubuntu
#Lets install NGINX
RUN apt-get -y update && apt -y install nginx
#Lets copy the local index.html to /tmp
COPY index.html /tmp/index.html
COPY default /etc/nginx/sites-available/default
#lets expose port 80
EXPOSE 80/tcp
CMD /usr/sbin/nginx && tail -f /dev/null
Build the Image
The Dockerfile is now ready to be ran to build the image. Open up your terminal editor and image can be built with the docker build command.
docker build . -t harrison/first

Running the Image detached
We are now able to test the image that was just created. Docker run will launch the image that we specify. There are several options on how the system will run the container. We can run run the container in a detached mode with the -d command. -p 8090:80 maps the container port 80 to the host port 8090. harrison/first specifies that we want to run the container that we just created.
docker run -d -p 8090:80 harrison/first
Running the Image Interactively
If you want to run the container interactively you can specify -it instead of -d. When running it interactively you will want to specify which application you want to launch. /bin/bash in this instance. This will allow you to make modifications to the image.
docker run -it -p 8090:80 harrison/first /bin/bash

Visiting Website
Now launch your favorite browser and visit 127.0.0.1:8090 or your IP and you should receive the content from the html file that was created earlier in this document.

Conclusion
The above Dockerfile will get you started with your container adventures. The next docker document will expand on this by installing PHP for Nginx.