nginx on Docker

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud

I’m going to do a quick run thru of building a web server that returns ‘hello world’ as html and json, using Nginx and Docker under the covers

Make sure you have docker & nginx installed before proceeding further

Run the following commands to set up files & folders 


mkdir hello_world_docker_nginx
cd hello_world_docker_nginx
touch Dockerfile docker-compose.yml
mkdir src conf
touch src/index.html conf/nginx.conf

After running the above commands, file structure would look like

├── Dockerfile
├── conf
│   └── nginx.conf
├── docker-compose.yml
└── src
    └── index.html

copy the below contents in the mentioned files

index.html :

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello world</title>
  </head>
  <body>
    <h1>hello world</h1>
  </body>
</html>
nginx.conf :

events { }
http {
    server {
      location /helloworld/ {
        return 200 '{ "message": "hello world" }';
      }

      location / {
        root /src;
      }
    }
}
What we are doing here is setting up an HTTP server with 2 “locations” or endpoints:
  • /helloworld/ returns a JSON payload with a 200 status
  • / the root which serves up our src directory, which by default serves the index.html file. Therefore we don’t need to specify the index.html file here.

Dockerfile :


FROM nginx

RUN apt-get update \
   && apt-get install curl net-tools -y

COPY /conf/nginx.conf /etc/nginx/nginx.conf

COPY /src /src


build a Docker container using the dockerfile above 

docker built -t nginx_docker .

check the container for build successfully

$ docker images
REPOSITORY     TAG      IMAGE ID       CREATED            SIZE
nginx_docker   latest   046b1b056f2c   9 seconds ago      141MB

run (start) the built docker container

docker run --name nginx_docker -d -it -p 9000:80 nginx_docker 

check the docker container is running successfully

$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS    NAMES
6a987322ee8f   nginx_docker   "nginx -g 'daemon ..."   4 seconds ago    Up 2 seconds   80/tcp   nginx_docker

hit the nginx health check endpoint running in docker container as

curl -v GET http://localhost:9000/helloworld/
* Rebuilt URL to: GET/
* Could not resolve host: GET
* Closing connection 0
curl: (6) Could not resolve host: GET
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9000 (#1)
> GET /api/ HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.13.7
< Date: Tue, 19 Dec 2017 06:29:25 GMT
< Content-Type: text/plain
< Content-Length: 28
< Connection: keep-alive
< 
* Connection #1 to host localhost left intact
{ "message": "hello world" }
 



Comments

haizellraabe said…
MOHEGAN RESORT - Mapyro
Find 청주 출장마사지 MOHEGAN RESORT, WYNN, NV, United States, 김포 출장샵 United States, 구미 출장안마 price, reviews, 강원도 출장안마 maps, MOHEGAN RESORT, 1 WYNN PIXRE, 1 WYNN Hwy, 의정부 출장샵 WYNN, Nevada 89449, USA.

Popular posts from this blog

Validate Cognito tokens in Kong

Tracing Requests in AWS Serverless Applications

Leveraging AWS Elasticsearch