We will start with some common commands. Then, we'll take a peek at commands that are used for Docker images. We will then take a dive into commands that are used for containers.
The first command we will look at is one of the most useful commands in Docker and in any command-line utility you may use. This is the help command. This is run simply by executing the command, as follows:
$ docker --help
$ docker <command> --help
You will then receive additional information about using the command, such as options, arguments, and descriptions for the arguments. You can also use the Docker version command to gather information about what version of Docker you are running:
$ docker version
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 08:10:07 2017
OS/Arch: linux/amd64
Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 08:10:07 2017
OS/Arch: linux/amd64
Experimental: false
Docker images management
Let's learn how to view which images you currently have that you can run, and let's also search for images on the Docker Hub. Let's first take a look at the docker images command:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 49f7960eb7e4 6 weeks ago 200 MB mariadb 10.1 e98a88b23fa0 7 weeks ago 400 MB
There are a few important pieces to understand from the output that you see. Let's go over the columns and what is contained in each of them. The first column that you see is the repository column. This column contains the name of the repository, as it exists on the Docker Hub. If you were to have a repository that was from some other user's account. The tag column will show you what tag the image has. Image ID is based off a unique 64 hexadecimal digit string of characters. The last two columns are pretty straightforward, the first being the creation date for the image, followed by the virtual size of the image. The size is very important because you want to keep or use images that are very small in size if you plan to move them around a lot.
So let's take a look at how we can search for images that are on the Docker Hub using the Docker commands. The command that we will be looking at is docker search. With the docker search command, you can search based on the different criteria that you are looking for. For example, we can search for all images with the term, mariadb, in their name and see what is available.
The command would go something like the following:
$ docker search mariadb
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mariadb MariaDB is a community-developed fork of M... 2075 [OK]
If we find an image that we want to use, we can simply pull it using its repository name with the docker pull command, as follows:
$ docker pull <image>
The image will be downloaded and show up in our list when we now run the docker images command that we ran earlier.
With the docker rmi command, you can remove unwanted images from your machine:$ docker rmi <image>
-f
flag and specify the image’s short or long ID, then this
command untags and removes all images that match the specified ID.Starting containers
Let's first go over the basics of the docker run command and how to run containers. The most basic way to run a container is as follows:
$ docker run -i -t <image>:<tag> /bin/bash
$ docker run -i -t mariadb:10.1 /bin/bash
Once you are comfortable with your container, you can test how it operates in daemon mode:
$ docker run -d <image>:<tag>
$ docker ps
$ docker run -d
-p <host_port>:<container_port> <image>:<tag>
$ docker logs <id>
Stopping containers
There are a few commands that we can use to do this. They are docker kill and docker stop. Let's cover them briefly as they are fairly straightforward, but let's look at the difference between docker kill and docker stop. The docker kill command will kill the container immediately.$ docker kill <container>
$ docker stop <container>
With the docker rename command, we can change the name that has been randomly generated for the container. When we used the docker run command, a random name was assigned to our container.
$ docker rename <current_container_name> <new_container_name>
$ docker stats
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
30feb5ef3800 0.04% 628.2 MiB / 31.26 GiB 1.96% 9.41 MB / 31 MB 164 MB / 73.7 kB 0
2fb1bdd70ec2 0.04% 384.8 MiB / 31.26 GiB 1.20% 75.3 MB / 45.3 MB 38.5 MB / 783 MB 0
The docker top command gives us a list of all running processes inside the container.
$ docker top <container>
$ docker rm <container>