PHP and Docker - Docker Compose File

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're talking you have to think of your Collective application as a network of services because that's exactly what it is and each of those Services is its own containerized component they are all individual in order to network them and make them communicate with each other we need to have some kind of mechanism luckily for us as developers we can do this with code and we can do it with a file called a Docker compose file and so that's what we're going to create here here I'm starting with a brand new empty project when I say it's empty there is actually one file already in there but that's just a git ignore file so that I ignore all the files which my editor adds for me and what I'm going to go and do is create my first actual real file in there and that will be my Docker compose file so new file and this will be called Docker hyphen compose something to note about the name is you do not have to call it Docker compose dot yaml however if you want to use things like push into elastic Beanstalk things like that a lot of services will have a convention in place where you do need to name it like this by default if I run Docker compose commands it will look for a file called Docker compose.yaml by default however I can specify a different file name when I go and run my Docker compose commands and it will use the file name which I specify but for the time being we're going to stick with the default but later on when we go to creating different Docker compose files for different environments then we'll create ones with different names I think what I'll do with this series like I've done with other series is in the past is I shall name my branches after what I'm working on for example with this one what we'll do is we'll create a branch for this so git add dot for git add all git commit and we'll just Commit This of course if you want to know more about git then check out my full git and GitHub course at garyclark.tech so I'm creating a new branch in I'm just calling it create Docker compose okay and then each subsequent step that we do I'll create a new Branch friend so that way if you just want to go and follow the steps on GitHub then it'll be nice and easy for you to do that kind of thing now what we need to Define our network of services we can do that by starting out with a Services key and then here you just create all your different components all your different services so we said that we want to have PHP nginx MySQL and redis in this instance what I think I'm going to do is I'm going to work on nginx first because once we've got our web server up and running then it means the other parts will be more easy to demonstrate as for naming these Services what you'll see a lot of the time is that people will name them more after the service which they provide rather than the technology which is powering it and that kind of makes sense because say for example I name this nginx for my web server what if I had another nginx server which was serving something else in that case it will become ambiguous I won't be able to name them both the same and so it's best to actually name them with something meaningful like the service which they are performing and so in this case we'll just call this web and the nginx will be our web server in order to build this nginx container I I'm going to need something called an image and an image is basically your blueprint your blueprint for how your container should be built think of PHP something like classes where you create an object using the class as its blueprint well this is pretty much exactly the same thing you take the image and then Docker reads that image in order to build the container the way that it should be built so this key here is image and then I need to specify which image and images are located in a registry you can have your own private Registries or you can have a centralized registry if I don't specify a particular registry in order to do this and address and what Docker will do is it will go on look for it in the docker Hub registry which is like the docker default centralized registry think of when you're using PHP and composer then packages is like a centralized location where a composer will go and look for packages this is exactly the same kind of thing and the image I'm going to look for will be called nginx colon latest bag is just a way of versioning things so here we have nginx which is the program and then latest which is the tag IE the version shortly when we come and create our own image what we're going to do is we're going to have a look at something called Docker Hub and I'll show you some tags then and believe it or not there's actually enough information here for me to be able to go and run a Docker compose command and for Docker to go and get the image from the registry pull it down onto my computer and build a container from it however it will be very hard for me to demo anything in order to demo this I need to expose a port from my local machine into the Container so that we have that mapping between the container and my computer the way that we can do that is by saying ports and here you can specify an array that's what the little Dash means in a yaml file but we're just going to have the one mapping and that will be 80 to 80 and so what this means is Port 80 on my local machine my Local Host will map to Port 80 on the container and so with that we now have like a Gateway where we can look into the Container from our local machine and that will make more sense when we go and do that in a second now let me show you the command that we need to run in order to build this network of services obviously we only have one service in there but it is still a network of services nonetheless and that command is docker compose and then up hit enter and so what's actually happening here is that it is reaching out to the docker registry getting that image and pulling it down extracting it and pulling it down onto my computer you'll only have to do this once because once you've been and got that image and it's on your computer the next time that you're on Docker up it will just simply go and get the image straight from your computer where it is located all that has happened so all I need to do now is go and visit localhost colon 80 hit enter welcome to nginx so as you can see nginx web server is successfully installed and working before we finish this one off let me show you a couple of commands which we can use to inspect things and to see what is running first off if you want to see which containers are running you can say Docker PS so apologies that I've made this quite small it's in order to fit it all on one line because as you can see there's quite a lot of information there but we have a container ID and so I want a container is created it's created with an ID it shows you the image which is being used in order to create the container how long it's been up status shows you any ports which are mapped so here you can see that Port 80 on my local machine is mapped to Port 80 on the container and it also gives your container a name now you can actually give your containers names Yourself by specifying a name but I just left it and if you don't leave it then it will come up with a name based on the folder in which it finds itself the other command I'll show you is one called Docker compose PS and so that will tell you which services are running which Services which parts of networks and so let's run that and so here we're not actually looking at this as a container we're looking at it as a web service status running again it tells you which ports are mapped there is one other place where you can go and check this stuff out and solid Docker desktop you can come to this icon at the top here and ask it to show you the dashboard and then here you have these choices of things that you can look at and so I'm looking at containers apps so we have Docker PHP running if we look inside that as you can see we have this one container running nginx latest it shows you which images you have these are all the images which are on my computer and also something called volumes and we'll get to volumes later on
Info
Channel: Gary Clarke
Views: 7,584
Rating: undefined out of 5
Keywords: Gary Clarke, docker and php, docker networking, docker tutorial for beginners, docker mysql, docker xdebug, docker composer, docker redis, docker php tutorial, docker php nginx mysql tutorial, docker php nginx composer, docker symfony, docker desktop, php tutorial, wamp server, mamp setup mac, lamp stack setup, garyclarke.tech, php container example, docker-compose tutorial, dockerfile tutorial, docker multistage build, docker multiple containers
Id: ZX6ENJruhzI
Channel Id: undefined
Length: 8min 40sec (520 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.