Push Docker Images to GitHub Container Registry • #docker #containers #github #githubactions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's learn how to publish your Docker images to github's built-in container registry so one feature that I don't think a lot of people understand that GitHub has is what they call their packages feature and packages let you publish your code you're kind of compiled or package up code whether it's uh you know a binary or or an npm package if you work at a JavaScript ecosystem or even a Docker container up into your GitHub profile so if you notice on your GitHub profile I have mine on screen right now there is this packages tab up here and if I click on that you can see I actually have my own npm package that I was playing around with several years ago that's published up here but any packages you publish into GitHub packages would show here inside of your GitHub profile so as I mentioned earlier you can actually publish Docker images up into GitHub as well so this specific portion of the GitHub package is called the GitHub container registry or ghcr.io is the URL so what we're going to do is we're actually going to log in using our Docker login process to the GitHub container registry now when you do this using Docker normally you would use your username and password to log in however you can't do that that with GitHub you have to use what's called a personal access token which is kind of like a secondary password that you can use to do things within GitHub without actually having to log in directly so we're going to start by creating one of those personal access tokens so we can log into the GitHub container registry locally using Docker on our computer so from within GitHub in the upper right hand corner drop down your profile and go down to settings on the left hand side scroll down all the way to the bottom and you can see this entry called developer settings and select that and then finally under personal access tokens drop that down and now there are two options you have here fine grain tokens are the newer version of tokens that are in beta but the docker login doesn't actually support these so we're going to select tokens classic and open this up now you can see I have several tokens that were created here already but we're going to create a new one I will create a new classic token and it's if you have two-factor authentication which you definitely should it's going to prompt you for your authentication code okay now that I've entered my two factor I am on the page where you would create one of these personal access tokens for your account so I'm going to name this ghcr login So within the token you can actually automatically expire the tokens we could select seven days since this is not really going to be used after these videos go out and then you have a series of Scopes which give your token permission to do specific things within um within GitHub the ones we want to select are write and read packages and then delete packages so this will 100 be able to manage the packages section of our GitHub profile scroll to the bottom and click generate token and now in this kind of green section in your GitHub profile you'll see that there's a token here now this will not be usable after you navigate away from this page it's a best practice that when these kinds of Secrets and tokens are created not to let them GitHub doesn't store them in any way so there's no way you're actually going to be able to get these back so you want to copy this now and put it off to the side so you can use it for the login process and now that I've copied mine I'm going to head over to vs code and we're going to open a terminal log in and publish an image so this is the repository or the project I've been working with for the last couple of videos and you can see we have a Docker file at an entry point just a quick recap this entry point.sh simply echoes.hello World it doesn't do a whole lot and the docker file simply wraps that entry point at sh script inside of an image so we can execute it within the context of Docker inside of a container so I'm gonna open a terminal and then we have to log in to the the GitHub container registry endpoint so we could say Docker login username is going to be me Morrison Dev well actually use your your username is going to be whatever your GitHub username is mine is be Morrison Dev so you want to replace that with whatever you log into GitHub as and we can double hyphen password and then we're going to paste in that token that personal access token that we created in the previous step and then the final thing here if you don't specify anything the docker login CLI is going to or the docker CLI is going to try and log into the Container or the docker Hub registry which we don't want we want to log into ghcr.io which is the GitHub container registry hit enter see you get a little message that says it's insecure but that's fine since we're storing this locally in our computer and it says login succeeded so now the next step is we have to actually build our container and when we build the container you can you would know go through the normal process of Docker build and then a period to build the files but when you tag the image that you want to build it has to follow this convention so it would be ghcr.io which is the repository we're going to publish to forward slash your GitHub username which in my case again is B Morris and Dev Dev forward slash whatever the name of your package is or image so in this case we'll do hello world ghcr and then we'll tag that with the latest as the version and click enter this is going to build our container and set us up to be able to publish it up into the GitHub container registry so looks like that process is done so now we can say Docker push ghcr dot IO forward slash vmorrison Dev or what again whatever your username is slash hello world ghcr colon latest okay and it says it refers to the repository URL that we specified in there let's go ahead it's it's building or it's rather it's pushing the container up or the container image up into GitHub registry and looks like that's finished now in order to show this running what I'm actually going to do is I'm going to say Docker image LS and then I'm going to grab B morsendev so you can see there are two images that I currently have locally on my computer so let's remove these I will say Docker image RM and then we're going to take the first I'll expand this out a little bit just so you can see it's better I'm going to take the first three characters of our hashes we have here so seven three six is the first one and eight nine B is the second one it says those images are being referenced so let's just force it this is all for demo purposes anyway so whatever containers we're using it are now blown away as well so now I if I run that LS command again and grab B Morse and Dev I get no output which means these images have been deleted locally from Docker so now if I say Docker run and then I say ghcr.io forward slash B Morrison Dev forward slash hello world uh ghcr latest Docker says it can't find the image locally so what it's going to do is download it from that URL the ghcr the GitHub container registry pull it down and then run it locally and you can see we have Hello World which was what we were echoing out from our image in the first place now that I'm back in GitHub if I select the packages tab here we can see we now have this hello world ghcr which is our our Docker image that we just published and if I click on that it's going to give us a little bit more information about it including the actual command you would use to pull that image down to your computer and then any tagged versions We would create going forward let's expand on this concept just a bit and see what it would take in order to automatically publish new versions of our containers using GitHub actions so what I'm going to do is I'm actually going to head back to my list of repositories that I have I have this repository published Docker Hub images that I was again I had used in one of the previous videos we're going to use this and set up an action that is going to automatically publish new version of my container anytime that I push new code to this repository so we don't want to store our personal access token inside the code itself so we want to create a new GitHub secret we can do that by clicking the settings tab up here on the left hand side I'm going to select secrets and variables and then actions and then let's create a new repository secret okay so I'm going to name this secret GH underscore Pat for GitHub underscore personal access token but I'm abbreviating it and I'm going to paste the token in here and click add Secret now let's head over to vs code and set up our workflow okay so I've got vs code open and I already have my DOT GitHub workflows directory since again I have used this I've used this repository previously but I'm going to create a new file here and let's just call this uh publish hyphen ghcr.yaml now this other workflow that I have in here only has the workflow dispatch um trigger enabled so I have to manually trigger that but we're going to do something different here we're actually going to set it up so this way every time new code is pushed into the repository it's going to rebuild our container image and publish it I'm going to name this uh Docker image CI for ghcr for GitHub container registry we're going to set the trigger to push so any pushes into the repository are going to trigger this and then I'm going to say we're going to have our jobs here we're going to have one job I'm going to call it build and publish and let's see runs on is going to be Ubuntu latest so it's going to run on a Linux machine powered by Ubuntu as far as our steps we're going to have two steps so the first one is going to be an unnamed step uses actions forward slash checkout at V3 which is the current version of the checkout action this is just going to pull our code from the repository into the actions Runner so this way it can build the code and the second one we're gonna the Second Step here is gonna be build and push the image and then this is just going to be an inline script and fix that capitalization on image this is just going to be an inline script so I'll type in run put a pipe there and then the first one the first command that we're going to enter here is going to be Docker login username B Morris and Dev password and then instead of pasting in the personal access token we're going to use that secret that we created so I'll say I'll type in a dollar sign with two curly brackets Secrets sec-r-e-t-s dot GH underscore p-a-t to pull that secret in and then we're going to pass an URL here of ghcr.io so this is going to log Us in now we'll type in the commands to build the image so Docker build period the tag is going to be ghcr.io forward slash B Morrison Dev forward slash hello world ghcr latest like so and then finally Docker push and then instead of typing all of that in again I'm just going to copy and paste it from this line above here and save that uh so again this workflow will have two essentially two steps the first one is just to pull down the latest version of the code and the second one is a series of inline scripts that will log in to the GitHub container registry service using Docker login it's going to build the image and then it's finally it's going to push the image into our GitHub packages Repository so I'm going to open my terminal back up I'm going to type clear to get some space here and then let's go ahead and commit to these files so I'll say git add dot get commit m a new workflow just call it and then we'll do get push and then the remote that I have set up for this specific git or the specific registry is called GitHub because I have it in multiple places so git push GitHub is going to push it to the remote if you only have one remote configured for your git client just push it directly up there don't have to type in this extra bit here so I'll hit enter we could see that this was pushed now let's head up to GitHub and see if our action is triggered okay so at the root of that repository I'm going to go ahead and select the actions tab we can see new workflow is the name of the commit and it's already running here we have the get Docker image CI for ghcr workflow on the left hand side now if I click inside this this run here we can see the build and publish it already had finished so everything that we had said to do it did already it logged in uh let's see here's our here's our operation to log into GitHub container registry here's all of our build output here and then finally here is the output from pushing that image up into the GitHub container registry if I head back to my account and go to packages GitHub container registry we can see this was pushed about one minute ago now as one final demonstration let's go ahead and make a change to our code we can actually do this directly within um within GitHub I'm going to go to go back to the repository that we're working with I'm going to come to my entry point.sh and we're just going to modify this we'll say hello world or hello from ghcr.io commit the changes and commit the changes again and now if I head over to actions this is going to run this should run automatically I'm going to refresh this page because sometimes it takes a moment for that to update and we can see just the just by updating a single file committing the code and pushing it up into GitHub action or pushing it up into GitHub action's already detected that that was a trigger for our new workflow and is going to build and publish the newest version of our container image so we're going to wait a minute for this to finish and then we're going to flip over to the terminal on my computer oh it's already finished let's go to the terminal on my computer and we'll try and run the latest version of this container image okay so we're on the computer and I have the terminal open I'm going to say Docker run ghcr.io forward slash B Morris and Dev forward slash hello world ghcr latest and it just ran hello world that's because it's using the version that's on the on my computer locally but if I say Docker image RM let's see Docker image LS grep the Morrison Dev we see we have that one so I'll say Docker image RM seven three six is going to remove it and I'm gonna force it to get rid of that container okay so now the current version is removed let's go ahead and run this one more time say I can't find it locally so it's going to download the latest version of that image and pull it down just like it did before but now the output here says hello from dhcr dot IO and notice how we didn't actually have to build a container and push it up that was all handled by our GitHub action the second that we made a slight change to the code and that is essentially how not only you would push images into the GitHub container registry but also use a GitHub action to perform that operation automatically so you don't have to do it with every single code change you make hopefully by now you have a good understanding on how you can publish your own Docker images into GitHub container registry if you've ever worked with GitHub container registry whether that's with Docker images or by publishing things like as npm packages do me a favor let me know in the comments below what you thought about the platform and if you enjoyed this video you like you might like my other video on how to publish images into Docker Hub I'll see in the next one bye-bye
Info
Channel: Brian Morrison
Views: 19,337
Rating: undefined out of 5
Keywords: github container registry, github containers, docker push, docker, github, github actions, devops, docker tutorial, github packages, github package registry, docker registry, containers, docker images, container, docker image, docker build, upload docker image, learn docker, docker - pushing and pulling, ghcr, github actions docker
Id: RgZyX-e6W9E
Channel Id: undefined
Length: 14min 42sec (882 seconds)
Published: Wed May 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.