📜 ⬆️ ⬇️

Use the official docker image of NGINX in InfoboxCloud: part 1

Over the past year, more than 100,000 images have become available in the Docker Hub, and images from the Docker Hub have been downloaded more than 300 million times. Of these, more than 20 million downloads occurred on 70 official Docker developer images, such as Oracle, CentOS, and NGINX.

NGINX is used on more than 40% of the largest sites in the world, not only as a web server, but also as a reverse proxy server, load balancer and HTTP cache. The official NGINX image has been downloaded 3.4 million times.


')
In this article you will learn:

If you have not used Docker before, it is recommended to read:
We use Docker and do not worry about vendor-lock
Dive into Docker: Dockerfile and communication between containers
Overview of Docker Engine from 1.0 to 1.8. Introduction to Docker Compose

At the end of the article, we distribute trial versions of InfoboxCloud for free.

Environment preparation


1. Create a server with CentOS 7 to install Docker in InfoboxCloud . A virtual machine is now needed for Docker to work, so when creating a server, be sure to check the “Allow OS kernel management” box.

How to create a server in InfoboxCloud for Docker
If you do not have access to InfoboxCloud - order it .

After registration, you will receive data to access the control panel by email. Enter the control panel at: https://panel.infobox.ru

In the “Cloud Infrastructure” section of your subscription, click “New Server” (if necessary, the subscription changes in the upper right corner in the drop-down menu).



Set the required server parameters. Be sure to allocate a public IP address to the server and check the box “Allow OS kernel management” , as shown in the screenshot below.



In the list of available operating systems, select CentOS 7 and complete server creation.



After that, the data to access the server will come to your email.

After creating a server with CentOS 7, connect to it via SSH .

We have prepared a script that will allow you to install Docker and useful utilities for working with Docker on such a server. The necessary settings will be made automatically.

