Deploy with Docker - Step-by-step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's learn how to deploy a project with [Music] Docker hello and welcome I'm Dave today we'll learn the basics of deploying with Docker and I'll provide links to all resources in the description below I'll also provide a link for you to join my Discord server where you can discuss web development with other students and you can ask questions that I can answer and receive help from other viewers too I look forward to seeing you there it seems like every time I try to r on a free hosting service or other service for my students the rug gets pulled out from under me and that free tier is removed and we've gone through this with Heroku and reppel when they also removed their teams for education and now have paid deployments and just recently Planet scale removed their free MySQL tier and I agree with this tweet as well so I hope I'm not jinxing myself today we're going to learn how to easily deploy a PHP Apache server with a Docker container on which can automatically deploy this to a host like render.png runtime parent image so you have Docker images that you can create containers from and this says from and then we're pulling in this PHP 8.2 with a patchy image now you can go to dockerhub to look up images about this and I'm going to quickly show you the docker Hub page so here I'm at Docker Hub which is really hub. do.com and you can search for images and you can see I've already pulled up PHP and it has an official Docker image but there are different images of PHP so I wanted the latest version essentially 8.2 and if we scroll down you can see where they show from PHP 8.2 here and notice this isn't what I'm using as a parent image I'm using one with Apache also so let's scroll down a little further where it discusses Apache and one thing I wanted to point out when I find where this is uh that they don't actually use the latest one in their Apache example so you could use PHP 7.2 but really PHP 8.2 is available so that's what I'm going to use and I just plug the number 8.2 in here instead of 7.2 so this is how we pull this Docker image in the parent image and we're going to base the image we build on this official PHP Apache image hey guys I hope you're enjoying the video you may be surprised to learn that three out of every 4 viewers nearly 75% of all people who watch my channel aren't subscribed so I just wanted to take a quick second and remind you to hit that subscribe button it really helps me out and if you really like my videos you can get exclusive content and support my channel even more by joining my patreon at patreon.com davay thanks for your consideration and now back to the video and now back in vs code after we've pulled in the parent image we want to configure our container so one thing we want to do is install any required system packages and dependencies now this is something that I needed for postgres SQL and I'm going to tell you how I figured out that I needed this I didn't just know this I actually had an error and when I had that error I pasted it into chat GPT and I said what is this error telling me and it said hey you need to install these required system packages and dependencies I don't work with PHP every day and I don't even work in Linux every day which which is essentially what this is building is a Linux system that you're working inside of so chat GPT helped me know these requirements now we'll get down to the postgres SQL requirement that I put in that then required that I use this okay after that we also set up a working directory in the container and then we copy in the current directory contents into the Container so you can see that as well and then I've got a comment here because with PHP you can use composer to manage your dependencies I'm not but I wanted to leave this note in here just in case and then under this it says if you have any specific PHP extensions required you could install them here for example if you need my SQL support and there's a line for that that I have commented out however I wanted post grez now there's a reason we're using post grees and not my SQL and here's the reason why I'm in the not having my students use it here is because they will ask you for a credit card it has to be with a paid account so not everything at postgres however can be used and so you set up a separate postgres database on render.png we have an Apache config file now here's an example once again of using chat GPT I'll click on this apache.com which is the config file and if you don't understand anything over here or if you haven't worked with an Apache server just take this file and paste it into chat GPT where it can explain each thing to you once again this video is not specifically about an Apache server however you might want to know what each one of these things does and that's a good way to have it explained to you bit by bit okay after that we have an Apache module here that runs a rewrite and then notice this line here where we run Echo and then we're going to bind to this IP address that's 4 Z and then Port 80 this is important when we deploy I don't I want this commented out I don't want this right now as we build the image to check it on our computer but then before we send the code to GitHub I want to uncomment this line because render does require this binding to this specific IP address then this note about variable names here for environment variables but we don't want to supply these here you can supply these in your dashboard for your project in Port 80 which is a typical port for web traffic so again a quick rundown of the docker file but explanations here in the comments for everything and if you don't understand anything yes chat GPT and other AI might be good with config files and it can explain any piece of this to you now as I said this would be the only file you would truly need of course we needed the Apache config for our Apache server as well but it is common to use a Docker compose yam file so let me click on that you can see it's docker-compose.yml which is just pronounced yaml and then here you could have more than one service so imagine you didn't just have uh Apache running here with PHP you also possibly had PHP my admin or MySQL or other services that were also running so they would each have a place here in this file we only need the PHP Apache service we are running a separate postgres database on render as well so you can see this is a very simple file it has the name of the service under service it has a version at the top and then under build it has a context and the context simply is a DOT meaning right where you are and then the docker file is listed with the name of the docker file and we can see that over here Port we're not translating any other Port like Port 8,000 would be translated to 80 if we put 8,000 over here on the left but this is just 80 going straight to 80 and then this shows the volumes which matches what we had back here in the docker file as we said showed right here under the working directory as well so it doubles up on a couple of things and really the docker compos file uh becomes more handy or or easier to understand when you see several Services here listed and you're identifying each one oh and once again you can pass environment variables in a Docker composed yaml also again I don't recommend it this file will also be pushed to GitHub okay with the required files in place let's quickly look at some commands I've got a markdown file over here that I put some Docker terminal commands in I also kind of put a definition of Docker up here to help you understand what it is and a link so you can also download Docker because you should do that before we attempt to run any of these commands or they won't work so I've loaded the link in and we're on the Docker page just pick the version for your operating system it's also going to install Docker desktops so not just Docker commands that we can run in the terminal however I'm not going to use Docker desktop today and we're back in vs code now before we run any of these commands make sure you have Docker installed and everything says it's okay then note that I also do have just a simple index.php over here in the file tree as an example and it's print printing out the server variables well it has the site URL if that exists if that is set in your environment but it prints out the server variables as well and then PHP info which will give a page full of info about the current version of PHP that's running on the Apachi server so we'll see a lot on the web page and of course you don't have to use this this is just an example index.php file okay back to those commands and we're ready to build our image and this is incorrect I'm going to remove that-- RM you want Docker build- T the image name and then once again the period meaning build it right here so let's go ahead and open up the terminal and I'm going to say let me pull this down where we can see it here we go now I want my fingers on the right letters Docker build- T and now I'm going to call this my PHP server my- phps server and then I want to build it right here so I'll press enter now this is going to take a little bit so all fast forward and come back when it finishes okay it is finished I'm going to pull this down just a little bit more and you see we can list images here I'll do that and then I also want to run the image and it will actually run the container so I'll say docker-compose up here and I noticed I haven't saved this file so now I've finished that let's first list out the image so Docker image LS and there it is my PHP server is listed as the image that has been built so now we'll say docker-compose up and this should get everything running as we expect it to now it should run on local host on our computer this may take just a little bit too I think it's ready to go though so let me pull up the browser and I'm just going to type Local Host and then space over and there's our PHP page so what I have here is that full array of server variables I don't think we had the site URL environment variable set yet but if we deploy and then we set that on render we could test that out as well just to make sure it's reading those environment variables so we see all of this information here and that's from that server variable and now here starts the PHP info and you can see I'm running 8.2.6 so we have Apache and pH HP running in a container on our computer and everything looks exactly like I expected it to so now we're ready to deploy this to GitHub I'm back in vs code now we want to exit this inside of our terminal so I'm going to press contrl C and that should exit the running container and notice I don't have a git repository over here yet so I'm going to say get and knit and this should initialize the repository now all the files turned green and I have a dogit folder over here if you don't have that that means your vs code settings have hidden this and of course you need to already have git installed to use git and then of course push the code to GitHub and I have a separate video about that that I'll try to remember to bring up a link here right on the video and I'll try to remember to put it in the video description as well for you so if you're not familiar with Git and GitHub you could learn about that as well because I just initialized a git repository here in this directory now I'm going to add all the files with get add and a period and that should add all those files now a note to my students if you have an entire project in PHP Not only would you want your index.php here you would want all of your other folders and files as well and of course if you have any secrets in a environment variable file you don't want to send that to GitHub so create a G ignore file and list that file in theg ignore so it is not sent to GitHub I don't have any secrets here so I'm not worried about that so I've added everything and I need a git commit and I'll put in a message here and I'll just say um completed building container and now we have everything ready to go and we're not really sending the container to GitHub we're in the docker file that it will be built from once again this creates an image that then of course builds the container and then allows that container to be ran so then we're ready to create a GitHub repository okay I've pulled up my GitHub I'm going to click new to create a new repository and I need a name here so I'm going to call this PHP D patchy dcker and then I will link to this in the video description deson as well so you can clone and get these files from my repository if you want to or you could Fork it over to your GitHub and then get the files that way okay so PHP aache D Docker I'm going to leave this as a public directory scroll down here and create create repository now that I have that I've already got an existing repository on my computer so from the command line I just want these three commands here so I'm going to copy all three of those let's go back to VSS code he I'm back in vs code let's open a terminal window I can rightclick here and it will run the first two of the three commands and now we're ready to just press enter to run that last command and now I'm doing that and it is pushing this code to get up back in GitHub let's see if our code is now there I'll click on the project name and yes the code is here now I'm going to update this read me after I create this video so you'll see a little more here but all the files will remain the same now I'm at render if you don't have an account at do it is just use it with your GitHub account so from there I'm going to click next and it's going to pull in my repository list here and I'll have to remember that PHP Apache Docker says it was created 1 minute ago hey that looks great let's click connect and it should be able to pull this code in scroll down here I really can leave everything the way it is the runtime is already set as Docker and that's what I need this is what I need though free not the starter package just just free and if it goes to sleep it won't stay awake the entire time that's fine but it just takes a little while for your application to fire back up after it goes to sleep so we've got a free one and here is where you would put in any environment variables say you needed to put in your connection string to your postgres database it goes in the optional environment variable section and after that you just click create web service and it begins to build the image which will eventually run the container here it says completed building container already well that's great so we come down here it says no logs to show but I assume there will be some logs I'll come back when this finishes and render has completed the build and it says your services live this is the success message you want and by the way I should point out you might get a warning says could not reliably determine the server's fully qualified domain name that's okay don't worry about that warning but the service is live we're ready to check it out let's scroll back up here and they give you a URL so you should just be able to click this URL which has my name here PHP dy- doon render.png for free in a Docker container on render.jpg Gray I've got exclusive content there that you won't find on YouTube and I've also got early release content hope to see you there remember to keep striving for Progress over Perfection and a little progress every day will go a very long way please give this video a like if it's helped you and thank you for watching and subscribing you're helping my channel grow have a great day and let's write more code together very soon
Info
Channel: Dave Gray
Views: 12,398
Rating: undefined out of 5
Keywords: deploy with docker, docker, docker compose, deploy app with docker, docker php, docker container, docker image, docker apache, php apache, docker php apache, postgres, docker postgres, deploy a php server, deploy an apache server, with docker, what is docker, dockerfile, docker-compose, docker-compose file, install docker, docker containers, docker images, deploy docker containers, deploy iwth docker compose, deploy app iwth docker, deploy with docker compose, docker file, php
Id: NL23_cVq6XI
Channel Id: undefined
Length: 20min 19sec (1219 seconds)
Published: Tue Mar 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.