How to use Docker in Web3 | Learn Web3 | Developer DAO
Developer DAO · 10 Aug 2023 · 1:06:31
0xThresh, a Chainlink hackathon winner, teaches a Developer DAO community workshop on Docker for web3 development, hosted by BillyJitsu. The session explains images and containers, installing Docker, finding public images such as the Ethereum client on Docker Hub, building containers and custom images from a Dockerfile, and practical tips on naming, ignore files and mounting local directories to keep secrets out of an image. A starter repository supports following along.
Speakers
Topics
From Developer DAO; community: Developer DAO.
Transcript
foreign Welcome to our next Community Workshop it's new rebranded you know all skills no Shields with chain link hackathon winner Xerox thrash he is going to give us a Lowdown on Docker and why to even use it how to use it in web 3 for people that struggle like me I've been trying to learn Docker and I don't care about the history of Docker I don't care who invented it I don't care why I should love it I just want to learn how to use it right so thrash is going to give us a Lowdown of like why you need it how are you going to use it what are the scripts to get started no better than than a hackathon winner right so I'm gonna let you take over the show thrush let him know what's up the the audience is yours
sweet sounds good thanks Billy I'm going to teach you about some Docker today a couple ways this is going to go through I've got some slides I've got some live walk through the main thing to know real quick is that as we're going through this um there is a quick start repo in GitHub highly recommend if you're wanting to follow along and maybe have some of this stuff for later reference without having to re-watch the whole presentation that you check this out this has all of the commands that we're going to run and all of the details that I'm going to walk through in the workshop here so this will be a good starting point for anybody who wants to follow along all right and with that here's what we're going to talk about today the first thing we're going to talk about is what
is Docker I promise it's going to be super quick we're not going to give you the life story we're going to talk about installing Docker we're going to talk about finding public images we're going to find we're going to talk about building containers going to talk about building your own images with your software that you've written and then we're going to give you some tips tips and tricks once you've got the basics down so what is docker well for those of you who haven't already heard of it Docker is a software platform that allows you to build test and deploy applications quickly using containers and containers are just a fancy piece of software that enables you to configure your application once build it as many times as you want in a repeatable manner and it enables you to distribute your software to other developers and allows them to use your
code even faster so when you're wanting to get your software out there and drive adoption containers are a great way to go just make it as easy as possible for everybody to get the quick start that they need so that they can start running your Suite application as soon as they want and here is the origin of how we got to Docker originally if you're a Dev you've probably said the words it works on my machine before when it didn't work on somebody else's Docker is a solution that says all right we'll just ship your whole machine then but no shipping your laptop to your customers required so that's how we're going to use our Docker images today is to get your it works on my machine out to the rest of the world all right some quick terminology these are going to be really important because
I'm going to use these words a lot through the rest of this presentation the first word you're going to want to remember is an image a Docker image is a blueprint of what needs to be on a running container including your code Library configuration settings and any other dependencies that you might need to give to somebody else in order for them to run your application it's similar to Distributing something like a node modules or a package.json or anything like that it's just so that whatever somebody tries to run your software they've got the same starting point as you so that's really what the image is going to do for us there the next word you're going to want to remember is a container the container is the live running version of your application that is built from that
image and we're going to talk about these a lot through the rest of the workshop and it'll probably start making a little more sense just to give you a kind of a quick visual on this as well this is straight from the docker documentation for uh this image that we've got here but basically you've got these images it can be python it can be redis it can be node.js anything that you're Distributing as part of your application that can be what makes up your image and that is used to build as many containers with it as you want it can be one running on your local machine it can be a thousand running in the cloud at the same time doesn't matter the image is going to be whatever you need to help build that container all right so now we're going to talk about
installing Docker and hopefully this will go pretty quick for some people but if you want to follow along want to make sure you've got the time and instructions on how you want to do this real quick so installing Docker is super easy and thankfully it is also free the way that we're going to do this is we're going to go to docker.com products slash personal here and I've got this in another tab over here so we're just going to go right over there you can see there is this get started for free page you can go ahead and sign up for an account if you don't already have one just need to give them a username email password and you should be ready to go in my case I do already have an account so I'm going to do this sign in here instead all right and here we are on the docker
Hub page and you'll notice right away that there is a download the docker desktop application we've got options for Windows Linux or either type of Mac software or Mac processor that you might have so just go ahead and click on the one that's best for you get that install going and we should be good to go the good news for everybody is I'm going to do some talking for a little bit now so you have some time to go ahead and get all that installed while I kind of go through Docker Hub and show you the rest of that so that should give you a few minutes to let that install run and do what you need to do there all right so we're going to talk about finding public images so a Public Image is really going to be any software that some specific
company or project or anybody else wants to distribute to the public and when they do that they post their images somewhere publicly that everybody else can go grab those so that you're able to use their software without having to run their npm install or their pip install or whatever else that you might need to do if you were to just try to run their software directly on your machine the best starting point for this is always going to be Docker Hub which you can just find at hub.docker.com again this is the most common place for projects to publicly store images that they maintain and once you find an image that you want to use we'll end up using a couple of commands to grab those you can see some examples here but we'll show you the rest of those later let's go over to the hub
page for a little bit and kind of look through this so coming back to that same hub.docker page that we were talking about before you can see there's a few things here there is a few container images right here on the front page for us which is great that a lot of you are looking for that node.js one we'll be using that later in the demonstration so we'll get a view of that but given that this is a web 3 presentation I'm going to go ahead and start with something that we're probably pull in pretty commonly which is going to be an ethereum client so in this search bar of the talk of Docker Hub we can search for anything any type of software that we might want to use a container image of and see if they're already hosting something publicly for us which is awesome in our
case we're going to use the ethereum client go as our example for these next few sections here and you can see that as soon as I search ethereum it's one of the first things that pops up so I'm going to go ahead and click on this one and right away there's some awesome info for us about how to run this container what it's made up of and how we can use it so just to kind of give a quick overview this container image is what we would use to run an actual ethereum client and it's made by the ethereum foundation we see some info about when it was last updated so they last made a new version of this 25 days ago and we can see that this has been pulled by developers over 10 million times in their particular case for this top right
part you can also see that they give us the command right here that we need to go download this container image if we wanted to which is awesome they're all anytime you go search for any container image this poll command will always be here so that you can just copy that you don't need to remember the commands you can just hit the copy on this paste it into your terminal and be Off to the Races now before we go over to that uh terminal do just want to show a couple more things that are worth talking about on this page so this is our landing page again we've got our Docker pull command a lot of really good info from the project maintainers about their software and what we can do with it there is one other tab here I want to point out to
everybody though and that is this tags tab here so there is always kind of a default image that you might want to push out to people or that you may want to pull in the case of the ethereum client they probably they have like a pretty minimal version of the ethereum client that they assume is going to be good for the majority of their users the thing you'll notice down here though is that there is more images and the one I'm going to focus in on here is this all tools latest so you can see that this is significantly larger almost or over five times larger than the base image that they distribute to everybody this is because presumably this has all of the tools that an advanced user of the ethereum client might need and so they do distribute that as a separate
image with a separate tag in order for those people to still have that image available without having to manually install all those extra tools on the image themselves but ethereum is also assuming that the majority of users aren't going to need these so why add the extra five times worth of storage on their hard disk when there's a very high chance they're not going to need these tools so whenever you're looking at some of these public images it is worth going into this tags section and just making sure that you're getting the best container for your use case a lot of the time in this overview tab they'll kind of explain the differences and we can look at a node.js1 as well where they do a better job of kind of breaking down the differences between tags but just another way of pointing this out is you
can see that some of these are for an arm64 architecture some are for an AMD 64 architecture so they're Distributing these separately just based on what your CPU architecture on your computer might be just to make sure that you're going to get the best experience based on what image you're going to pull the other thing that's very nice about this is that you see we still have these Docker pull commands with the exact tag and the format that you would need so if we want to pull this all tools latest we're just going to click this copy button right next to that paste it into our terminal just like we would have with the same pull command on the original page and we'll get the exact version of that image that we're looking for so just another helpful thing to know about Docker Hub there's probably more than
one use case and you should probably check to make sure you're getting the best image experience that you can get just based on what they're Distributing real quick we can just go take a look at the node.js one as well since that is another very common one to use and here you can see on the node.js image that they have even more tags just another example of how to think about this here are some node.js version 20 node.js version 18 version 16. so they have different versions that have node.js and npm already installed for you on this container so that again as part of building your images you don't have to do that part manually it's just already going to have that stuff ready to go for you and installed and you just need to choose basically which version of this
matches your project for whatever your code is going to do so just another one to point out where they've got a lot of different tags and a lot of different functionality that you can pull from here and we will use this node image later so you'll kind of get to see what I mean by them making it easy on us with this all right and with that we'll come back over here I'm hoping at this point people have had a chance to get Docker desktop installed if they wanted to go ahead and follow along with some of this so that's kind of Docker Hub and we're going to start actually running some commands shortly here and getting into it the other part to talk about with finding public images so we got through Docker Hub the next part is actually finding the docker files on somebody's
GitHub repo so sticking with our go ethereum client example there I'm going to come back to their GitHub and you can see here in the ethereum go GitHub repo where they distribute the ethereum client they do have a Docker file here and this Docker file is the instructions to actually build the image and we'll walk through all of that in a bit but the main thing to note with this is that when you are going through somebody's GitHub you should pay attention to see if there is a Docker file there because if there's a Docker file rather than walking through all of these separate steps that we're going to have to do in order to build the source and do all of these configurations and everything else we can actually just go
down and look for any info on running this with Docker and it's going to be a simpler process here I've got that in my URL there so so we don't have to keep scrolling but so here's their Docker quick start section in their repository and you can see they have the exact command here ready to go for us to pull their image actually run this as a container and just start our own ethereum client without having to do any of of the manual installation so that's going to be super helpful we'll do that in a terminal in just a second here so that you can see what that's going to look like but just another way to keep an eye on what you can grab from other projects publicly there's a very good chance that they're going to be Distributing a Docker file that you can
just pull get started with without having to do all of the extra config and installation to actually run their software so this can be great for things like chain link nodes it can be great for things like file coin nodes ethereum nodes any other node that you may need to run this is usually a great place to start is just go see if the the specific project has a Docker file or a Docker image on Docker Hub that you're able to pull from so that you can get that quick start for yourself and now that we're through that let's start talking about building some containers from these images so when using images from Docker Hub we can actually pull and build a container in the exact same command because it's smart enough to do that pull part for us so we're going to go
over to our terminal and start running some commands here so that everybody you can see us start to build these containers and get an idea of what this actually looks like one thing to note you'll notice that on this command there are quite a few Flags we'll break these down in the terminal but just know that one does not simply run Docker without any arguments there's good reasons to have these here if you just run a Docker command with no arguments whatsoever usually you're not going to have a good time and we'll kind of talk through what that looks like in a minute here at this point we're going to go over to our vs code and we're in our web3 Docker quick start here so hopefully you all have cloned that and had a second to kind of check that out and we are going to go ahead and pull
and run the ethereum client image that we saw there and so if you're following along in the GitHub this is right under the building containers from Docker Hub section there's a command kind of right at the top of that that's where I'm pulling this from here and you can see that this is what we're getting ready to run here now before we actually run this let's talk about what we're doing here and what each of these flags actually means so Docker is always our base command that we're going to start with no matter what if we're working with Docker if you're using Docker Docker has a lot of sub commands you'll see a lot of them throughout the rest of this Workshop here but the first one we're going to start with here is this Docker run and what Docker run is doing is telling Docker to take a
specific image and actually turn it into a running container uh that image name is going to be your very last argument so as you can see here we have our ethereum client go if you're looking back at Docker Hub that is going to be a perfect match with our ethereum client go that we have on this page here so when we did our Docker pull ethereum client go that is the exact name of the container image that we're actually going to pull here now again in this case this command is going to be smart enough to do the pull part for us if we have not already done the poll manually so we'll just go ahead and go through that the first flag to note here is this Dash D what Dash d means in a Docker context
is detached so if I don't have this Dash D here basically what's going to happen is my live running terminal here is going to connect directly to the container that we run and the process that we start with it in my case I just want to start the ethereum node I don't necessarily want to connect my terminal to it so this Dash D flag is just going to make it so that the container is going to run without essentially grabbing our terminal section and forcibly attaching it to the container that we spin up the next flag that we have here is a name for and I'm just naming this ethereum node since I want to know exactly what this is doing basically if you don't include this dash dash name flag when you do a Docker run Docker
will give a automatically generated name to your container which is fine but if you start spending up four or five different containers at a time you're going to start to lose track of which one is which and you're not going to really know what you're doing anymore so whatever I'm spinning up containers I tend to try to give them a name just so I know which one is which and I'm not losing track of everything as I start to run multiple of these the next flag we have here you'll see that we have this twice is this Dash p dash P is for Port so I'm sure you're all familiar like as part of your own software especially if you're using stuff like ethers where you need to set like a specific endpoint as your RPC endpoint you're familiar with setting a
specific port to that you might be used to seeing like 80 80 or Port 3000 or something like that that's exactly what we're doing here with this Dash p flag is we're saying this container that's going to run with this software I want that container to be listening on this port and when I forward connections from my local machine to that container Port I want it to be listening on that exact same port so really what that means is with these two numbers here the first one is a local Port that our machine is going to listen on and the second Port is the port that it's going to get forwarded to on the container so basically the container is listening on the exact same port that our host would if we were to start this ethereum client
just on our machine instead and it just ensures that any traffic that we would normally send to this port 85 45 that should be for our ethereum client it's going to get to our container that's running that client and the same traffic will pass through exactly how we would expect it to if the container weren't involved at all you can see that we've got the dash P flag here again this one is just for Port 30303 so we're just passing another Port through no change here this is just something else that we want the container to listen on and then again like we said at the beginning here this last command or this last argument is just the specific image that we want to pull from when we run this container so that's kind of the breakdown of this command and what we're actually doing at
this point we can go ahead and run this and we should get a live running container once we do that and if you see an ID like this it means you're in good shape you now have a running container that is a running ethereum node on your machine this isn't the most perfectly helpful thing in the world though because you might be looking at this and wondering okay now what here's what we do now so another helpful command to be aware of is Docker PS and this PS is essentially short for process and it's going to show us all of the docker containers that we have running right now so as you can see here when we run that Docker PS we get all of this output about all of the different things that are running on
this container so it's telling us the name it's telling us the and I'm trying to clean this up a little bit so it lays out there we go that's a little better so it's going to give us that container ID this is just the short version of the long ID that it's spat out up here above it's going to tell us what image we spun this up from which could be another helpful way for you to keep track of which container is which this is the command that is actually executed when the when the container starts so in the case of this it needed to run the geek command to start the geese client so that's the command that it ran here's how long ago we created it this is how long it's been online and here are the ports that are getting forwarded as part
of our container build and then lastly we have the name that we gave the container which was just this ethereum node from up above so that's how we can look at the container that we have running now what do we do with it at this point we're going to kind of look through some of the ways that we can perform specific actions with this container that we just spun up one of the first ways that we can do this is using a Docker exec command and what Docker exec is going to do is this is going to allow us to execute a specific command against the container that we have running this can be really useful to do things like make sure that a file that you think should be there is written to the system or in our case we're even going to get a full running
shell on the system so that we can run a terminal on this and take any actions against it that we would like if this was running on our local machine so real quick let's do another quick breakdown of what this command is actually doing so we talked through the docker exec we have this Dash it this Dash it is a little bit is almost opposite of the Dash D where Dash D is saying I want this to be detached Dash I is saying that we want this to be interactive and what that means is we actually want to connect our terminal to the output of whatever command we're going to run this time and then the t is also telling it that this is actually for a terminal so that we get a little bit nicer input back from the container if you don't include the T it just kind
of shows us like a blank and you can't really tell that you're connected to something but if you run commands it'll still work but the T just kind of makes it a little bit nicer of a user interface for you so you can keep track of what's Happening this ethereum node here just matches up to the name of the container again so we ran an ethereum node container and this is just the name of that this is just telling it which container that we want to execute this command against and then our last part here is Dash bit slash bin shell really what this is doing is saying we want to run the bin shell command which is just basically the terminal that we're going to run on this container so really what we're doing is starting a terminal that we can connect to on the container since we
have the dash it and here we are you can see that this dropped me into a terminal and if I just print working directory actually right now it's just on root but let's just change the directory to root and do ls-al and the reason I'm doing that is just because you can see here that here is our DOT ethereum folder that you would be used to seeing if you were to ever start a geek client and if we CD into that you can see that we have all of the GIF files and folders that you would normally expect to see on any running geek node so at this point we're on the container we're actually connected to it with the terminal and we can see all of the stuff that we need to hear so let's say for whatever reason we were using
this client but we're having issues like sending any of our requests outbound and we think that it might be because we don't have a key set or something like that connecting our terminal to this allows us to actually go through and check all of that and kind of do some live debugging on our container so that if something isn't working the way we expected it to we have a way to come in here and actually check it out and make sure that everything is in here as we'd expect it to so connecting this shell to your running container instance is a really handy debug tool it's also really useful for coming in here and doing things like looking at logs or making sure that your app is actually running with the right command and things like that so you don't necessarily have to use a bin shell when you're doing exec
you can actually just run any command and that you might want and we have another example of that later on but this is kind of a quick intro to Docker exec and probably one of the most useful things to do with it is connect a live terminal to your container just so you can see what's going on in there so what other helpful interaction command to know for your Docker containers getting a full shell on it is useful but sometimes you actually just want to connect to the process so like let's say you have an Express server which for the non-javascript people is just a simple JavaScript web server that you can run and we'll be in our example later uh and you know that you needed to run like a yarn start in order to start your system and you know that you have a lot of
logging going straight to that process as part of the way that you've built the app well if your yarn star is the command that your container starts with and you want to see those logs directly the way you would as if you were running it in your own terminal there's a command that can help us with that and that command is Docker attach so Docker attach is going to connect us to the live process us that the container kicked off with so in our case we're going to do Docker attach and once again we're going to do our ethereum node which is just the name of the container again that we've been interacting with and so we'll hit enter on this and you don't see anything happening right now but what I'm going to do here is do a control C and it's going to feed
that directly to the geek process which is actually going to shut my ethereum node down because I'm now connected directly to this process so if I do my control C there it is you can see that we got the interrupt and that it's actually starting to shut down so this is just like hitting command C on like any server that you're running or any other process that you might be running in your terminal and it went straight to the container instance because we did this Docker attach so when I did that control C the running process took that as oh I'm supposed to be shutting down the geek process stopped and actually as a result of this if we do our Docker PS again to look at our running containers you're going to see that the container stopped altogether because of the
process that it was running with got an interrupt when I sent it back control C so the docker attached can be a pretty useful way to watch the live output of the process that you ran in your container just to see whatever is going on with it at the moment so Docker attach and Docker exec are two of the most useful commands to kind of interact with your running containers and get an idea of what's going on with them we are good to move on to our next section here we're going to talk about building containers from a Docker file so with this one that we had over here we were just pulling directly from the docker hub using this ethereum client go in order to pull from Docker Hub get the
image that they're hosting publicly there and kind of run it that way but the other example we talked about was when a specific project uh shows you a Docker file or distributes that in their GitHub repo so that's the example we're kind of going to hit here next so I'm just going to clear out this side and we're going to focus on this left terminal here and basically to keep going with our ethereum and ethereum go client example I'm actually going to clone their GitHub repo and if you're following along in my GitHub repo this is right under the building containers from dockerfile section uh and there's a four command section here that we're going to run through I'm just running the first one here now and so we're going to go ahead and clone
this this part does take a second this is a fairly substantial repo that they have here all right we have cloned our GitHub repo and we are ready to go here so I am just going to CD into that and sorry that is go ethereum here and then following along in that same section there are those four commands that we want to run we've done our get clone and our CD now before I run this next one I do just real quick want to show that here is the docker file inside this go ethereum repository here and you can see that this next command is a Docker build I'm going to put a period Dash T geeth all right let's break this command down again for everybody so now we're running
a Docker build with the sub command and what build is going to do is it's going to take a Docker file the specific type of file and it's going to turn it into a live image that we can then use in our Docker run command to create a container based on the image that we build from these instructions in the docker file here this period here is just setting the build context so since the docker file is in this go ethereum folder we're actually just pointing it to the current directory and saying there's a Docker file here so I want you to build the docker file from this directory you can set this to something else but the most
common format that you're probably going to see for this is going to be this Docker build period and it's always again just setting the build context you're basically telling Docker where is the docker file that I want you to use for this build and then this Dash T flag here is setting a tag on the image um the tag on the image is basically telling you when you do a list of all of your images that you have locally what image is there and what is this particular image named again this is another place where you can get a lot of sprawl it my own machine probably has a hundred container images right now just locally and if I don't add a tag to kind of keep an idea of which one is which it
makes it a lot harder to keep track of what your images are when you have so many of them installed on your machine so definitely always recommend adding the stash T flag even though it's not specifically required in order to run this cool all right so for right now we're ready to go we're going to build our Docker file to create our image and I'm going to hit enter here and here we are this is the expected output here what Docker is doing right now is reading all of these instructions from the docker file and doing all of the steps that it needs to in order to build the image and it's going to tell you piece by piece what it's doing with that so basically it's just running through each of these steps in this Docker file and reporting to you as it's doing it it's even telling us how long
each specific step is taking so as you can see most of this up until now has been pretty quick but this go Mod download part is taking the longest this is another great example of why Distributing your software as a Docker container can be so helpful because let's say you have a thousand people that you want to send your software to and this is taking up to five minutes for this go download or this npm install or for anything like that to occur instead of making them do that process and have to wait for all of the stuff to install to their local machine before they can get running these installations have all been run directly on your Docker image you send them the docker image and they're able to just pull that and immediately start running your
software without having to do the installations or any other configuration steps that might be required so this is another great example of why Distributing your software as an image can be so helpful to drive adoption and just let people start working with your software as quickly as possible and my tangent ended just in time because here we are we've got a built Docker image ready to go now before we actually run a container with this I want to show you one more useful command here just to kind of keep track of things we have our image and so what we're going to do now is this Docker image LS again image is just another sub command and LS is just like what you're used to seeing if you run it on a directory it's just telling us what images we have on our local machine here
so a sub command of this image command and so when I run that again this is what I was talking about where I have all kinds of different repositories here but you can see at the very top we have our geef image that we just built and you can see that this was built 43 seconds ago and it's taking 64 megabytes up on our system so rather than pulling that container image from Docker Hub where it's hosted publicly we now actually just took the docker file from ethereum goes repository their GitHub repository and we just built that image for ourselves on our local system so again rather than pulling this from somewhere remote like you would if you were cloning some software from GitHub
we're actually building the exact same thing that they have hosted publicly except now we've got it on our machine and if we wanted to make any modifications to their Docker file in order to better suit our needs for what we need our ethereum client to do we could absolutely do that rebuild the image and run the container with whatever steps that we need uh in addition to what they would normally we do but again that Docker image LS is just going to show you everything that you've got installed locally here and now you're good to go all right and the next part here is actually running the container that we just built with that image so the last step in that four command line that we have in the Builder contain
building containers from Docker file section of the GitHub repo is this Docker run dash dash dash name test Keith and Keith quick breakdown again this should look a little familiar by now but just to make sure we're all still following Docker run is just what we're using to build a live container with the software this Dash D is just putting it in detached mode so that we are not connecting our terminal directly to the container as soon as it spins up we're giving it a name again just so we can keep track of which container is which don't have a thousand names of nonsense that mean nothing to us and can't keep track of which one and then geef is the image name that we just used in our Docker build command so from this Dash T flag in our Docker build that's now what we're using in our Docker run
to point it to the image that we just built in that last command and so I'm going to go ahead and run that and once again good news we've got our container ID here which means that we were able to start our container successfully and this looks very similar to the output that we had when we did the uh Docker poll and the docker build from the image that was on Docker Hub uh we basically have the exact same ports open we have everything ready to go um honestly in this Docker run I should have actually even had the dash P Flags again just so that our local system was forwarding the port connections to that but if we do our Docker exec again on this and do it on test geeth the name of the container and do our bin shell and
come back here and put our Dash it so we get our nice terminal input here and if we do that once again we can see that here is all of our ethereum files so we have the exact same image from building it ourselves that we would have got from pulling it from Docker hub from ethereum so that's kind of the power of that is this enables us to build this from anybody's GitHub repo so that any project that doesn't have their stuff on Docker Hub but does distribute a Docker file in their GitHub repo this enables us to build that image ourselves so that we can still use their software as a container all right so coming back to our slides here here's those four commands that
were the same as what we had in the GitHub repo there so congrats you've made it this far we know what an image is we know where to find them we know how to build them but we've only looked at other people's images so far what if what about when you write your own dope software and you want to put that out to the world and let everybody else use it so this is the part where we're going to actually talk about building an image from what you've written and the code that you have all right so coming back to our terminals here at this point we're going to go ahead and start talking about the docker file instructions and what building this actually looks like so in order to build a Docker file there's a few things that you want to be aware of going into it
and it's really just having some familiarity with your own software when you've written your software what needs to happen before somebody is able to use it completely removing Docker from the discussion here for a second like if somebody wants to run your software in the repo that you've built what do they need to do first is their software dependencies they need to install are there config files that they need to make updates to a lot of the common stuff that you see in a readme where it's like update your dot EnV don't check it in don't remove it from the git ignore all of those types of things um those are the types of things you want to have in your head before you start trying to build the docker image for it now just to give you a quick demo of the software that we have here that
we're going to use for this you know actually just kill these real quick all right we're back to just our quick start here so this quick start is what we have open in our left panel here and you can see there's not a whole lot but there is this server.ts that we want to take a look at here is my extremely awesome Express server that's going to change the world and I can't wait for everybody to use it and all this is really doing is taking a get request and when we get the get requests it's just spitting out a Docker makes life easy and this is just like any other JavaScript project and I happen to know that as the person who wrote this there's two things somebody needs to do before they can run this I need them to run yarn install and then I
need them to run yarn start so the first thing we're going to do just like we would with any other project is I'm going to do my yarn install given that I've been playing with this for a couple days you can see I've already got everything there but your yarn install might take a few seconds just to get everything it needs not a ton of packages in here so hopefully it's pretty quick now that I've got my dependencies I'm going to run my yarn start and as you might be used to seeing with any other Express server there it is it's ready to go it's listening on Port 8080. so I'm going to go ahead just to make sure that I know this is working correctly I'm going to go curl this on localhost port 8080 since that's where I'm running my web server right now
and there we go Docker makes life easy couldn't have said it better myself so that's our running Express server that should be you know it's just like any other software that you might run whether it's a flash server in Python whether it's like a lens or a chain link external adapter like whatever it might be that you've written we're just starting this the exact same way and we're going to kind of feed that instruction right to our Docker container now so at this point I am going to kill my web server there and we're going to start talking about what's going to happen in the docker file in order for us to get this exact same output but in the container instead of on our local machine so in this Docker file you can see there's a handful of instructions here
and we're going to go line by line through these the first thing I'm going to advise everybody of if you're using Docker consistently there are a lot of good plugins especially for vs code I'm sure IntelliJ stuff probably has all of this as well but if there is a Docker extension out there definitely download it and you can see I've got just the regular Docker one installed right there so definitely grab that it's going to make your life a lot easier and I can show you exactly what I mean by that so here are all of these Docker file instructions and when I'm hovering over any one of these instructions thanks to the plugin I've got the live info straight from docker's documentation about what each of these instructions is doing which is extremely
helpful because docker's documentation is very hard to read it's very hard to get the info that you need it's extremely informative and in depth which is great but it's so hard to read and I say that as somebody who's been dealing with these for a long time now that we've got our extension installed let's go through these instructions so the first thing we're doing here is we're saying from node 1620. what does this mean well the from command sets the base image for us to use for subsequent instructions what that means is that we are pulling this node 1620 image as our starting point and I can show you exactly what we mean by that once again if we go back to the docker hub
here is the node image and somewhere in here that I'm not going to try to find the exact one but here's close enough right there are these 16 20 images and what that is doing is pulling node version 16.20 and installing all of its dependencies we're pulling the image that has all of this stuff installed already so that I don't also need to go through the process of trying to install npm and node and yarn and all of these other tools that I might need in order to enable somebody to use this container so really what this is doing is it's pulling from an image that has all of that stuff installed for us already and saving us a bunch of time so that we don't have to go do all of those steps so pulling from with your Docker
containers is usually a very good idea the question is okay to deploy on Docker desktop absolutely I personally am used to the CLI flow so that's what I use you do see that I do have a Docker desktop over here this is a very useful interface especially if you're not as comfortable with the CLI but do want to still use some of the functionality of Docker they've made it very easy to do a lot of the functionality in here now like instead of running my Docker kill and Docker RM on one of my containers I could just click this delete button here get rid of it all that way so to answer the question yes you are more than okay to do whatever you want in Docker desktop I'm just showing the workflow that I'm a little more used to but if
this is more comfortable for you highly recommend it but all right coming back over here so that's our starting point is this from node so that we have node.js npm all that good stuff installed the next command is copy and copy is copying files or folders from source which is the first argument to destination in the images file system so in our example here's what we're doing we're copying everything in our current directory seeing as the docker file is here and all of these other files are included in this directory it's copying all of this into the slash opt Express directory in the container image so when we actually spin this container up after
building the image it will have all of these files added into the slash Ops Express directory and we'll connect to it once we build it just so you can see exactly what I mean by that this next command is work done and workter is setting the working directory of any subsequent commands and really it's just like running a CD so if I were to run CD mounts over here workter is doing the exact same thing except in the context of the image build and the reason I do that worked over there is because if all of my yarn packages and all of my software is in this directory because of that's because that's where we copied it all to I want to make sure that I run my yarn install
inside this same directory so that it actually knows where it's uh package lock and all of that is going to be so we're changing to the directory that's going to have all of these contents and then we're running our yarn install inside that directory so that it has exactly what it needs to go execute that and that's really all this run command is doing is just running whatever command that you want it to so in our case we had two steps before somebody could start our software it was a yarn install and a yarn start so this is meeting the requirements to install those dependencies inside the image so that anyone who spends a container up from it already has that stuff installed they don't have to sit there and wait for it this next command here is expose 8080.
what exposed does is actually make sure that the container can listen on the port that we set so in our ethereum client example earlier we were passing the dash P flag for Port 8545 and 30303 in order for us to actually pass traffic to that Port that Docker file that we built from would have needed to have those two ports exposed in the docker file and in our case since we have an Express server that's listening on port 8080 we want to go ahead and expose port 8080 on the container so that when that traffic gets sent to that container on port 8080 it's already listening for it and it's expecting traffic on that port
and then our last uh instruction here is the entry point the entry point is the command that's actually going to run as soon as we start the docker container so this is the second part where now we did our yarn install earlier but now when somebody starts our container we want it to run yarn start and that will start up the express server and let that container be ready to go listening for web requests a quick note about the syntax of this you're going to notice obviously that it's a little different from this run up above and you might be wondering why does entry point need to be like this whereas run is like this I do forget off the top of my head if this is like a strict requirement to have it laid out like this but basically entry point has like there's a syntax
difference that allows Docker to kind of handle some of this better in the background and that's really the reason for this syntax change here and you can see when you hover over the instruction it does give you this exact same format here where it has the brackets and and then a quote for each of the things that you want to run with a comma between any of the different arguments so without getting in the semantics of why that is because to be honest I even forget off the top of my head just be aware that entry point has a slightly different syntax here um and you just want to pay attention to that whenever you're setting the entry point command that's going to run when we start that container at this point we did it we built our Docker file and now that we have the blueprint it's time to
actually build the image with it so we're going to come back to our GitHub repo here and again now we're in this building your own images section here and there is a Docker build command that is towards the top here it's after that first paragraph in there and so here's what we're going to run and we'll break this down again just the way we have been the whole time so we have Docker build and again this is just saying take a Docker file and turn it into an image the dash T is once again just giving this a name I want to remember what this image is the dash T is going to give me the ability to do that so I'm going to give it the dash T to say this is my Docker Express image that I can use spin a container up later
and then once again we're just doing the current directory which is what that period stands for again this is just because of ILS this we see that in this current directory and actually this might even be a more helpful way to kind of demonstrate that but in our current directory we have this Docker file right here so when I do this Docker build Dash T Docker Express and point it to that current directory with the period it is just defaulting to using this Docker file one quick note if you do have more than one Docker file you can actually give this a specific path to that as well to tell it which Docker file to build but if there's only one dot or the docker file name that you see here is
kind of the default so if this is present in a directory and you you just tell it to build in the directory that has that Docker file it's just going to default to that one and it's going to choose that but all right here we go let's go ahead and build this thing I forgot something Docker build would be helpful in front of the dash T there knew I couldn't get through the whole thing perfect all right and just like we did with the geek node earlier we're going to run a build this one is much smaller we only have a few packages that we've got to install but you can see in the output here that we've got our Specific Instructions in each of these and you can see that happening as it's doing the
build process now it is exporting to the image and now we should have a perfectly good to use image on our machine and just to check again we can once again do our Docker image LS this is just checking all of the locally installed images that we have and when I do that you can see that actually I already had this one somewhere in here yeah so I didn't make any updates to this since I last built it which is why it's showing us the 16 hours ago when you all run that it should be the first one that pops up here and it should say that it was just built a few seconds ago since it again mine was cached it had all of this already built from when I was running through this last night but same thing here this is my Docker
Express image this is where I'm wanting to see it and we know that we have it on our local system so now that we've built our image it's once again time to actually run the container with it uh so if you're following along in the GitHub repo we're going to move past where we have the output should be similar to this and we have another Docker run command with again the breakdown of what these are going to be doing and let's talk through this one more time again y'all should be getting pretty used to seeing these by now but we've got our Docker run we've got our Dash free d for detached it's not going to connect our terminal to it we've got our Dash p to tell it what ports to use so that when we send our curl request to port 8080 our local
system will be listening on port 8080 and it'll forward it to the containers port 8080 so that the express server running on our container is getting that request from our local machine we are once again naming our container so that we can keep track of which one is which and we're using the docker Express image that we just built in our last command up here as the basis of what we want to run this container with so let's go ahead and start that and yet again we get a container ID which means that that built successfully if I do my Docker PS again there is my test Express with all of the info about what's running in this including the yarn start as the starting command so remember when we started the express
server on our machine we did our yarn install we did our yarn start and then I had that directly in my terminal just showing all of the uh web requests that came to that now that's running on the container instead so the express server is still running but it is it within this container that we just built so at this point once again we should be able to do a curl for localhost on 8080 and we get the exact same result back so this container that's listening on port 8080 is running the exact same Express server that we were running on our machine when we did yarn start except now it's running within this container and we're able to distribute this to anybody who wants to use our software
the same way so that's kind of the very quick overview of how to build your own images with software that you've written just going back through the Section here yeah curling that's the last step there and exactly as you can see we got the same result we were happy it did exactly what we wanted it to but all right let's take a quick breath everybody uh we've got a lot that we went through and the last step that we're going to go through is some extra tips and tricks to help you go even further with Docker now that we've gotten through the basics here here is our tips and tricks and I have not forgotten the security question and we're about to get to the point where we're going to talk about that before we get there tip number one name all the things I've been harping on it
the entire presentation and I'm gonna harp on it again here if you don't name your images when you do a Docker build Dash T and you don't name your containers with the dash dash name when you do a Docker run it's just very easy to lose track of all of the things that you've built it might not seem like that big of a deal when you've only got that one image and that couple of containers that you've built so far I can tell you though as somebody who uses this in my workflows all the time my image list is a disaster and I'll show you a screenshot in just a second of what this starts looking like but image tags help you keep track of the names just to know which image is which the other thing that we didn't really do in the presentation but we did kind of
point out in Docker Hub is you can use that Dash T to specify more specific tags as well so this is kind of a good example if we go back to the docker Hub here for node where there is that latest tag there's also these other tags so just to kind of show a very quick example of what that would look like if we were doing our Docker build for Docker Express basically if we had made some change to this where let's say this is running now with node node.js18 instead of node.js16 like we chose we could actually put a colon here and do a node.js-18 just as an example now we still have our same Docker Express image name but we're giving it this extra tag
to help people know which exact version might be the best fit for them so again coming back to Docker Hub you can kind of see what this looks like and you can see here that this Docker pull node colon slim is that exact same format this is the specific tag that gives you an even better idea underneath that image name of what a specific version uh that image is so that's another way that you can kind of further break this down if you have multiple versions of the same image that you want to give to people this is kind of a helpful way to do that just to give people the different versions and idea of what they might be running so that's one thing to keep in mind for your images and then with your container names again dash dash name flag if you don't do that
all oops sorry if you don't do that all you're going to get is the container IDs like this and again starts getting pretty hard to keep track of what's what after you've got about 10 of these running at the same time so tip there is just name everything keep track of your stuff later you is definitely going to thank you for that here's what it looks like when you run a Docker image LS when you don't follow my advice there this is a disaster I don't know what the vast majority of these are and look at how much space they're taking I'm wasting gigs and gigs and gigs with these images which you can see on the sides with the size flag over here I'm wasting multiple gigs of file storage and I have no idea what all of these even are so this is why you want
to do that Docker build Dash T because otherwise you're going to start looking like it's on my computer and you definitely do not want that so use that Dash T flag keep an eye on your stuff and name all the things the next trick is use dot Docker ignore to reduce bloat all right come into the security question let's talk about Docker security yeah when it comes to downloading public images you do want to use the same amount of caution that you would with anything else just like you shouldn't necessarily like blindly clone a GitHub repo and immediately feed its setup.sh into your bash with you know running as root without any idea of what it's doing same thing is true for a Docker container maybe get some idea of what's
going on there first do just a tiny bit of due diligence and make sure that they're like the official images from Docker Hub as well something else that you can keep an eye on there is like this Docker official image so when this comes to node always be looking for these if you see a node look alike but you don't see like node actually in your URL and you don't see this Docker official image that should be setting some red flags off for you where you're thinking maybe this isn't the container I'm looking for I should double check this is the same thing too where it's like maybe don't click the link that just got sent to you randomly through Discord same thing applies here like if you get a really random Docker Hub link that has no verification there's like no downloads on it it looks super sketchy
you're probably right it probably is sketchy you probably don't want to run it so I didn't have that in the presentation but it's a great question there's a few things to keep an eye out for when it comes to downloading your own images from public sources well that's one side of the security story the other side of the security story is what you're publishing just like you shouldn't be adding your dot end to your public GitHub repo you also want to be careful about what type of what type of stuff you're adding to your Docker images let me tell you a story there was a Docker image that was posted for Vine which was the old school Tick Tock back before Tick Tock was a thing Twitter bought it all of that a developer accidentally posted source
code and secrets to a Docker image that made its way to Docker Hub everybody was able to go clone that image and create containers from it and they essentially had a breach that information that was supposed to be private to Twitter was now public to the entire world so one thing to keep an eye on is when I'm building an image and actually posting that to Docker Hub if I go that far with it what am I posting and am I being careful about what I'm putting in there the best way to kind of keep track of what you're doing and what you're putting into your image is by using a handy file that is called a DOT Docker ignore this doc Docker ignore really does two things for you it's just like a git ignore first of all like so you can think of it the
exact same way fills the same type of function but it helps you prevent bloat on your images it helps you prevent adding a bunch of stuff on your image that doesn't strictly need to be there just from a bloat perspective so that you're not making your images run slower or take up too much uh disk space when they don't need to stuff like that it can also help you make sure that you are not adding Secrets or anything that you don't want in that image as part of that as well so if we go back to our repo here we have a DOT Docker ignore and we haven't touched on this yet but I'll show you all what this all is here so this format is going to be exactly the same as what you're used to a DOT
get ignore like I said the thing to note about the difference with the dot Docker ignore is that what this file is really doing is it's keeping an eye on your instructions and we have this copy command and we re-ran that copy command as part of our image we copied all of the contents of this folder and we put them in this location on our image what this Docker ignore is doing is saying hey you can copy everything from this directory but anything that I have specified in this file is not going to be copied into the image so a few examples we have our DOT get as part of this repo that is completely irrelevant to what's running on the on the container it doesn't need to be
there in order for the container to run so instead of including all of this info about my branches and all of the things that I have on my GitHub repo we're just going to Docker ignore it and it's not going to get included in the image again because it just flat out doesn't need to be there same thing with the dot get ignore it's not helping anything the yarn lock and node modules this stuff kind of could be up for debate there might be cases where you actually would want to include this in your image just so that anybody who runs it has the exact same packages and starting point in general though it I tend to think that maybe it's not the best idea we're already going through the process of like installing everything that's needed as part of the docker file so in this
case I just went ahead and included those in the docker ignore and didn't add them there may be cases where you actually want to include these in there though so kind of a case-by-case basis to Think Through on what you would want to include in here so that's kind of the overview of the docker ignore and what that does again it's just to kind of help you reduce anything out that doesn't need to be in there and maybe exclude files that have secrets as well just to show you all too I have this slash Mount folder and then this dot EnV I'm going to commit a cardinal sin and show you the dot EnV because it is definitely not a real password so I promise I'm not putting my uh wallet key out there for you all to get all of my stuff out of there but
let's say you have this dot EnV and it does have a private key to a wallet because you need to build a signer in ethers and you need to be able to submit transactions as part of your code you don't want to include that in your image if you're going to push that image up to Docker Hub and have that be publicly hosted so this Docker ignore is really targeting this dot EnV within this dot Mount but if it was just a DOT EnV in this same exact folder same thing you would just specify the dot EnV folder sorry dot EnV file in your Docker ignore and it'll make sure not to copy any of those files into that container if that's not an intended purpose of that all right and that's pretty much all I've got on the dot Docker ignore again just think about it as a DOT get
ignore always make sure you use it it's one of those things I personally try to include in every Docker container that I build just to make sure that you're not going to include anything that doesn't need to be there all right our last trick mounting local directories in a container there are a few different ways to do this we're going to focus on a pretty basic use case here it's possible to mount directories on your local on your local file system in a container so that the container has access to them this is another place where specific to web3 I think this can be pretty useful let's say you have that folder with a EnV or your dot ethereum client if you're running that locally uh anything else that might be a secret and you want it to be inside the container image but you don't want to copy or
sorry you want it to be in the docker container that's running so it has access to your secrets but you don't want to include it in your image so that when you push your image out everybody who clones that doesn't have access to it mounting local directories can be a good way to kind of solve that problem uh it can also just be super convenient if let's say you have a piece of software that like makes a bunch of edits to an Excel file and you don't want to copy that file in you just want to mount it from your local directory that's another use case that might be really useful to just use a mount in that case and kind of run it that way so we're going to go back to our command line here and I'm going to clear all this stuff out and if you're still with
me and following along in GitHub this is close to the very end here this is in the trick mounting local directories in a container and it's the first of two commands that we have there and I'm copying directly from that myself and this is going to look pretty familiar to you at this point but there's a specific flag that we need to point out here and it's this Dash V Dash V is for volume and we've talked about how you take something from local and move it into remote and anything on the other side of this colon is usually going to be at the remote right so just like here with our Port we have port 8080 on the local side on the other side of the colon is the remote side where we're going to actually put that once we or what port is going to listen on the
container same thing here on this side of the colon this is what uh directory we want to mount from our local machine and this is the location within the container that we're going to mount that file or directory to this PWD thing I hope that isn't too confusing basically that's just taking the output of the print working directory command which just grabs the exact directory that you're in and places it in front of this slash Mount just so that the repo doesn't make any assumptions about where you've cloned this and potentially mess you up when you're trying to copy and paste this command later so that's really all that that's doing but let's actually take this again together and we're going to run a new container this time with a volume mount
oh this failed and if we take a look at why it's because I'm still running a container with port 8080 we can't use the same port twice so real quick good troubleshooting exercise I'm going to do a Docker kill on test Express and then I'm going to do our Docker run again this is freeing port 8080 up so we should be able to do this now except now I built a container that failed on the last run so I have to delete the container that failed before I can do it sorry about that but again hopefully hopeful so I'm going to Docker RM mounted Express there we go if I do a Docker container LS okay I have no containers running
there's nothing going on anymore this time it should really work third time's the charm we got our image ID back if I run my Docker PS again there we go we have a new one named Docker Express and here's our other example of Docker exec which I promised you all earlier Docker exec again is just going to run a specific command on the container that we tell it to so to break this down one last time mounted Express is the container that we just created in our Command Above This is the entire command that we're now going to run in that container so this time instead of doing the dash it and including the dash bin bash to actually connect the live
terminal to the system all we're doing is running a cat inside the system and if you don't know what the cat command does it just reads out the contents of a file to your screen so this time we're just running a cat on optexpress secret dot end the reason we're choosing that specific uh directory there is because when we did our Mount um how did I actually get the wrong maybe even actually gotten the wrong one there let's try that okay somewhere along the way I jumbled up my terminal there but when we ran our Docker uh run this time I'll copy the command that we originally pulled from there
this time when we did that we took the contents of our slash Mount which is what's in this folder right here in the GitHub repo and you can see that there is this dot end we took the contents of dot Mount and mounted them to this opt Express secret so when we're running this cat opt Express secret dot end this should be showing us the contents of this file over here since this is now mounted from my local file system directly into the Container itself and as you can see we have our super secret password that is now printed to my screen because the mount worked successfully so rather than this file being there because of the fact that we copied everything in here the way that
this actually worked was when I ran this copy of all of these files in the directory this file did not make it and the reason it didn't make it is because I ignored the mount folder in my DOT get ignore which prevented this whole folder from getting mounted in there but when we ran our Command we added this volume Mount which now takes the contents of this Mount folder puts it at this opt Express secret location and then when we ran our Docker exec against that container with a cat to read that file out that's how we ended up with the contents that we were expecting there so that's kind of a quick overview of mounting your local file system and
getting files in there that way again this can be a useful way to add in secrets to your container that you don't want present in the image or just to make things a little bit more convenient if you just want to mount your entire documents folder in there and take actions against them in there like that at this point though that's all I got folks thanks for sticking with me thanks for tuning in if there's any other questions I'm definitely happy to answer them but that's basically all I've got to teach you about Docker today
Related talks
- Learn about Oracles with API3
- Onboarding the Next Generation of Ethereum Builders with BuidlGuidl
- DevNTell - Nader Dabit - Smart Contract Development with Foundry
- Dev & Tell | Dhaiwat - web3-ui
- Dev & Tell | Giancarlo - Dappify - Build3r
- Build Your First ZK App with Noir
- Introduction to Scaffold-ETH 2 and ScaffoldBase
- Beyond GitHub: Building Open Source on Radicle
- Building on Ethereum in the AI Era
