CentOS:Docker:Nginx-Web

2019-06-15

### Problem identification

Problem Identification

Problem Description:

  1. The URLs of the various systems in our department had just been updated again, so the addresses already saved in everyone’s browsers were now obsolete.
  2. Everyone — product, frontend, and backend — had to update their addresses.

Solution:

  1. Build a small website containing the needed links (a URL resource center)
  2. Set up a simple domain name
  3. Make updates and deployment easy

Resources Needed:

  1. A frontend guy (I didn’t want to write the UI myself)
  2. A server (a cloud VM would do)
  3. A domain name (internal DNS)

Process Breakdown

Overall Process

  1. Told the frontend guy the goal (he was very supportive)
  2. Sketched a rough draft of the design
  3. He finished the UI in about 20 minutes — most of the time went into collecting the URLs
  4. Uploaded it to a public gitee repository myself
  5. Asked Li Zi for a server: 10 minutes
  6. Installed Docker
  7. Pulled the Nginx image
  8. Started the container: mounted the files
  9. Troubleshot the Server 500 error (installed vim in docker)
  10. Happy success — shared it with the team 😊

#### Frontend UI (Vue.js) Never studied it; probably not hard. Backend architecture is my priority lately.
#### Installing Docker (CentOS) Found this [great tutorial 👍](https://www.cnblogs.com/yufeng218/p/8370670.html) online
  1. Check the kernel version (must be > 3.10):

uname -r

  1. Update the yum packages (root privileges):

sudo yum update

  1. Uninstall old versions (optional):

sudo yum remove docker docker-common docker-selinux docker-engine

  1. Install the required packages (yum-utils provides yum-config-manager; the other two are dependencies of the devicemapper driver)

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

  1. Set up the yum repository

sudo yum-config-manager -‌-add-repo https://download.docker.com/linux/centos/docker-ce.repo

  1. List all docker versions across the repositories and pick a specific one to install

yum list docker-ce -‌-showduplicates

  1. Install docker

sudo yum install docker-ce # the repo only enables the stable repository by default, so this installs the latest stable version, 17.12.0

sudo yum install # e.g.: sudo yum install docker-ce-17.12.0.ce

  1. Start docker and enable it at boot

sudo systemctl start docker

sudo systemctl enable docker

  1. Verify the installation (seeing both the client and service sections means docker was installed and started successfully)

docker version


#### Pulling the Nginx Image with Docker and Configuring Nginx Followed a tutorial — working through it step by step gets you there [a very complete tutorial, though it has pitfalls](https://www.cnblogs.combingo1024/p/9022890.html)
  1. Pull the image

docker pull nginx

  1. Create the mount directories

mkdir -p /data/nginx/{conf,conf.d,html,logs}

  1. Install vim

yum install vim

  1. Configuration files
vim /data/conf/nginx.conf

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {

   worker_connections 1024;

}

http {

  include /etc/nginx/mime.types;

  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '

     '$status $body_bytes_sent "$http_referer" '

     '"$http_user_agent" "$http_x_forwarded_for"';

   access_log /var/log/nginx/access.log main;

   sendfile on;

   #tcp_nopush on;

   keepalive_timeout 65;

   #gzip on;

   include /etc/nginx/conf.d/*.conf;

}

vim /data/conf/nginx.conf

server {

listen 80;

server_name localhost;

location / {

   root /usr/share/nginx/html;

   index index.html index.htm;

   autoindex on;

  }

error_page 500 502 503 504 /50x.html;

location = /50x.html {

   root /usr/share/nginx/html;

   }

}

Overall Process

Map port 80 of the nginx container to local port 80

docker run --name nginx81 -d -p 80:80

   -v /data/nginx/html:/usr/share/nginx/html

   -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf

   -v /data/nginx/logs:/var/log/nginx

   -v /data/nginx/conf.d:/etc/nginx/conf.d

   -d nginx:latest


#### Problem Analysis
  1. nginx configuration gotchas:

The resource paths in the nginx config must be paths inside the container — i.e., the mapped internal paths

  1. Locating nginx errors:

Check the error log inside the container:

tail -f error.log

  1. docker commands:

docker -p host port:container port -v host file path:container file path -d image to start

  1. The whole thing took about an hour:

Most of the time went into: an nginx configuration error that kept causing 500 errors (a redirect issue)

The problem was hard to pin down — I kept second-guessing the mounted files (when it was actually an nginx configuration problem)