Docker Compose in 12 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the first video we learned that an image is a template for the environment that you want to run and when you run an image you get a container we tried this out by running a PHP image this was okay for our very simple application but imagine if you have something more complicated let's work through an example traditionally a big website like an online store would be one big application but a newer trend is to split these big applications up into smaller micro-services the website can then be quite minimal and it just makes calls to other services to get information or to ask them to do some piece of work we're going to build a really simple ecommerce website but we're going to put the codes that provides the product and the product information in its own microservice the website will then use an api on the product service to request the list of products to show to the customer something I touched on at the end of the last video is how you should only have one process per container so each container should provide a single service so this will want one container running the website and one container running the product service they should be independent this means that there can be written in different languages but they do need to be able to talk to each other but we'll get to that bit later let's start with a product service I'm going to build this using Python I'm going to create a new directory for this service called product and then here I'm going to write a very simple Python script I'll call it API dot EU I remember this is another Python tutorial so this is going to be a very quick example so this is going to be the product service I want this to be a simple restful api so i'm going to import flask and flask restful and instantiate these objects then i'm going to have a product class which extends resource and this is just going to have a get method which will return some jason so we want to sell some ice cream some chocolates and we'll be healthy and sell some fruit as well i'm going to add the routine at the bottom and then finally just some code to run the application listening on port 80 with debug turned on obviously a real API would return a lot more data than this it would be a lot more false out and a lot better in general but this will since I've use flasks and flasks westphall I'm going to add these to a requirements txt file so this is a list of dependencies pip can then use this file to install everything this is not specific to docker this is just a Python thing but now let's get this running in a docker container first I'm going to make a docker file remembering back to the previous video the first line needs to use the Frog keyword to specify a base image that will then build on top of there is an official Python image that we can use we just want the latest version of Python and you'll notice it has these on build tags it explains further down the page that these ones automatically install requirements from the requirements txt file it sounds very useful for us it's as if I planned it in advance so let's put this in our Docs well from Python : and the tag 3 on build next we want to copy the source code into the image so we just want to copy the current directory into slash user slash source flush up I've chosen user slash so flush up because this is what the on build image expects then we need to give this a command to run when it starts I'm using the command keyword and then this weird array like syntax we can tell it to run Python and any subsequent elements in this array are arguments passed to Python so we can just tell it to run the script API py so we already know how to run this using docker we do docker bills to create an image out of the steps defined in the docker file and then we do docker run and add some various arguments to specify ports and mount volumes but you might find us get a bit tedious and as we have more containers running more of our services building in 2000 each one separately and making sure that can communicate is just a pain this is where dr. Campos comes in dr. Campos lets us define all of our services in a configuration file and with one command it'll spin up all the containers that we need the first thing we want to do is make a file called docker compose and I'm going to do this one directory up because this is going to cover all of our services so wants to be called docker - compose and - yamo file so the extension is why MPL in this file we start by specifying in the version of the composed file format we do version : and we're going to go with three dr. has changed a lot in the last few years and to keep changing the way compose files have to be written so this doesn't relate directly to the version of dr. compose that you have installed this is just a version of the dr. Campos file format that you wish to write in the latest version right now is three there are several differences between each version there's no reason not to use the latest one after this we specify our services or services : and we'll start with a product service so we give the name it can be anything you want first we tell dr. Campos what to build so we use the builds property and we give it a directory containing the doctor compost files so the folder is called product we can see here that we've got the folder called product next to dr. Campos dot ynl the directories are relative to where the docker compose file is next we can serve our volumes and this is done as a list so you have as many as you want to the format - - then a space and then the directory on the host so your computer so product and we want to mount that in slash user slash soul / app so of course the image when it's built will already have the call inside but mounting a volume makes development easier because it'll see live code changes as they happen we can do the same with ports another list our application is listening on port 80 so we want to map something to pull 80 so to the port on the host will use 5,000 won this can be anything you could method support AC but it might clash with a webserver already installed on your operating system or something else using port AC easier to avoid this will go for 5,000 won and connect that to port AC in the container and that's all we need this is the stuff that we would have previously specified in the docker run command but in a much easier format unsaved in a configuration file to run this we go back to the terminal and type docker - composed up you obviously want docker to be running already I'm assuming you have dock of a mask or dock for windows installed and that app is already open and long as we're in the same directory as the dr. Campos file it will automatically start the services it'll build the images if necessary in this case it's going to download that Python image we found on the docker hub the on-bill trigger will run here automatically and install our flask dependencies and then finally it'll run our container so if we go to our browser we can go to localhost 5001 it works it gives us our JSON array of products if we make a change to the product service let's say we also sold eggs because we've got the volume mounted Python sees the change it detects this and reloads so now when we refresh we get eggs as well ok we can press control C in the terminal to sell for that container now we want to build a website which will use this product service so make a new folder called website I'm going to write this in PHP again it's going to be very quick and simple we make a file called index.php we'll add a little bit of HTML what i want to retrieve here is a bullet point list where each element in the list is a products from the product service so let's open PHP tags and i can use this function called file get contents i wouldn't recommend this in production but it works for an example this function you give it a URL and a return the string containing the contents of whatever is asked that URL now we get to the interesting question of which URL to use when we access the product service locally we can use localhost call on 5001 this won't work within a docker container because for the container localhost we'll be itself conveniently for us docker compose creates a virtual network for all of the containers so by default each container can access all of the others defined in the compose file and their host names much the service name which is nice so we call that product - service this is the hostname that we can use in our PHP script so we can do HTTP colon slash slash product service and when we use docker compose to run this that will resolve to the product service container so delegation will contain that role written straight from the product service we can decode it then we can pull the products out of it and loop through them under echo each one a a bullet point list item for this we could write a docker file again but instead I'm going to demonstrate another way to use docker compose we're going to make a new service called website and instead of specifying a directory to build with a docx file inside we can just use the image property and give it a name of an image so this could be an image we've built previously ourselves or it can be an image straight from the docker hub from the previous tutorial we know that there's a PHP image that we can use and we want the version that comes with Apache next we want volume so with the product service this was just for convenience because we made a custom image which included the source code this time we're getting the image straight from the docker hub so it's not going to include our source code so we need the volume here for anything to happen so here we want the website folder to go in slash var slash www slash HTML again this is just dependent on the image that you use PHP expects or to exist in that directory again we can do pulse for five thousand two months to port AC and finally we can use the depends on the property so we know that the website's not going to work unless the product service is running that called a file get contents which has failed so we can tell docker compose that the website depends on the product service so just uses this name here you can list multiple things that a service depends on so now when we do docker compose up it's going to pull up here to the image then ill saw the product service then it will start the website so when we go to localhost 5,000 it doesn't work what's gone wrong we need to pluralize products now when we refresh welcome to my shop then the PHP script made a call for the product service decoded the duration and it displays it on the page we can see how easy it is to start all of these services and have them communicate it's all defined in this config file which is easy to change and saved alongside the code so you could commit this to github for example then anyone else who works on the project just has to run docker compose up and everything will happen automatically they don't have to go to any effort to configure a dev environment another feature of docker compose is detached mold you can do docker compose up - Dee and this will run it in the background so I can continue to do work we can type docker PS to see that those containers are running so we've got the the PHP one and the product/service one running now this also shows us that the PHP image is unmodified straight from the docker hub whereas the product service is using our custom image which it is named tutorial underscore product service you obviously can't do ctrl C any more to stop them but you can do docker compose stop I hope you see the power of docker compose and I hope that this helps you out when running docker containers if you've got any questions or feedback please do leave a comment if this was useful I'd really appreciate a like and if you want to see more of this then do subscribe in a future video we'll look at docker deployment options thanks for watching
Info
Channel: Jake Wright
Views: 1,136,253
Rating: undefined out of 5
Keywords: jake, wright, computer, science, software, developer, tutorial, docker, container, image, compose, composer, docker-compose, how to, beginners, explained, what is, build, learn, mac, windows, python, python3, php, dockerfile, multi-service, two services
Id: Qw9zlE3t8Ko
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Tue Mar 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.