Docker Compose Tutorial - Docker in Practice || Docker Tutorial 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so in the last video we created and started to docker containers MongoDB and mangu Express and these are the commands that we used to make it happen right the first we created an Network where these two containers can talk to each other using just the container name and no host port etc is necessary for that and then we actually ran to docker run commands with all the options and environmental variables etc set now this way of starting containers all the time is a little bit tedious and you don't want to execute this run commands all the time on the command line terminal especially if you have a bunch of docker containers to run you probably want to automate it or just make it a little bit easier and there's a tool that's that makes running multiple docker containers with all this configuration much easier than with docker run commands and that is docker compose if you already know that compose and you were wondering why is it useful and what it actually does then bear with me in the next slide I'm gonna explain that so this is a docker run command of the MongoDB that we executed M previously so basically we docker compose file what we can do is we can take the whole command with its configuration and map it into a file so that we have a structured comments so if you have let's say ten docker containers that you want to run for your application and they all need to talk to each other and interact with each other you can basically write all the run commands for each container in a structured way in the docker compose and we'll see how that structure actually looks like so on the right side in the docker compose example the first two ticks are always there right version three that's the latest version of the compose docker compose and then we have the services this is where the container list goes so the first one is MongoDB and that Maps actually to the container name all right this is going to be a part of container name when docker creates a container out of this configuration blueprint the next one is actually the image right so we need to know which image that container is going to be built from and of course you can specify a version tag here next to the name the next one is port so we can also specify which ports is going to open first one is on the host and the second one after the colon is on the container so the port mapping is there and of course the environmental variables can be also mapped in the docker compose and this is how actually the structure of docker compose looks like for one specific comments let's actually see the second container command for Express that we executed and how to map that so now again we have a docker run command for Express and let's see how we can map it into a docker compose so as I said services will list the containers that we want to create and again names Express will nipt map to the container name the next one will be the image again you can add a tag here if you want to be have a specific one then you have the port's 82 82 80 80 and then you have all the environments of variables again under the attribute environment and this is how the docker compose will look like so basically docker compose is just a structured way to contain very normal common docker commands and of course it's it's gonna be easier for you to edit the file if you want to change some variables or if you want to change the ports if you want to add some new options to the run command so to say and maybe you already notice the network configuration is not there in the docker compose so this network that would create it we don't have to do it in a docker compose we go to the neck slide because we have the same concept here we have containers that will talk to each other using just the container name so what docker compose will do is actually take care of creating a common network for these containers so we don't have to create the network and specify in which network these containers will run in and we're gonna see that in action right away so let's actually create a docker compose file so I'm gonna paste all my contents here and this is exactly what we saw on the slides and I'm gonna save it as a llamo and we see the highlighting as well be very aware of the invitation they have to be correct so this is the list of all the containers on the same level and then each container has its configuration inside that so now compared to docker run commands it will be very easy for me to go here and change these environment variables or add some new configuration options etc so here again for demonstration we actually save the dr. compose in the code so it's part of the application code so now that we have a docker compose file the question is how do I use it or how do I start the containers using that so let's go to the command line and start docker containers using this docker compose file so the way to use it is using docker compose comment now if you've installed docker on your laptop it usually gets installed with the docker compose packaged inside so you should have both talker and docker compose comments installed as a package so docker compose command takes an argument which is the file so I'm going to specify which file I want to execute and in my case it's called yamo and at the end I want to say what I want to do with this file in this case the command is up which will start all the containers which are in the mo so let's actually check before that there there are no containers running so I have anything running here and I'm gonna start those two containers okay so there are a couple of interesting things here in this output so let's crawl all the way up so we've talked about docker Network and how we created our own network at the beginning to run the containers inside and I said the docker compose takes care of it and here we see the output where it actually created a network called my app default this is the name of the network and it's gonna run those two containers these are actually the names of the containers the docker compose created this is what we specified and it just added prefix and suffix to it and it created those two containers in that network so if I actually go here and do dr. Network LS I see the my app default is here so that's one important thing another one is the logs of both containers actually mixed because we're starting both at the same time as you see the manga Express has to wait for MongoDB to start because it needs to establish a connection so we here see the logs the MongoDB is starting we still get connection refused because it's not started completely and somewhere here when MongoDB is started and listening for connections Express is able to connect to it so this is something that you can also do with docker compose when you have two containers that where one depends on another one starting you can actually configure this waiting logic in the docker compose okay so now let's see actually that the docker containers are running so we have both of them here you see the container names that docker compose gave them and one thing here to note is that the Express actually started on port 8081 inside the container so we can see that here so we are opening a port 8080 my laptop that actually forwards the request to container at port 8081 just so that you don't get confused because it was 8080 on the slides so now that we have restarted the containers let's actually check the first one which is Express so it's running on 8080 in the previous example we created a database in the collection which is gone because we restarted the container this is actually another very important concept of containers to understand when you restart a container everything that you configured in that containers application is gone so data is lost so to say there is no data persistence in the containers itself of course that is very inconvenient you want to have some persistence especially when you're working with the database and there is a concept we're gonna learn later in this tutorial series called volumes that makes it possible to have persistency between the container restarts ok so let's actually create the database again because we need it and inside the database we had actually users collection let's create that one as well and that is empty now let's actually start our application and there you go so now if I were to modify this one here and update I should see the updated entry here so the connectivity with MongoDB works so now what do I do if I want to stop those containers of course I could go there and say docker stop and I can provide all the ideas as we did previously or we don't compose it's actually easier I can do docker compose again specify the file and instead of up I'm gonna say down and that will go through all the containers and shut them all and in addition to removing the containers or stopping them removing the containers it also removes the network so the next time we'll restart it it's gonna recreate so let's actually check that the network LS that default my app default network is gone and when I do up see it gets recreated that should give you a good idea of what docker compose is and how to use it in the next video we're gonna build our own docker image from our nodejs javascript application thanks for watching the video I hope it was helpful and if it was don't forget to like it this is a video series so I will create a new one every week so if you want to be notified whenever a new video comes out then subscribe to my channel if you have any questions if something wasn't clear in the video please post them in a comment section below and I will try to answer them so thank you and see you in the next video
Info
Channel: TechWorld with Nana
Views: 54,170
Rating: 4.9707494 out of 5
Keywords: docker in real world, docker compose, docker compose tutorial, docker compose volumes, docker compose network, docker compose in 12 minutes, what is docker compose, docker networking, docker tutorial, wright, raghav pal, how docker compose works, how does docker compose work, docker compose vs docker run, docker run vs docker compose, how to use docker compose, docker tutorial for beginners, docker explained, techworld with nana
Id: MVIcrmeV_6c
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Sun Nov 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.