Differences Between Docker Compose and Dockerfile by Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i'm cameron mckenzie i'm the editor chief at the serverside.com and i want to show you the difference between a docker file and a docker compose file now to the left we have the docker file what's a docker file used for it's used to help docker create an image so here you see i want an nginx image basically nginx is a web server i want to stuff my website into that engine x image and then i want to create a custom image based on all of this so the docker file tells docker start with the latest nginx image then stuff all of cameron's website files into it this describes how to create an image now compare and contrast that against the docker compose yaml file to the right the docker compose yaml file describes how we want to run the image when you run images you create a container and the container runs all the software in the image now how do we want to run this image well when it runs we want to give it a name so we'll just call the container my website you have to specify what image you're using well i'm going to use my nginx image which will be created from this docker file and when this container runs i wanted to use one and a half cpus i have a couple of gigs of ram and i want it to map port 8080 to the internally used port 80 of engine x so there is the difference right there the docker file is used to describe how to create an image you create an image with a docker file docker compose describes how the container should run what its name should be how much memory it should get the volumes it can use ports that should be mapped things like that now let's put this theory into action as you can see i've got docker installed i've got docker desktop for windows and look no images there at all so no containers so that's just proof to you that no razzle dazzle is going to be going on here i do have a website you can see this number guesser folder here holds a subfolder called website which is the standard website files there's you see index.html and when i click on that a beautiful little web-based application runs but it runs on the c drive i want to run this on engine x in a docker container so how do i do that well i stuff all of these files into an nginx container that's what i use this docker file for here so i'm going to copy this code i'm going to go right into that number guesser folder and create a new file called docker file interestingly enough no extensions on the docker files not txt it's not yaml it's not html it's just docker file and i'm going to put this content in here start with the official nginx image and then copy all of my website files into it that dot slash means the folder under the current directory and you can see that folder website is under the current directory that the docker file runs in then all you have to do is run your docker build command i actually had it hidden right down in there but the docker build command is this so docker build what else was written there dash t let's give the image a name what do we name it my dash nginx image what else do we need here oh colon latest to give it a version i guess it's not a bad thing to do and then a dot what does that dot mean if you miss that dot it won't work the dot means look for a file named docker file in the same directory that this runs in you can see that it's running in the number guesser directory and there's that docker file in that number guesser directory i'm actually going to go up to images here before i run this command i want you to keep your eye on the list of images i'm going to hit enter boom enter has just been hit docker is now thinking about this command how am i actually going to go about and actually build this image well it's going to download the official engine x image first then it's going to copy all of my website files into it and then it's going to create a new image called my engine x image and there you go that was docker using my docker file to create this nginx image now let's go to containers oh no there's no containers running well that's because docker file just creates an image you create images and then you run containers based on it so how do you run a container well you can use the docker run command but that gets messy a better way is to use describe docker a docker compose file everything that you want to use to configure your container so in the same folder i'm going to come over here create a file called docker compose.yaml yet another markup language yaml this does have an extension right dockerfile doesn't yaml does i'm going to paste this in here and you can just see i'm going to use a i'm going to create a new service to run this service will use the my engine x image and you can see right there you know there's my in nginx image that i'm going to be using that was the one i created with the docker file specify how much memory and cpus to use even do a little port mapping there click control s save the file is saved now all i have to do is go into the same directory as this file do a little ls command and you can see there's docker compose and then i'll just use the docker compose up command i'm actually going to look on the list of containers here as this runs because you'll see things changing very quickly docker compose creating my website running it and you can see up here we've got that number guesser running that was the name of the folder i believe and then it's running my website on port 80 80. so i've now used this docker file that docker file to create the image i've used this docker compose file to help run the image and let's see if this is actually running localhost on port 8080 and look at that all of a sudden this application is running so everything is working swimmingly i'm going to click ctrl c to just stop that and then i might even come over here and junk that container and junk that whole system that's running because i wanted to show you one more little trick here the other cool thing about these services is that you can have multiple services in them so i could add my other service and that could be my other website and i could run this on port 80 88 do a shout out to eric lindros there you know you could even do other crazy things too who knows you could actually maybe even run up a tomcat service as well if you want i'm getting away from the whole idea of the docker file here but this is one of the things about the docker compose you don't have to just have one container listed in here you can actually run many containers at the same time and as long as you don't have any port conflicts in there i might put this on wayne gretzky's port there so 8099 and that should map into i think port 8080 internally but as you can see you can actually map multiple containers here so i've got nginx i've got another nginx running on a different port i've got tomcat in here even and i can save that go back to the command window say docker compose up click enter boom all of a sudden it's now going to download tomcat and java and the jdk that requires tomcat to run you can probably see that happening over here on images then eventually it's going to run that container and when it runs that container everything is going to run at the same time but again i just want to emphasize the key difference here on the left the docker file the docker file is used to build images when you run images that's called a container you run containers based on images and you parameterize them you give them things like cpus memory port mappings port exposures that is what docker compose is used for docker composed is used to parameterize the containers as they run so docker file create images docker po docker compose run containers and parameterize those containers as it runs and as you can see here that docker compose has now run three different files and if i went to port 80 wayne gretzky here i probably would still get my web app right so there it was on 8080. let's reference the great one and boom now all of a sudden it is not running on port 80 99 oh it was 8088 that it was supposed to be running on there we go 8088 and it is working by the way 8099 it looks like it's not working it's oh no 404 status that actually is working you can actually see it saying here it's running tomcat version 10 and that's what this other one was running tomcat version 10 and i'm getting an error message here from tomcat saying look i'm running on port 80 wayne gretzky but nothing has been deployed to me and well if we want to deploy a war file or a jsp or a servlet to that tomcat server well i guess we're going to have to create another docker file but if you want to learn about configuring apache tomcat and deploying war files that's another tutorial but there you go that does prove that all of these are working and it does demonstrate quite clearly the difference between that docker file and docker compose now if you enjoyed that tutorial why don't you head over to the serverside.com i'm the editor-in-chief over at the serverside.com we've got lots of great tutorials on docker kubernetes engine x apache web server tomcat java devops github you name it we've got all sorts of tech info over there now if you're interested in my personal antics you can always follow me on twitter at cameronmcnz but most importantly why don't you subscribe on the youtube
Info
Channel: Cameron McKenzie
Views: 17,644
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, azuredevops, devops, apache, docker, dockercompose, dockerfile, devsecops, cloudnative, cncf, comparison, cloudcomputing, programming, development, software, containers
Id: JmyAMcKUNYA
Channel Id: undefined
Length: 11min 1sec (661 seconds)
Published: Thu May 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.