Run the command to install Docker and Compose:
bash <(curl -s http://repository.sandbox.infoboxcloud.ru/scripts/docker/centos7/install.sh) 

After that, docker and compose will be installed. You can create an image with the docker installed in the control panel by clicking on the server and then “Create an image”.

After that, it will be possible to create new servers from the Docker image and not to perform this step again.

Creating a portable environment


Our portable environment consists of:

It is better to keep the portable environment in the repository for example on github, when updating the site or configuration files to rebuild the container. This will ensure that, if necessary, you can redeploy a customized site in minutes. You can also quickly raise the site in a separate container for development.

To work with git, install it on the server.
 yum install -y git 

Copy our simple portable environment from github with the command:
 git clone https://github.com/InfoboxHosting/DockerNginxSimpleStaticSite.git 

Go to the environment directory with the command:
 cd DockerNginxSimpleStaticSite/ 

Build a Docker image with the command
 docker-compose build 

Deploy the image to the container with the command
 docker-compose up -d 

Try to access the server by IP. The pre-configured static site was successfully deployed.



If you run
 docker ps 
You can see the site in the list of containers.



Let's see what the portable environment consists of.



The nginx configuration is in the conf / nginx.conf file:
nginx.conf
 user www-data; worker_processes 1; pid /run/nginx.pid; events { worker_connections 4086; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { listen 80; server_name domain.tld www.domain.tld; location / { root /usr/share/nginx/html; } } } 


where domain.tld needs to be replaced with the domain of your site.

The web server configuration for the site is stored in a portable environment, so that during deployment you do not need to reconfigure the web server every time.

At the root of the portable environment is the Dockerfile , which uses the official NGINX image and adds your website from the html folder and the NGINX configuration from the conf folder to it.

 FROM nginx MAINTAINER Yuri Trukhin "trukhinyuri@infoboxcloud.com" COPY conf/nginx.conf /etc/nginx/nginx.conf COPY html/ /usr/share/nginx/html 


More details about the Dockerfile were covered in this article .

Also in the root of the portable environment folder, create a file docker-compose.yml with the following contents:
 sitename: build: . ports: - 80:80 restart: always 

It says that you need to expand the sitename environment, first building it from a Dockerfile in the same directory. Port 80 of the container must be forwarded to port 80 of the host. If NGINX is dropped, restart it.

More information about Compose was covered in this article .
If you change the site or nginx configuration file - just rebuild the image with the command
 docker-compose build 

And then expand the image into a container:
 docker-compose up -d 


We do not need to worry about server setup every time. Once developed, a portable environment with Docker allows you to quickly deploy.

To perform the recommendations in the following section, stop and remove the expanded container, since it takes port 80 on the host we need.

To do this, run:
 docker ps 

Copy to clipboard container_id.
Stop the container with the command:
 docker stop bc88ee61a933 

where instead of bc88ee61a933 insert your container_id.
Remove container:
 docker rm bc88ee61a933 

where instead of bc88ee61a933 insert your container_id.

Deploy multiple sites with reverse proxy


In this section, we will deploy several static sites with different domains and reverse proxy. To complete this part of the article, you will need to send your DNS domains to the ip – address of the server:

The necessary ip – addresses can be found in the InfoboxCloud control panel.



What if you do not have domains
Option 1. Configure hosts

This option is great for testing, but will only work from your computer.

Add domain records to your computer’s hosts file .
In OS X and Linux, you can add entries to the / etc / hosts file .



On Windows, you need to run Notepad as an administrator. To do this, enter Notepad or Notepad (in the Russian version of Windows) in the search field and select the launch item as an administrator.



Open the file C: \ Windows \ System32 \ drivers \ etc \ hosts . If it will not be visible in Notepad, select the “All files” file type next to the “Open” button.



Add the necessary entries and save the changes. Do not forget to use the addresses of your server from the control panel as ip-addresses.



After the tests, do not forget to remove these entries from the hosts file.

Option 2. Purchase the required domains

For example, this can be done here .


Now connect to the server with Docker in InfoboxCloud over SSH.
Clone a prepared repository for 2 sites with a load balancer.
 cd ~ 

 git clone https://github.com/InfoboxHosting/DockerNginxSimpleBalancer.git 




Go to the directory of the portable balancer environment:
 cd ~/DockerNginxSimpleBalancer/balancer 

Run the build of balancer and dependent images with one command:
 docker-compose build 

Deploy the balancer and dependent images:
 docker-compose up -d 

Note: docker-compose.yml files in the folders of each of the frontends in this example are not used.

Open in the browser the domain of the first site:



Now check that the second site opens correctly:



In order for sites to open not with the specified domain1.com and domain2.com in your / etc / hosts , you need to correct the file ~ / DockerNginxSimpleBalancer / balancer / conf / nginx.conf :

nginx.conf
 user www-data; worker_processes 1; pid /run/nginx.pid; events { worker_connections 4086; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { listen 80; server_name domain1.com www.domain1.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend1:80; } } server { listen 80; server_name domain2.com www.domain2.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend2:80; } } } 


Replace domain1.com and domain2.com with the addresses of your sites, rebuild the image and re-expand the container:
 cd ~/DockerNginxSimpleBalancer/balancer 

 docker-compose build 

 docker-compose up -d 


Deploy multiple geo-distributed sites


Create a server with a docker in each of the InfoboxCloud regions: Moscow, St. Petersburg, Amsterdam as shown in the “Preparing the environment” section and install it with the git command:
 yum install -y git 

How to connect additional regions of the cloud
In the control panel go to the main page and click "Order a new service."



Select the "Cloud Servers" service.



Select the desired region and complete the order process to the end.



Connection of additional regions is free, but it is required that the account be more than 500 rubles.

You can switch between regions in the upper right corner of the control panel in the drop-down menu.



To complete this part of the article, you will need to send your DNS domains to the ip – address of the server:



The necessary ip – addresses can be found in the InfoboxCloud control panel.

Save the prepared portable environments to each of the servers:
 git clone https://github.com/InfoboxHosting/DockerNginxGeoRedundantBalancer.git 


On each server, edit the file ~ / DockerNginxGeoRedundantBalancer / balancer / conf / nginx.conf and replace domain1.com with the domain name of the first site, domain2.com with the domain name of the second site, domain.com with the service domain.

Instead of manual renaming, you can run the command on each of the servers :
 bash ~/DockerNginxGeoRedundantBalancer/rename.sh domain.com domain1.com domain2.com 

in which instead of domain.com specify the name of the service domain, instead of domain1.com is the name of the first site, domain2.com is the name of the second site.

