2.5 Manage Containers and Images
1.Introduction1 lesson, 00:43
2.Starting With Docker6 lessons, 29:40
3.Web Servers With Docker3 lessons, 14:26
4.Conclusion1 lesson, 00:44
2.5 Manage Containers and Images
Hello everyone. This is Reggie Dawson. Welcome to the Docker for Deployment course for Tuts+. Now that we know how to find images and create containers, we just need to learn a few more commands to manage them. We can of course delete a container from Kitematic by simply clicking on the X next to the container name. After clicking the X, you'll be prompted with a pop up window. Click on Remove in this window. Understand that even though we delete the container, the image still exists. Let's go over to our terminal. First we will use docker ps to list our running containers. Remember that this only lists our running containers. We have to use a docker ps -8 to show all of our containers. If our container is stopped, we can use the docker rm command to remove a container. We can use either our container name or our container ID to remove. If we do another docker ps -8, we'll see that the container is gone. Remember I said that removing the container does not affect the image, which is still stored on your system. To check what images we have locally, remember we use the docker images command. This will show you all the images you have stored locally. We can of course delete any images we don't need anymore. The command to delete an image is docker rmi followed by the name or the image ID. Again, we can use the image name or the image ID. And in this case, when I used the image name, I had to add the tag, since we know we have two versions stored locally. One thing to take note of is that you will not be able to remove an image that a container uses. In that case, we would need to use the -f flag with this command in order to force it to remove the image. Now, after we have our container created, we can easily use these containers to create our own custom images. Remember when we set up our Apache container? Well, when we did, we added a custom setting exposing a volume from our container. The base image does not have this exposed, and if we run the image from Kitematic, it will not allow us to share our content with our container. Now, if this were a one-off situation it would be no big deal to use the terminal. But what if we were rolling out images to multiple developers who are all using Kitematic? To avoid that, we can save the container we created as a custom image. First let's run a docker ps. Here we need to find the container ID or the container name that we want to use to make an image. Once we have this, we can use docker commit. The commit command is similar to the same command we would use with a version control system such as Git. Incidentally, we could have also included options that would have allowed us to add a message and alter information to this commit. The command first takes the container name, then we have the image name in the format of a user name followed by the name of our image. This is more of a Docker convention and what you will normally see on the Docker Hub. Remember the eBoris Apache that we used? We can also add an optional tag to our image if we like, but if not, it will be tagged as latest. After we run this command, if we run docker images we should see our new image. Also, the run command we need to use is now much simpler. We have our name, the d flag to run our container in detach mode, and finally our image name. We don't have to add a tag, since Docker looks for the latest tag by default. Now, before we continue, I just want to let you know that there is another way to build your own images by using a Dockerfile. This Dockerfile includes instructions on how to build an image. For the scope of this course, we won't go into depth on Dockerfiles, as we should be able to do enough through creating images from customized containers. I just wanted you to be aware of creating images this way, and have included links to the documentation on Dockerfiles if you want to investigate further. Now if we run this command and then take a look at Kinematic, and we should see our image running. Unfortunately, this image has some issues and our ports and volumes are not working properly, probably due to the underlying container that it is based on. No worries, though, as I will show the ports and volumes being mapped from a custom image in a later video. We created this container just to show the process of creating images. Let's say this image was working properly, but if we wanted to use it, we still had to go to the terminal to launch it. What if we just wanted to start a new container from the image in Kitematic? That's where Docker Hub comes in. We can sign up for Docker Hub and upload our images. We can also set our images to be either public or private. After that, accessing them from Kitematic is simple. First let's go to the Docker Hub and sign up. After signing up, check your email to verify your account, and log into the Docker Hub. Now, we could create a repository from here, but we will create it through our terminal. Now, before we push our image, I want to show you a way to change the name of our image if we decided to do so. Let's say we weren't happy with the name of our image. We can use the tag command to change it. Now if we check with docker images, you will see a new image with the same image ID of the shared Apache image. All this is is the same image but with an alternate name. If we like, we could even delete the original. Now the only thing left to do is run our Docker push. Now, if you get authentication errors when you try to run this command, make sure you run the Docker login command. Now I'm already logged in, but if I wasn't, I would just supply my username and password I used when I registered to Docker Hub. Now we can go ahead and run our docker push. Our image has been pushed up to the Docker Hub. And if we look on the Docker Hub, we can see that our image has indeed been uploaded. We can also see any associated tags with our image, and incidentally, if we ever want to delete our image, we can do so from the Settings link. Then finally go over to Kitematic and click on Log in to log into the Docker Hub. Once we're logged in, click on My Repos and click on Create to install your image. And now the image we uploaded to the Docker Hub is running in our Kitematic installation. Our image is now ready to be distributed. Now you fully understand how to create and manage your images in Docker. In the next video, we will learn about networking our containers.