Configure a Docker Nginx Reverse Proxy Image and Container

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i'm cameron mackenzie i'm the editor-in-chief at the serverside.com and i want to quickly show you how to configure an nginx reverse proxy in docker now in order to do this example the first thing we need is a docker container to run so i'm going to do a little pseudo-docker run command and i'm going to name this container nginx base because it's going to be the baseline for the nginx reverse proxy we're about to create i'm going to run it on port 80 as we usually do and i'm going to use the latest engine x image from docker hub now there you go that starts off nginx and if i come over to my browser and go to localhost well i should get a little welcome message saying hey nginx is up and running but this isn't exactly what i want it's great that nginx is working as a file server but i want it to work as a reverse proxy i've got this application running on port 8080 on this great big long ip address and the application uses the context root sample as you can see i don't want users to have to type that in i want users to be able to go to engine x say engine x hey you're running on localhost give me the sample application and when they do that i want that sample to map to the application running on the apache tomcat server or any node angular whatever type of application running on the back end and that essentially is configuring nginx as a reverse proxy but as you can see it's currently not doing that so i have to make that configuration now here's what i'm going to do to start off there's a configuration file inside that nginx container that well if tweaked properly will turn nginx into a reverse proxy i want that file so i'm going to do a little pseudo docker copy command and i'm going to say go into that nginx base container and look in etsy engine x conf d and grab that default.com file and make a copy of it on my desktop now i'm just going to take a look at that make sure everything was spelled correctly because sometimes i make an error there but that all looks good i'm going to click enter and boom all of a sudden in the lower right hand corner you can see that default.com file has been copied i'm going to open that up with a little sudo nano default dot comp command and here you can see that entire default.com file now in order to configure this as a reverse proxy what you have to do is you have to add a new location setting so i'm going to add a new location setting here and i'm going to say anytime somebody comes in and asks for slash sample what i want to do is i want to route them to that tomcat application that is running on port 8080 and that means configuring a proxy pass setting and specifying the ip address port number and application name context root of that tomcat server so i'm going to type that in here 192.168.246.131 colon 8080 slash sample now you also have to make sure you've got a semicolon at the end of that i'm going to do a ctrl o to write that out i'm then going to do maximize on the screen just to take a look at everything in there i think that all looks good 192.168.246.131. i think it all matches so i'm going to do a control x get out of here and now my configuration is done but all of that configuration is in that local default conf file that's no good to me i need to actually take that file and put that inside of the docker container so the next step is to do the reverse copy i guess you could say i'm going to do this command here sudo docker let's copy that file but not from the container to the file system but instead from the file system namely desktop in that file called default dot conf and we're going to put it back in that nginx container slash at c slash engine x slash conf and i'm going to double check that just to make sure i didn't make a mistake sudo docker cp desktop default com to nginx base at cngxcom.d i'm going to click enter looks like that was pushed in we can always validate that little pseudo docker exec nginx base nginx dash t command which will validate that file looks like the syntax is good we can even do a pseudo docker exec command on nginx base and asked to reload that file so that it all runs without having to stopping and starting the container looks like that signal was processed so the theory is if that file has been edited properly we should no longer get a 404 here if we do a refresh let's see refresh and boom all of a sudden we now have engine x handling that slash sample request when it comes in nginx is forwarding the request to the tomcat application server the tomcat application server processes it sends it back to the engine x container in the nginx container sends the response back to my browser that is engine x working as a reverse proxy inside a docker container now what's next well we'd actually like to create an image based on that container right we don't want just that container floating around we want to actually create an image so we can start that image anytime in the future so how do you do that well now you issue a well i'm going to do a pseudo docker images command first so you can see the images that are currently running looks like i got nginx tomcat and sample this is image is not running but images loaded i'm going to do this sudo docker commit command i'm going to take that nginx base container and turn into a new image called the nginx proxy and if i do that docker images command again you'll notice that nginx proxy is a brand new image i've now created a brand new image called enginex proxy and i could even go in sudo docker run nginx proxy and we actually have the proxy server running as our own image now some people don't like that approach so i'm going to show you one other way to actually create an image and it's using a docker file so i'm going to just create a new docker file here so touch docker file and then i'm going to do sudo nano docker file and this puts me into the basis for the docker file the docker file is actually over here on my local file system you can see it's just created there right next to the default.conf file and in this file i'm going to say hey i want to start off with the latest nginx configuration and what i want to do is then copy that default.conf file that is in the same folder as the docker file and copy it into etsy nginx compd default.conf and that is now a docker file that i can use to build a new image based on that configuration this is great just in case you know you start editing this default com file and making changes to it and you want to recreate the image rather than starting off an nginx container and copying a file from it and updating it you can just run this particular command here so i'm going to save that control o ctrl x to get out and now with that docker file created all i have to do is say sudo docker build engine x reverse proxy t and dot which means looks for the docker file the current directory helps if you spell things correctly but what this is going to do is this is going to create a brand new image but not creating an image from a snapshot of a container but actually creating a new image based on that default conf file and the docker file so pseudo docker build nginx reverse proxy three words this time dash t dot i'll click enter and i think i got that t tag flag transposed let's change this around a bit dash t tag this image as nginx reverse let's keep spelling it wrong proxy dot and boom all of a sudden we have another image created if i do docker images here you can see that we not only have that nginx proxy image created that we created from a snapshot of the container but we also have the nginx reverse proxy image created which we created from that docker file and there you go that is how you configure an nginx reverse proxy in a docker container and create a couple of images that you can use over and over again now if you enjoyed that tutorial i want you to head over to theserverside.com we've got lots of great tutorials on docker kubernetes git devops microservices java you name it if you're interested in my personal antics you can always follow me on twitter cameronmcnz and please subscribe on the youtube
Info
Channel: Cameron McKenzie
Views: 71,632
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, github, gitlab, dvcs, versioncontrol, tortoisegit, gitgui, atlassian, bitbucket, linux, azure, devops, devopstools, gitops, gitopstools, azuredevops, docker, containers, programming, software, development
Id: ZmH1L1QeNHk
Channel Id: undefined
Length: 10min 51sec (651 seconds)
Published: Mon Jun 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.