Home » Categories » Linux Cloud Server » NGINX |
Docker Explained: How To Containerize and Use Nginx as a Proxy |
Article Number: 257 | Rating: Unrated | Last Updated: Sat, Jan 4, 2014 at 1:16 AM
|
Docker in BriefThe docker project offers higher-level tools, working together, which are built on top of some Linux kernel features. The goal is to help developers and system administrators port applications - with all of their dependencies conjointly - and get them running across systems and machines -headache free. Docker achieves this by creating safe, LXC (i.e. Linux Containers) based environments for applications called "docker containers”. These containers are created using docker images, which can be built either by executing commands manually or automatically through Dockerfiles. Nginx in BriefNginx is a very high performant web server / (reverse)-proxy). It has reached its popularity due to being light weight, relatively easy to work with, and easy to extend (with add-ons / plug-ins). Thanks to its architecture, it is capable of handlinga lotof requests (virtually unlimited), which - depending on your application or website load - could be really hard to tackle using older alternatives. It can be consideredthetool to choose for serving static files such as images, scripts or style-sheets. Installing Docker on Ubuntu (Latest)With its most recent release (0.7.1. dating 5 Dec.), docker can be deployed on various Linux operating systems including Ubuntu / Debian and CentOS / RHEL. Installation Instructions for Ubuntusudo aptitude update
Make sure aufs support is available:
Add docker repository key to apt-key for package verification:
Add the docker repository to aptitude sources:
Update the repository with the new addition:
Finally, download and install docker:
Ubuntu's default firewall (UFW: Uncomplicated Firewall) denies all forwarding traffic by default, which is needed by docker. Enable forwarding with UFW: Edit UFW configuration using the nano text editor.
Scroll down and find the line beginning with DEFAULT_FORWARD_POLICY. Replace:
With:
Press CTRL+X and approve with Y to save and close. Finally, reload the UFW:
Basic Docker CommandsRunning the docker daemon and CLI Usage Upon installation, the docker daemon should be running in the background, ready to accept commands sent by the docker CLI. For certain situations where it might be necessary to manually run docker, use the following: Running the docker daemon:
docker CLI Usage:
Note:docker needs sudo privileges in order to work. Docker CommandsHere is a summary of currently available (version 0.7.1) docker commands:
Let’s Begin!Building a Docker Container With Nginx InstalledAfter having installed docker and having quickly gone over its commands, we are ready to start with the actual work to create our docker container running Nginx. Note: Although after following this section we will have a running docker container with Nginx installed, it is definitely not the recommended method due to its complexity. However, it is here to offer you a chance to learn how to work with a live container and get familiarized with the commands we will need to define later to automate the process. To create a docker image with Nginx installed in a much better way, see the next section: Creating a Dockerfile to Automatically Build Nginx Image. Creating a Base Docker Container From UbuntuUsing docker's RUN command, we will begin with creating a new container based on the Ubuntu image. We are going to attach a terminal to it using the "-t” flag.
Note: After executing this command, docker might need to pull the Ubuntu image before creating a new container for you. Remember: You will be attached to the container you create. In order to detach yourself and go back to your main terminal access point, run the escape sequence: CTRL+P followed by CTRL+Q. Being attached to a docker container is like being connected from inside another. To attach yourself back to this container:
Important:Please do not forget that since we are in a container, all the following commands will be executed there, without affecting the host. Preparing the Base Container for Nginx InstallationIn order to install Nginx and the tools we are going to need for the process, the relevant application repository must be available for downloads. Let's append Ubuntu's universe to the default list of the base image.
Update the list with the newly added source.
Before we proceed to install Nginx, there are some tools we should have installed such as nano - just in case.
Installing NginxThanks to having it available in the repository, we can simply use apt-get to download and install nginx.
Configuring NginxUsing the text editor nano, which we have installed in the previous step, let's create a sample Nginx configuration to proxy connections to application servers.
First, on top of the file, a line must be added to not to have Nginx spawn its processes and then quit. The reason we cannot allow this to happen is because docker depends on a single process to run (which can even be a process manager nonetheless) and when that process stops (i.e. quitting after spawning workers), the container stops. Start with the following as the first line of the
We will use a simple sample configuration to have Nginx run as a reverse proxy. Copy-and-paste the following after the
Save and exit pressing CTRL+X and confirming with Y. To run Nginx, you can execute the following:
And that's it! We now have Nginx running in a docker container, accessible from the outside world on port 80 as we set using the Remember: This Nginx file, albeit configured correctly, will not do anything since there are currently no application servers running on the server. Instead of this one, you can copy and use another example which simply works as a forward proxy for testing HTTP headers until you have your application server(s) installed and working. Creating the Dockerfile to Automatically Build the ImageAs we have mentioned in the previous step, it is certainly not the recommended way to create containers this way for scalable production. The right way to do can be considered as using Dockerfiles to automate the build process in a structured way. After having gone through the necessary commands for downloading and installing Nginx inside a container, we can use the same knowledge to compose a Dockerfile that docker can use to build an image, which then can be used to run Nginx instances easily. Before we start working on the Dockerfile, let's quickly go over the basics. Dockerfile BasicsDockerfiles are scripts containing commands declared successively which are to be executed in that order by docker to automatically create a new docker image. They help greatly with deployments. These files always begin with defining an base image using the Usage:
Dockerfile Commands Overview
Creating the DockerfileTo create a Dockerfile at the current location using the nano text editor, execute the following command:
Note: Append all the following lines one after the other to form the Dockerfile to be saved and used for building. Defining the FundamentalsLet's begin our Dockerfile by defining the basics (fundamentals) such as the
Installation Instructions for NginxFollowing our steps from the previous section, let's form the block to have Nginx installed.
BootstrappingAfter adding the instructions for installing Nginx, let's finish off with configuring Nginx and getting Dockerfile to replace the default configuration file with one we provide during build.
Final DockerfileIn the end, this is what the Dockerfile should look like:
Again save and exit the file by pressing CTRL+X and confirming with Y. Using the Dockerfile to Automatically Build Nginx ContainersAs we first went over in the "basics" section, Dockerfiles' usage consists of calling them with "docker build” command. Since we are instructing docker to copy a configuration (i.e. nginx.conf) from the current directory to replace the default one, we need to make sure to have it alongside this Dockerfile before starting the build process. Note: The above explained procedure of copying in an Nginx configuration allows you great flexibility and saves a lot of time by not dealing with attaching and detaching yourself from containers to create configuration files. Now you can simply use one to directly build and run an image. Create a sample
And replace its contents to use it as a forward proxy for testing:
Let's save and exit the nginx.conf the same way by pressing CTRL+X and confirming with Y. This docker image will allow us to port all our progress and quickly create containers running Nginx with a single command. To start using it, build a new container image with the following:
And using that image - which we tagged as nginx_img_1 - we can run a new container:
Now you can visit the IP address and your Nginx running docker container shall do its job, forwarding you to the HTTP status testing page. Example:
Sample Response:
|
Attachments
There are no attachments for this article.
|
How To Install (LEMP) nginx, MySQL, PHP stack on Arch Linux
Viewed 13593 times since Sun, Dec 29, 2013
How To Install and Configure Django with Postgres, Nginx, and Gunicorn
Viewed 4253 times since Fri, Jan 3, 2014
How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7
Viewed 3864 times since Fri, Dec 27, 2013
How To Install nginx on Ubuntu 12.04 LTS
Viewed 2792 times since Tue, Dec 24, 2013
How To Configure Single and Multiple WordPress Site Settings with Nginx
Viewed 4207 times since Sat, Jan 4, 2014
How to Scale Django: Beyond the Basics
Viewed 2278 times since Fri, Jan 3, 2014
How To Install Wordpress with nginx on CentOS 6
Viewed 9209 times since Sat, Jan 4, 2014
How To Deploy Node.js Applications Using Systemd and Nginx
Viewed 9568 times since Sat, Jan 4, 2014
|