From the course: Docker on Azure (2019)

From app to containerized app

From the course: Docker on Azure (2019)

Start my 1-month free trial

From app to containerized app

- So the first thing we want to do, before we really get into running within the cloud, is just look at how a container actually works. We're going to do that in our directory here, if we do an ls, we have a Docker file which describes the process for building our environment. Actually we can just look at that, real quick, more DockerFile, Do+tab should get us there, capital D by the way. And, we can see that we're going to pull from another image, in this case, the nginx image, we're going to grab the latest version. Because we haven't defined a larger path, it's going to use the default Docker container registry which is the Docker hub registry. We pass an environment variable and an argument, so we can either pass in at build time or in the environment, when we run the system, we can pass in a version string and that will get used. We have a hostname.sh, we'll look at that in just a second, and that's actually going to execute some tasks for us and launch our process. And we can see that we have, the last line in the docker file is a command which describes what's the default command that the Docker container is going to run when we execute it. And if we look at the readme document, the readme document describes the basic steps that we're going to run through. First we're going to build our instance, we're going to call it hostname, it's going to get a default tag of 'latest' because it's the first and latest thing that gets built. And we're going to tag it with version one as well, just to create more consistency in our overall naming. We're going to run the image, both passing the remove which means, when it's done running, go ahead and clean up the container, don't just leave it on disk anymore. We're going to name it, so that we have an easier name to work with when we're trying to manipulate the container, like, stopping it. And we're going to expose the port, we're mapping port 8080 on the localhost to port 80 on the container, so it's outside to inside, left to right, in this particular case. A -d means demonize which means go ahead and run this in the background for me. And then we want to pass what image we want to run. We can test it by curling against the localhost, on port 8080, that's what we're going to expose with the -p parameter in the Docker run command. And we're going to stop the instance by calling out the name that we've provided for the Docker run command. Lastly we're going to clean up our host resources, the images that we built, on both the latest version and the v1 version and that will then remove them from our system, keeping our system nice and tidy. So we're going to go ahead and run through these things again. And, first we're going to build our container, so we're going to use the docker, d-o-c-k-e-r, build ., the local directory, so it's going to look for a docker file there. And then we're going to tag it at the same time with just the hostname and it will add latest to that tag. So first it has to go find the nginx base image and it's pulling that down from Docker hub. And then we go ahead and build that and now our container is built and it was tagged with hostname 'latest', we're going to add another tag just for good measure. Docker tag hostname 'latest' hostname 'version one' and, before we actually run this, just to look at the hostname script, real quick. So this is the script that's going to be run, so our container is now going to run itself within it's own little environment, it has all the resources it needs to launch and run the nginx application and it's going to emit a couple of data documents, if we request them the right way. So if we just ask for the default index file, we're going to get an HTML document that includes the hostname or the container name and the version string that we pass it. In addition to that, we could ask for the /hostname path, as long as we include a trailing slash, and we'll just get a text version of that information. Or we could just get the version string, if we ask for the /version/ endpoint, or if we want to get just, effectively, a 200 response, an OK response, we can ask for the /healthz endpoint, we'll actually get the text 'healthy' as well. So we can now go ahead and run this, so docker run. We're going to clean up with rm. We're going to name it hostname. We're going to demonize it. We also want to expose port 80 on the container to port 8080 on the localhost. So 8080 localhost, 80 on the container and then we're going to run hostname version one. Great, so that should be running. Oh we can do a docker ps just to see and sure nuff there it is. And then if we do a curl on localhost, against port 8080, we see our basic body. And just to sort of complete it hostname/, we just get the text. Version/, we just get the version. And healthz, we get that it's healthy, perfect. So now we can do docker stop hostname, 'cause that's the name that we provided and you can see that, that actually exists, in the docker ps output, the very last parameter is hostname. Takes it a second to realize that we really do want to stop it. And then we can go ahead and clean up, so docker remove image hostname:latest and hostname:v1, just to make sure that we don't lose a tag. And now we're back to our starting position, with nothing in our Docker environment.

Contents