This section explains how to use Docker to run any application inside containers. The Docker installation explained in the previous section also installs the docker command-line utility or the Docker client. Let's explore the docker
command. Using the docker
command consists of passing it a chain of options and commands followed by arguments.
The syntax takes this form:
$ docker [option] [command] [arguments]
# To see help for individual command
$ docker help [command]
To view system wide information about Docker and the Docker version, use the following:
$ sudo docker info
$ sudo docker version
Docker has many subcommands to manage multiple resources managed by the Docker daemon. Here is a list of management commands supported by Docker:
Management command | Description |
| Manages Docker configs |
| Manages containers |
| Manages images |
| Manages networks |
| Manages Swarrn nodes |
| Manages plugins |
| Manages Docker secrets |
| Manages services |
| Manages Docker stacks |
| Manages swarm |
| Manages Docker |
| Manages volumes |
In the next section, we will explore container and image resources.
An image is a lightweight, standalone executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files. Docker images are used to create Docker containers. Images are stored in the Docker Hub.
You can list all of the images that are available in the Docker host by running the Docker images subcommand. The default Docker images will show all top-level images, their repository and tags, and their size:
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wordpress latest c4260b289fc7 10 days ago 406MB
mysql latest c73c7527c03a 2 weeks ago 412MB
hello-world latest 1815c82652c0 2 months ago 1.84kB
Docker will automatically download any image that is not present in the Docker host system. The docker pull
subcommand will always download the image that has the latest tag in that repository if a tag is not provided. If a tag is provided, it pulls the specific image with that tag.
To pull a base image, do the following:
$ sudo docker pull Ubuntu
# To pull specific version
$ sudo docker pull ubuntu:16.04
One of the most important features of Docker is that a lot of people have created Docker images for a variety of purposes. Many of these have been uploaded to Docker Hub. You can easily search for Docker images in the Docker Hub registry by using the docker search subcommand:
$ sudo docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 97 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 76 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components... 30 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images... 22 [OK]
tutum/ubuntu Simple Ubuntu docker images with SSH access 18
To delete an image, run the following:
$ sudo docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:b2ba691d8aac9e5ac3644c0788e3d3823f9e97f757f01d2ddc6eb5458df9d801
Deleted: sha256:05a3bd381fc2470695a35f230afefd7bf978b566253199c4ae5cc96fafa29b37
Deleted: sha256:3a36971a9f14df69f90891bf24dc2b9ed9c2d20959b624eab41bbf126272a023
Please refer to the Docker documentation for the rest of the commands related to Docker images.
A container is a runtime instance of an image. It runs completely isolated from the host environment by default, only accessing host files and ports if configured to do so.
Launching a container is simple, as docker run
passes the image name you would like to run and the command to run this within the container. If the image doesn't exist on your local machine, Docker will attempt to fetch it from the public image registry:
$ sudo docker run --name hello_world ubuntu /bin/echo hello world
In the preceding example, the container will start, print hello world, and then stop. Containers are designed to stop once the command executed within them has exited.
As an example, let's run a container using the latest image in Ubuntu. The combination of the -i
and -t
switches gives you interactive shell access to the container:
$ sudo docker run -it ubuntu root@a5b3bce6ed1b:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
You can list the all containers running on the Docker host using the following:
# To list active containers $ sudo docker ps # To list all containers $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2db72a5a0b99 ubuntu "/bin/echo hello w..." 58 seconds ago Exited (0) 58 seconds ago hello_world
You can also view the information logged by a running container using the following:
$ sudo docker logs hello_world
hello world
You can start a stopped container using the following:
$ sudo docker start hello_world
Similarly, you can use commands such as stop, pause, unpause, reboot, restart, and so on to operate containers.
You can also delete a stopped container using the following:
$ sudo docker delete hello_world
# To delete a running container, use -force parameter
$ sudo docker delete --force [container]
Please refer to the Docker documentation for the rest of the commands related to Docker containers.