CentOS:Docker:Nginx-Web
### Problem identification
Problem Identification
Problem Description:
- 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.
- Everyone — product, frontend, and backend — had to update their addresses.
Solution:
- Build a small website containing the needed links (a URL resource center)
- Set up a simple domain name
- Make updates and deployment easy
Resources Needed:
- A frontend guy (I didn’t want to write the UI myself)
- A server (a cloud VM would do)
- A domain name (internal DNS)
Process Breakdown
Overall Process
- Told the frontend guy the goal (he was very supportive)
- Sketched a rough draft of the design
- He finished the UI in about 20 minutes — most of the time went into collecting the URLs
- Uploaded it to a public gitee repository myself
- Asked Li Zi for a server: 10 minutes
- Installed Docker
- Pulled the Nginx image
- Started the container: mounted the files
- Troubleshot the Server 500 error (installed vim in docker)
- 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
- Check the kernel version (must be > 3.10):
uname -r
- Update the yum packages (root privileges):
sudo yum update
- Uninstall old versions (optional):
sudo yum remove docker docker-common docker-selinux docker-engine
- 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
- Set up the yum repository
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- List all docker versions across the repositories and pick a specific one to install
yum list docker-ce --showduplicates
- 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
- Start docker and enable it at boot
sudo systemctl start docker
sudo systemctl enable docker
- 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)
- Pull the image
docker pull nginx
- Create the mount directories
mkdir -p /data/nginx/{conf,conf.d,html,logs}
- Install vim
yum install vim
- 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
- nginx configuration gotchas:
The resource paths in the nginx config must be paths inside the container — i.e., the mapped internal paths
- Locating nginx errors:
Check the error log inside the container:
tail -f error.log
- docker commands:
docker -p host port:container port -v host file path:container file path -d image to start
- 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)