Original nginx.conf
 user www-data; worker_processes 1; pid /run/nginx.pid; events { worker_connections 4086; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; upstream frontend1 { #!!!replace domain.com #!!!remove backup word in your location server spb.domain.com:8000 backup; server msk.domain.com:8000 backup; server ams.domain.com:8000 backup; } upstream frontend2 { #!!!replace domain.com #!!!remove backup word in your location server spb.domain.com:8001 backup; server msk.domain.com:8001 backup; server ams.domain.com:8001 backup; } server { listen 80; #!!!replace domain1.com server_name domain1.com www.domain1.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend1; } } server { listen 80; #!!!replace domain2.com server_name domain2.com www.domain2.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend2; } } } 


nginx.conf after making changes for Amsterdam
 user www-data; worker_processes 1; pid /run/nginx.pid; events { worker_connections 4086; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; upstream frontend1 { #!!!replace domain.com server spb.plugndo.com:8000 backup; server msk.plugndo.com:8000 backup; server ams.plugndo.com:8000; } upstream frontend2 { #!!!replace domain.com server spb.plugndo.com:8001 backup; server msk.plugndo.com:8001 backup; server ams.plugndo.com:8001; } server { listen 80; #!!!replace domain1.com server_name site1.plugndo.com www.site1.plugndo.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend1; } } server { listen 80; #!!!replace domain1.com server_name site2.plugndo.com www.site2.plugndo.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend2; } } } 



nginx.conf after making changes for St. Petersburg
 user www-data; worker_processes 1; pid /run/nginx.pid; events { worker_connections 4086; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; upstream frontend1 { #!!!replace domain.com server spb.plugndo.com:8000; server msk.plugndo.com:8000 backup; server ams.plugndo.com:8000 backup; } upstream frontend2 { #!!!replace domain.com server spb.plugndo.com:8001; server msk.plugndo.com:8001 backup; server ams.plugndo.com:8001 backup; } server { listen 80; #!!!replace domain1.com server_name site1.plugndo.com www.site1.plugndo.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend1; } } server { listen 80; #!!!replace domain1.com server_name site2.plugndo.com www.site2.plugndo.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend2; } } } 



nginx.conf after making changes for Moscow
 user www-data; worker_processes 1; pid /run/nginx.pid; events { worker_connections 4086; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; upstream frontend1 { #!!!replace domain.com server spb.plugndo.com:8000 backup; server msk.plugndo.com:8000; server ams.plugndo.com:8000 backup; } upstream frontend2 { #!!!replace domain.com server spb.plugndo.com:8001 backup; server msk.plugndo.com:8001; server ams.plugndo.com:8001 backup; } server { listen 80; #!!!replace domain1.com server_name site1.plugndo.com www.site1.plugndo.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend1; } } server { listen 80; #!!!replace domain1.com server_name site2.plugndo.com www.site2.plugndo.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://frontend2; } } } 


Now for Moscow, go to the appropriate folder:
 cd DockerNginxGeoRedundantBalancer/balancerMSK/ 

For Amsterdam, browse to the appropriate folder:
 cd DockerNginxGeoRedundantBalancer/balancerAMS/ 

For St. Petersburg:
 cd DockerNginxGeoRedundantBalancer/balancerSPB/ 


On each of the servers, run the commands
 docker-compose build 

and
 docker-compose up -d 


Now you can go to each of the sites and check their availability:





Now from the control panel, turn off the server in any of the regions. Sites are still available.
Shut down the server in the second region. Sites are still available.

While at least one server is available, the sites will work. If the request is on a server that is down, all modern browsers attempt to send a request to another server from DNS. As a result, in a few seconds the site will open.

You can make the process of deploying servers and sites in several regions fully automatic using Ansible.
Part 1 .
Part 2 .
Part 3
Part 4
Part 5 .

How to get a trial version of InfoboxCloud for free?


Send us your email address and full name to trukhinyuri@infoboxcloud.com , in response you will receive data to access the control panel. You can test the new cloud region for 15 days, then you can go to the full version of the cloud. You can request a free trial version before September 5, 2015.

If you have questions or comments, write to us and we will be happy to answer.

If you can not leave comments on Habré, write to us in the Community .

Successful use of NGINX and Docker in InfoboxCloud !

Source: https://habr.com/ru/post/265231/


All Articles