Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Find and Run Images

In this video you’ll learn how to find pre-configured Docker images and how to run launch containers from them.

Related Links

2.3 Find and Run Images

Hello everyone. This is Reggie Dawson. Welcome to the Docker for Deployment course for Tuts Plus. In this video, we will learn to find and run Docker images. Now that we have a working instance of Docker, we need images to work with. Fortunately, Docker has something called the Docker Hub where we can download images for our use. If we go to hub.docker.com, we can browse the numerous images available. You can sign up here if you like, but it is not necessary to use the images. If we scroll down a bit, you will see a listing of a few of the official repositories. We also have the option of browsing all the official repositories. Take note that anyone can contribute images to the Docker hub, so the only way to be sure of the content of your images is to build your own or use official repositories. Unofficial repositories could contain anything, so use with caution. If you click on ubuntu, for example, we will be taken to instructions on how to grab the image and what is included. If you look at the top, you will see something that says Docker Pull Command. When we ran the hello world image, it was not present on our system so Docker downloaded it and ran it for us. The pull command is a way for us to download an image to our Docker installation without creating a container from it. Let's open up the quickstart terminal. At the top you will note an IP address. Keep this as this is the IP address we will need for our containers. The Docker pull command will download the image with the latest tag. We could have also added a colon followed by a version number to obtain a specific version. This will grab the specific version that we want. This is useful if an app you are developing needs a specific version of software. If we run the Docker images command, we can list the images that we have stored locally. If we run any of these images, we will not need to download them as they are stored locally. Now that we have our image, we can run it. Here we are running the 12.04 version that we installed. Nothing really happens when we run it, so how do we know it worked? If we run the Docker PS command which shows us running containers, we should see it. Nothing shows. Now if we run the Docker PS dash a command we can see all containers, even those that aren't running. When we did the hello world example, all it did was run the command in the image and exit. It's not that useful if our container does not continue to run, but we will get to that in a minute. First I will show you how to access the terminal from your container with the run command. This time we omitted the version and used the latest version of ubuntu that we downloaded. The i flag creates an interactive connection with the container. The t flag sets up a terminal inside of our container. We are now essentially logged into our container and if we do a dir we can see we are not inside of a Windows file structure. This will be useful if you are comfortable working with the command line in Linux, but many of us will not need to access our container in a terminal. For example, let's say we were setting up a web server. Exit from the interactive terminal and let's go back to the Docker hub. This time we're going to run an Apache web server. Find the Eborus Apache web server on the Docker hub. Once we find the name that we need, we can go back to our Quick Start terminal. This time we're going to use the run command with a few more options. Before I run it, let me explain this command. First we have our Docker run command that is followed by a double hyphen and a name. This is optional and will give our Docker container a name. If we omit this, Docker will come up with its own unique name for our container. After that, we have our dash P 8520:80. This sets the port for our web server. 8520 represents the port on the host machine. 80 represents the port on our web server. This is mapping the web server to the host machine so we can access the web content. Here we are using nonstandard ports, although we could have just used port 80, which is the standard port for web content. We also could have added a port for HTTPS., but we don't need it for this example. Then we have the dash v option which allows us to map a volume from our container to our host machine. The first part represents the file structure on our windows machine. The second part represents a folder in our container. This folder is where the web content is stored for Apache. Therefore when we store our web content on our local folder, we will see it on our web server. And then finally, we have the -d option. This starts the container in detach mode, which does not exit after running like the previous example. And finally, we have the image that we want to run. Notice that I have added the old stable tag. After looking at this image, I found that there was a bug with the latest, so I looked back at the old commits. You can do this on the page for the image on the dock or hub. After that, I decided to use old stable which worked. One of the reasons why I chose this example was to highlight what can happen with unofficial images. This particular image has a known bug that the creator of this image has not addressed yet. As we saw, it went out and downloaded our image and ran our container. Now if we do a Docker ps, we should see our container. If we scroll over to the right, we can also see the port that we set up. Now I have a simple HTML file I have created that just says test, and I have copied it to my local folder that I met with the Docker run command. In order to view our content, we need to browse to our IP. When we first started the quick start terminal we saw an IP address. Use that in your browser followed by a colon and our port number of 8520. We should see the HTML page that we uploaded and we can be sure that our container is running. Now let's go back to our terminal. First, let's try and run the same command. We will get an error about the name being in use. Again if we didn't use a name, Docker would create a container with a randomly generated name. Keep this in mind if we use a randomly generated container name, we will need to know the name to get back to it. Docker psa again will show us all of our containers. Then we would just need to use the name of the container to access it again. Now let's stop our container. The Docker stop command requires the name of the container that you want to stop. Of course if we tried the same run command again from here, we would meet an error as we still have a container with that name. To run an existing container we use the Docker start command. This will start up the container again, and if you do a Docker PS, you will see the container running. The last command I want to show you is Docker restart. This command stops and then starts our container for us. This is the quick way to restart your container if you are experiencing problems or made some sort of change you need to take effect. You now know how to find and run images from the command line. In the next video we will work with Gipematic and I will show you how to use the gooey interface to configure your containers.

Back to the top