We have covered the types of installers and what they can be run on. We have also seen how to control the Docker VM that gets created for you and how to use Kitematic. Let's look at some Docker commands that you should be familiar with already. We will start with some common commands and then take a peek at the commands that are used for the Docker images. We will then take a dive into the commands that are used for the containers.
The first command we will be taking a look at will be one of the most useful commands not only in Docker but in any command-line utility you use—the help
command. It is run simply by executing the command as follows:
$ docker help
The earlier command will give you a full list of all the Docker commands at your disposal and a brief description of what each command does. For further help with a particular command, you can run the following:
$ docker <COMMAND> --help
You will then receive additional information on using the command, such as the switches, arguments, and descriptions of the arguments. Similar to the boot2docker version
command we ran earlier, there is also a version
command for the Docker daemon:
$ docker version
Now, this command will give us a little bit more information than the boot2docker command output, as follows:
Client version: 1.7.0 Client API version: 1.19 Go version (client): go1.4.2 Git commit (client): 0baf609 OS/Arch (client): darwin/amd64 Server version: 1.7.0 Server API version: 1.19 Go version (server): go1.4.2 Git commit (server): 0baf609 OS/Arch (server): linux/amd64
This is helpful when you want to see the version of the Docker daemon you may be running to see if you need/want to upgrade.
Next, let's take a dive into the Docker images. You will learn how to view the images you currently have that you can run, search for images on the Docker Hub, and pull them down to your environment, so you can run them. Let's first take a look at the docker images
command. Upon running the command, we will get an output similar to the following output:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu 14.10 ab57dbafeeea 11 days ago 194.5 MB ubuntu trusty 6d4946999d4f 11 days ago 188.3 MB ubuntu latest 6d4946999d4f 11 days ago 188.3 MB
Your output will differ based on whether you have any images at all in your Docker environment or upon what images you do have. There are a few important pieces you need to understand from the output you see. Let's go over the columns and what is contained in each. The first column you see is the REPOSITORY
column; this column contains the name of the repository as it exists in the Docker Hub. If you were to have a repository that was from someone's user account, it may show up as follows:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE scottpgallagher/mysql latest 57df9c7989a1 9 weeks ago 321.7 MB
The next column, the TAG
column, will show you different versions of a repository. As you can see in the preceding example with the Ubuntu repository, there are tag names for the different versions. So, if you want to specify a particular version of a repository in your Dockerfile (as we saw earlier), you are able to. This is useful, so you're not always reliant on having to use the latest version of an operating system and can use the one your application supports the best. It can also help you do backward compatibility testing for your application.
The next column is labeled IMAGE ID
and it is based on a unique 64 hexadecimal digit string of characters. The image ID simplifies this down to the first 12 digits for easier viewing. Imagine if you had to view all 64 bits on one line! You will learn when to use this unique image ID for later tasks.
The last two columns are pretty straightforward; the first being the creation date for the repository, followed by the virtual size of the image. The size is very important as you want to keep or use images that are very small in size if you plan to be moving them around a lot. The smaller the image, the faster is the load time; and who doesn't like it faster?
Okay, so let's look at how we can search for the images that are in the Docker Hub using the Docker commands. The command we will be looking at is docker search
. With the docker search
command, you can search based on the different criteria you are looking for. For example, we can search for all the images with the term ubuntu
in them and see what all is available. Here is what we would get back in our results; it would go as follows:
$ docker search ubuntu
We would get back our results:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating s... 1835 [OK] ubuntu-upstart Upstart is an event-based replacement for ... 26 [OK] tutum/ubuntu Ubuntu image with SSH access. For the root... 25 [OK] torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 25 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components... 10 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 4 [OK] maxexcloo/ubuntu Docker base image built on Ubuntu with Sup... 2 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images... 2 [OK] nimmis/ubuntu This is a docker images different LTS vers... 1 [OK] alsanium/ubuntu Ubuntu Core image for Docker 1 [OK]
Based on these results, we can now decipher some information. We can see the name of the repository, a reduced description, how many people have starred and think it is a good repository, whether it's an official repository; which means it's been approved by the Docker team, as well as if it's an automated build. An automated build is typically a Docker image that is built automatically when a Git repository it is linked to is updated. The code gets updated, the web hook is called, and a new Docker image is built in the Docker Hub. If we find an image we want to use, we can simply pull it using its repository name with the docker pull
command, as follows:
$ docker pull tutum/ubuntu
The image will be downloaded and show up in our list when we perform the docker images
command we ran earlier.
We now know how to search for Docker images and pull them down to our machine. What if we want to get rid of them? That's where the docker rmi
command comes into play. With the docker rmi
command, you can remove unwanted images from your machine(s). So, let's take look at the images we currently have on our machine with the docker images
command. We will get the following:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu 14.10 ab57dbafeeea 11 days ago 194.5 MB ubuntu trusty 6d4946999d4f 11 days ago 188.3 MB ubuntu latest 6d4946999d4f 11 days ago 188.3 MB
We can see that we have duplicate images here taking up space. We can see this by looking at the image ID and seeing the exact image ID for both ubuntu:trusty
and ubuntu:latest
. We now know that ubuntu:trusty
is the latest Ubuntu image, so there is no need to keep them both around. Let's free up some space by removing ubuntu:trusty
and just keeping ubuntu:latest
. We do this by using the docker rmi
command, as follows:
$ docker rmi ubuntu:trusty
If you issue the docker images
command now, you will see that ubuntu:trusty
no longer shows up in your images list and has been removed. Now, you can remove machines based on their image ID as well. But be careful while you do so; in this scenario, not only will you remove ubuntu:trusty
, but you will also remove ubuntu:latest
as they have the same image ID.
We have gone over the images and know how to obtain and manipulate them in some ways. Next, we are going to take a look at what it takes to fire them up and manipulate them. This is the part where the images become containers! Let's first go over the basics of the docker run
command and how to run containers. We will cover some basic docker run
items in this section and more advanced docker run
items in the later chapters. So, let's just look at how to get images up, running, and turned into containers. The most basic way to run a container is as follows:
$ docker run -i -t <image_name>:<tag> /bin/bash
Upon closer inspection of the earlier command, we start off with the docker run
command, followed by two switches: -i
and -t
. The -i
gives us an interactive shell into the running container, the -t
will allocate a pseudo-tty
that, while using interactive processes, must be used together with the -i
switch. You can also use switches together; for example, -it
is commonly used for these two switches. This will help you test the container to see how it operates before running it as a daemon. Once you are comfortable with your container, you can test how it operates in the daemon mode:
$ docker run -d <image_name>:<tag>
If the container is set up correctly and has an entry point setup, you should be able to see the running container by issuing the docker ps
command. You will see something similar to the following:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cc1fefcfa098 ubuntu:14.10 "/bin/bash" 3 seconds ago Up 3 seconds boring_mccarthy
Based on the earlier command, we get a lot of other important information indicating that the container is running. We can see the container ID, the image name that is running, the command that is running to keep the image alive, when the container started, its current status, if any ports were exposed they would be listed here, as well as the name given to the container. Now, these names are random, unless it is specified otherwise by the --name= switch
. You can also the expose the ports on your containers by using the -p
switch as follows:
$ docker run -d -p <host_port>:<container_port> <image>:<tag> $ docker run -d -p 8080:80 ubuntu:14.10
This will run the ubuntu 14.10
container in the demonized mode, exposing port 8080
on the Docker host to port 80
on the running container:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 55cfdcb6beb6 ubuntu:14.10 "/bin/bash" 2 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp babbage
Now, there will come a time when containers don't want to behave. For this, you can see the issues you have by using the docker logs
command. The command is very straightforward. You specify the container you want to see the logs off. For this command, you need to use the container ID or the name of the container from the docker ps
output:
$ docker logs 55cfdcb6beb6
Or:
$ docker logs babbage
You can also get this ID when you first initiate the docker run
command:
$ docker run -d ubuntu:14.10 /bin/bash da92261485db98c7463fffadb43e3f684ea9f47949f287f92408fd0f3e4f2bad