How to deploy your Docker Application to Azure Container Instance using GitHub Actions [CICD]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
GitHub actions is a continuous integration and continuous delivery CI CD platform that allows you to automate your builds tests and deployment pipeline you can create workflows that build and test every pull request to your repositor our deploy merge posts request to production docker is an open platform for developing shipping and running applications Docker enables you to separate your application from your infrastructure so you can delete your software quickly the CI CD and containers has its origin and it devops movement if you want to know more about it I rarely recommend reading the book The Phoenix projects which is a novel very interesting also more into detail about the methodology is a devops handbook devops allow companies to softer very quickly in a reliable way CI CD Pipelines your development will be quicker more reliable and safer I'm gonna use a use case now if I say you are developing a API you are develop an API in fast API and you want to test locally and package that application in a container using docker but as soon as you push the codes to GitHub you trigger a CI CD pipeline where your software will be tested and then deployed automatically into the clouds using azure container instances let's get start and go step by step to the process open a new repository called GitHub CI CD openvs code you can just type code Dot and EVS code opener terminal because I'm using python so make sure to have python in your system to isolate an environment I'm going to create a virtual environment and then I'm going to install fast API because I'm gonna use that for my backend of this application it's going to be a very simple hello world demo application once I start my fast API I'm just going to start development I'm gonna create a folder for my application called API in this folder I'm going to create a file called main.pi also because I'm gonna test it as a module I'm also going to create a file called init Dot so in the main file I'm gonna just write a quick fast API application my employee libraries create an app and create a simple route let's call it just hello because I'm gonna change it later let's run this app locally and make sure that everything is working to run development server and fast API you just type unicorn and because I don't have any local directory I have to put this argument after equals dot which is the current folder and then I type the path to my app admin colon app dash dash reload and that's going to start my development server if I click here and go to my localhost which is 127.0.1.1 at Port 8000. and that's my app running here on my computer let's press Ctrl C to stop the development server now I'm just gonna create tests for my app because I want to automate the tests I first create folder called tests where I'm going to create my tests and then I have to install Pi test I test so inside the test folder I create another folder called test underscore API dot pi in the test api.pi file I'm just gonna board the client test client import from my project my application that I'm gonna test create a client and then write my tests now let's run my test locally my test is passing so to organize my project I'm gonna create a requirements file I'm just gonna remove Pi test here because I don't need Pi tests to run my applications just testing I'm going to create another file called requirements Dash Dev and then this one is going to be our requirements txt and then Pi test disconnect in order to use GitHub actions I have to have this GitHub so head to my repository here I'm just going to create a new repositor and then I'm just gonna call CI CD back to my vs code here because I don't want a 3000 in all my requirements here I'm gonna create a file called ignore it's called Dot git ignore so I wanted to remove all my virtual environment my cache my how to push to my remote I need to add a remote so what I'm gonna do here I'm just gonna create click that one here and then I'm just gonna paste I'm just gonna initialize my remote and then I'm gonna have first commits and then I'm gonna push no to push I'm just gonna get my origin as my Upstream and then I'm just gonna do that and if I refresh here it's in gate now let's create our first workflow this is where GitHub shines GitHub action shines the way it works is you can use Imo file which is a declarative way to say what needs to be automated for this first workflow I just wanted to test my app what I wanted to do is to create this search flow that's consistent and each workflow would have a job and its steps so let's create the first job and its steps for that I have to create a folder called Dot GitHub and then inside that folder I have to create another folder called close with this at the end within workflows I'm just gonna write create a emo file you can call it anything you want I'm just gonna call it build and deploy dots yemo in the ability to deploy emo file what you have to do is first you have to give it a name and then you have to tell the event that's gonna trigger that cicd pipeline then you have to create a job and then you have to tell the runner which is a machine that's going to run your job then the first step is to download your repository that machine which use actions check out victory and then you have to set up that machine with an environment which is python in my case I'm installing python version 3.11 you can install the dependencies which in my case because I'm testing I'm installing all the dependencies plus the dev dependencies which include Pi test and then I just run the script Python and Pi test with that let's add this to the repositor with that you can go to GitHub and then go to actions and that gonna trigger the action it start to run now as I said let's put this application in a container because I wanted this to be repeatable and also I'm gonna use a technology a cloud native technology in Azure that's called Azure container instance a container is a way to isolate your environment into a machine where the container doesn't have CPU it just use the CPU of Y hose machine and then run all the process that's necessary to run your application let's create a container you have to have an image and then you can have a declarative way to create an image so you have to create a file called Docker file and then using this Docker file and then in this Docker file we have to declare your environment of for your application in this case I'm gonna create an image that's based on python 311 I'm gonna create a working directory called code I have to copy all the code that I've developed my app into that path or directory I have to install the dependencies that I will need for my app and then I'm gonna copy the code lastly I'm just run the startup process with a command in the command line to start my application but in this case now I'm running production last test is locally before push to the cloud to build a container obviously you have to have installed Docker Engine with dock installed you just need to type this command Docker Butte give it a tag and put dot that's the current context that's gonna be the container once the container is built you want to run the container for that just type this commands Docker run detached port 8080 and then the name of the content that's just built the name of the image of the content that you just built and then after that container will run and press that the container starts to check if the container is running just press Locker PS and that show the container running to go to our application manage by the container just open your browser and then press localhost and then because it's part 80. that's going to be the default port yes the application is running fine what I'm gonna do is I'm gonna to push that container to the clouds so that when I deploy it I pull it from somewhere it's called container registry because this is just a demo I'm gonna use a pop like container register which is Docker Hub so to do that in CI CD you have to create a new stat here another job first job is test which has got these steps let's create new steps called Docker that comes right after test jobs should be on the same level and I have like the keyword needs because I wanted to run that after the test pass I don't want it to build my container without the past tests so I'm gonna run a new bootle test and then I'm gonna have several steps here that I'm using actions I'll put in the description the actions GitHub that you can read the documentation because I need to create an environment where I can build my container and then push to Docker Hub because I'm using uh sensitive information here I'm gonna use a functionality of GitHub actions called secrets and I'll do that after that and lastly I'm just gonna push the container to my Docker hub to create the secrets you go to your repository settings secrets and variables and then actions and then create a new repository once your secrets are created you can now push your code let's go back to GitHub and see if the actions is going to work fine it's running test in the meanwhile I'm just gonna have my Docker open here to see if it's updated you can see that I've done that a dry run yesterday but let's see if the docker step is gonna work setting up the job Dot job is complete so let's check if it's been pushed in Docker Hub if I refresh here should be like just now or something one minute ago yeah just a few seconds ago so now it's all that's how it's set up I wanted to make this application useful so I need to this application to be out there to the road that people can use it so a good way to deploy a Docker application is to use Azure container instance a good way to develop your application is to use Azure container instance which develop apps fast without managing virtual machines or having to learn new tools it's just your application in a container running in the cloud so let's do it I'm gonna create this application to the CLI so you have to have installed a application called Azure CLI which I have here on my computer so to make it work you have to log into your account in my case let's log in I said login in my case I'm logged in after it logged in you're gonna receive that thing here so you need to install an extension to do to deploy a Content instance in this case is this come on here Azure extensor ad name container app upgrade as you can see I've already had this installed to deploy my application I'm gonna write those commands here which is I'm just gonna up my application based on my container that's here and if I press enter that's gonna trigger the application that's gonna create an application create a resource Group an environment based on that image here that I upload the docker hub in this port good running grass is external and then it's going to create a fully qualified URL that I can access my application if I go to my Azure account a new Resource Group called my container apps it's already been created and then our resources will be added to this Resource Group we can just watch it so it's already started creating okay my application was developed in this URL which I can go here [Music] there we go I have like a foreign with a fully qualified URL with my application running in the cloud anyone can access this now the last step here is to make sure that I allow my GitHub actions go to my Azure account and get that image that I pushed to Docker Hub and deploy it automatically without me have to do it manually in order to do that I need to create what they call service principle which is this a service account that I give some privilege just specifically to that application and then that application or service principle can connect to my GitHub account and then push to my Azure content instance to do that I have to create this service principle I'm just gonna do it using the CLI um by doing this command you have to put your subscription name here once it's created just copy this to somewhere safe on your computer then login to your azure active directory then goes to app registration all applications and then you will find that service principle principle name that's just been created and then you go to certificates and secrets and then class Federated credentials it's credential select a scenario in our cases GitHub actions deployed to Azure resources in this case is my name my GitHub account this name and the repositor is this The Entity type is I'm just gonna be the brain is going to be the event and the branch name is the master branch I have to create some secrets in my Repository I go to settings we are almost there the last step you have to create a new job into cicd pipeline employ Azure and that needs docker running runs on the button on Ubuntu latest I'm gonna use some actions here called azure and then with those credentials I can log in into my account and then with that I have access to my CLI and then run that same command that I've just run from my computer with that I think we can now finish done so let's see our full pipeline running now that you see that our app is all working last two or less change here in our app to make sure that it reflects the change when you push our code everything is going to automatically be deployed I'm gonna go to API here and then instead of saying just hello I'm just put world and I have to change my tests so make sure that it passes good I have an error here I know what it is I needed to add 4 here some context which tells me the context that I'm using that connection that I created in azure connecting with GitHub actions now with my tests everything and this little correction everything should work now let's do a last commit one more time so let's see the last step setting up the job so far so good yeah nuts connects into the CLI good now it's just running that command that's going to redeploy my app looks like my app has been redeployed let's check it there we go my new app has been deployed I just need to I just skipped one test a step here before deploying solids look usually to review the container locally if you want just in case you make sure everything is working but once you push the GitHub on that event which is my master branch that automatically deployed without me having to do anything so and then that's it so thanks for watching if you haven't subscribed the channel subscribe like the video and click the Bell thank you for our time see you next time
Info
Channel: Pytalista
Views: 3,661
Rating: undefined out of 5
Keywords:
Id: fEdHtvBp7Dw
Channel Id: undefined
Length: 24min 28sec (1468 seconds)
Published: Sun Jul 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.