The post How to install Nginx as a reverse proxy server with Docker appeared first on Little Big Extra.
]]>On a single docker host machine, we can run 100’s of containers and each container can be accessed by exposing a port on the host machine and binding it to the docker port.
This is the most standard practice which is used and we use docker run command with -p option to bind docker port with and host machine port. Now if we have to do this with a couple of services this process might work well but if we had to cater a large number of containers, remembering port numbers and managing them could be a hurricane task.
This problem can be dealt by installing Nginx, which is a reverse proxy server and directs the client requests to the appropriate docker container
Nginx Image can be downloaded from docker hub and can be installed by simply using.
docker run nginxNginx Configuration is stored in file /etc/nginx/nginx.conf. This file holds a reference to default.conf
include /etc/nginx/conf.d/*.conf;
Follow the below steps to run a nginx server and have a peek around nginx configuration
docker run -d --name nginx nginx
bash -c "clear && docker exec -it nginx sh"
Navigate to /etc/nginx/conf.d and
cat default.confcopy file contents.We will use this file contents in next steps.
In this step we will try to modify the base nginx image, with changes to default.conf
Create a simple project in eclipse(File->New ->General-> Project) and create a new file called default.conf in the project directory.
In this file add the location block where
location /<URL-To-BE-ACCESSED> { proxy_pass http://<DOCKER_CONTAINER_NAME>:<DOCKER-PORT>; }
for eg,
location /app1 { proxy_pass http://microservice1:8080; }
where the app1 is the URL and microservice1 is the docker container name and 8080 is the docker port , this info can be found using
docker ps-a
This is how the default.conf looks like for 2 docker containers named microservice1 and microservice2
server { listen 80; server_name nginxserver; location /app1 { proxy_pass http://microservice1:8080; } location /app2 { proxy_pass http://microservice2:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
Next step is to create a Dockerfile where we will replace the default configuration file default.conf in the nginx base image with our version of default.conf
Create a file called Dockerfile and add below contents. Make sure the file is at same level as default.conf and under project root directory.
#GET the base default nginx image from docker hub FROM nginx #Delete the Existing default.conf RUN rm /etc/nginx/conf.d/default.conf #Copy the custom default.conf to the nginx configuration COPY default.conf /etc/nginx/conf.d
Now all we need is to build this docker image. Open terminal/command prompt and navigate to project directory
docker build -t mynginx .
If above command was suucessful, our own custom nginx image is ready with our configuration.
Each Docker Container is a seperate process and is unawaare of other docker container. However docker has –link attribute which can be used to create links between 2 docker containers and make them aware about the existence of other containers.
Before we run our image we need to make sure that the services mentioned in the location block are up and running , in our case microservice1 and microservice2.
docker ps -a
Next we need to link these 2 docker containers with our nginx container using this command
docker run -d --name mynginx -p 80:80 -p 443:443 --link=microservice1 --link=microservice2 mynginx
If the above command was successful with no errors we have successfully installed nginx as reverse proxy server and can be tested by opening a browser and accessing
http://localhost/app1
http://localhost/app2
For reference , please see below video.
The post How to install Nginx as a reverse proxy server with Docker appeared first on Little Big Extra.
]]>