Deploying static websites (nginx) using docker

Keywords: Operation & Maintenance Nginx Docker vim yum

Article directory

Create an interactive container that maps port 80

[root@localhost ~]# docker run -it  -p 80 --name web centos /bin/bash

Install nginx

Install wget, vim, make and some required library files and language environments

[root@ca453e479d0c /]# yum -y install wget gcc vim make
[root@ca453e479d0c ~]# yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel

Download nginx, unzip installation

[root@ca453e479d0c ~]# cd /usr/local/
[root@ca453e479d0c local]# wget http://nginx.org/download/nginx-1.7.4.tar.gz
[root@ca453e479d0c local]# tar zxf nginx-1.7.4.tar.gz 
[root@ca453e479d0c local]# cd nginx-1.7.4
[root@ca453e479d0c nginx-1.7.4]# ./configure prefix=/usr/local/nginx && make && make install

Create static pages

[root@ca453e479d0c ~]# mkdir -p /var/www/html
[root@ca453e479d0c ~]# cd /var/www/html/
[root@ca453e479d0c html]# vim index.html
<html>
<head>
        <title>Nginx in docker</title>
</head>
<body>
        <h1>Hello, I'm website in Docker!</h1>
</body>
</html>

Modify nginx configuration file

[root@ca453e479d0c ~]# vim /usr/local/nginx/conf/nginx.conf

 location / {
            root   /var/www/html; 
            index  index.html index.htm;
        }

Run nginx

[root@ca453e479d0c sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@ca453e479d0c sbin]# nginx
[root@ca453e479d0c ~]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 07:16 pts/0    00:00:00 /bin/bash
root       5417      1  0 07:54 ?        00:00:00 nginx: master process ./nginx
nobody     5418   5417  0 07:54 ?        00:00:00 nginx: worker process
root       5422      1  0 07:55 pts/0    00:00:00 ps -ef

Verify Web Site Access

View mapping ports

[root@localhost ~]# docker ps
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:32769


Verify that nginx can provide services to the outside world

[root@localhost ~]# curl http://127.0.0.1:32769
<html>
<head>
	<title>Nginx in docker</title>
</head>
<body>
	<h1>Hello, I'm website in Docker!</h1>
</body>
</html>

Browser access (host ip address)

View the ip address of the container

[root@localhost ~]# docker inspect web | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
                    
[root@localhost ~]# curl http://172.17.0.3
<html>
<head>
	<title>Nginx in docker</title>
</head>
<body>
	<h1>Hello, I'm website in Docker!</h1>
</body>
</html>

Browser access (container ip address)

Posted by richardw on Sun, 13 Oct 2019 14:37:29 -0700