Docker Compose networking basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm ryan with moonswitch today we're going to continue our conversation about troubleshooting docker networks specifically today we're going to get into networking with multiple containers attaching to the same network via docker compose a caveat that i'll lay down before we get started if you're struggling getting two containers to talk to each other i recommend doing exactly what we're about to do which is setting up a very simplified version of our problem and going through all the mechanics until you can understand where where things might be breaking down okay so last time we booted a single container uh and just to recap quickly that was docker run we're going to expose the port 3 or publish a port 3000 and expose port 3000 internally we're going to pass an environment message or environment variable called message to it which we'll just say test and i'm just doing these back slashes to help me break things up on different lines you could do this all in one ugly line if you wanted to and the container was called hello node with the v2 tag [Music] okay and so this is listening on port 3000 if we come over to new uh console and curl it local host 2000 we can see our our message test and we could could boot that with different messages which we will in a second so let's use this container and boot a couple of them up okay so next up we need to introduce docker compose docker compose allows us to orchestrate two containers running together it's very similar to smashing two docker run commands on top of each other and running them at the same time so let's let me go ahead and create docker dash compose.yaml and open up my code editor which i'll just drag over from the other screen um here we can see the docker compose file so this starts with the version tag which will use three seven that's just what i'm used to using i think there's at least one other minor version newer than that out [Music] services and we're going to do tweedle a and tweedle b um inside of here we are going to pull the same image that we used earlier which is our hello node hello node v2 and very similar to our run command we want to expose some ports so we'll expo suppose this one on port 3001 externally 3000 internally and same as our previous command we need to pass an environment with a message of hello from tweedle a and i'm just gonna copy pasta season to taste for tweedlebee same image i'm gonna bump the port number by one so this will be running on 3002 and instead of tweedle a we'll say tweet will be and save this and we'll return back to our console um make sure i'm in the right folder and do the command docker compose up um okay in unsupported configuration option port okay so that is because that should say ports so let's fix that real quick no coding like live coding minimize it again rerun docker compose up we'll see both services booting and here we'll see tweedle a and tweedle b so it's kind of multiplexing our standard outs from each container and if we go and you know if you remember we booted tweedle a on 3001 tweet will be on 3002 so let's actually confirm curl local host 3000 should have nothing running on it uh 3001 should be tweedle a 3002 should be tweedle b cool okay so um let's actually go back into let's see this was talking with docker compose containers talking talking with docker compose containers cool so um oops so if we look and see what's in here this is where our docker compose file is and we can do docker compose ps and here we just kind of see more information confirming what we already know having curled these two addresses this will be very similar to an expanded version of docker ps where we can see both of these containers running let's actually pull one of these containers actually docker i t i just like copy and pasting the id and run the shell inside of this container so if we do this we want to see that we can actually see tweedle b right i don't think curls installed by default so let's do apt get update i have to get install curl yes um clear screen and let's do let's curl localhost 3000. so here we are shelled into tweedlebee um we can see it running um i do not think that we should have access to it on 3002 since we are publishing that port but it shouldn't be available internally cool um so where where are we actually running um like how is this working it's docker is creating kind of a pseudo network that we're attaching to so if we exit back out of the shell and do docker network ls we can see what docker calls a named bridge network that is the same the same name as a project we're working with this allows us to do something cool so if we go back into the shell which unfortunately i'm going to have to do well let's see i think curl should still be running since nope so get update uh apt-get saw curl so on when containers attach to a bridge a named bridge network we can do something really cool which is we can curl by name let me see hello from tweedle a and then if we run the same curl tweedle b so using the container name it has created an alias that we can use to communicate so this is your container named postgres or database you could hit database with the port number instead of actually having to know what the local ip is to communicate cool so that is the bulk of what i wanted to show you is communicating using the host name but let's actually do a few bonus things you know i don't like feeling like docker's using too much magic i like to use vanilla linux tools to know what's happening so let's uh let's let's in-map this network so what i want to do next is poke around on the local network not using any docker magic and kind of see see what we can see so i installed nmap which is a way to explore local networks if we pick up the ip address of this box we'll see a few interfaces the one we're interested in is ethernet 0 and so the internal ip of this box is uh 1721802 so if we use nmap we can scan our local network for other boxes that might be around so let's see if we can actually pick up our other container that's running and so the last thing is a cider address and so i'm going to pass in 24 which would scan everything that's 172.18.0 and then anything on that last block could be zero to 255. this is running on dot two i have a suspicion that our other one's running on dot three but let's actually just scan this network real quick cool and so we're on tweedlebee we can see tweedle a running on um this ip address so again this would work on your local ip address if you're just scanning for other computers kind of a fun tool and let's just do a quick port scan and see what we can see on this ip so if we do nmap dash v and i just have a few of these memorized go to map.org i think and you'll see a lot of other examples let's see see what is it oh well it would help if i do nmap not npm sorry that's just bad muscle memory from too many node programs nmap dash v dash a and then 17 to 2.18.0.3 so this should do a port scan plus some other things for this other container on our network so that's running we can see the fully resolved name we can see the ip address and it will run for another second and let's see we also see that it's running a node application we see some supported methods and you know little little tools like this can be helpful if you're trying to troubleshoot you know trying to connect to redis or postgres and just poking around the network and i think i'm going to go ahead and end today it's going to be a big rabbit hole if i keep going from here but hopefully this will lead to some more interesting docker lessons in the near future and please stay tuned and next time i think we'll be booting up a full app with other dependencies from docker compose
Info
Channel: Moonswitch
Views: 2,350
Rating: 4.609756 out of 5
Keywords:
Id: VbaQBIQjRb8
Channel Id: undefined
Length: 11min 28sec (688 seconds)
Published: Fri Aug 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.