Introduction

What is Docker?

Docker is an open source container based technology.

What is a Container?

To more easily explain what containers are, let’s compare them to Virtual Machines:

  • A Virtual Machine includes the application, the necessary binaries and libraries and a guest operating system. This means that it tries to do everything a computer has to do, which is quite resource intensive.
  • A Container is different from a Virtual Machine. It’s like a box that includes the application and all the dependencies associate with it, but shares the host operating system’s kernel. You can think of it as a lightweight server that contains just enough for it to be whatever it needs to be.

“The Kernel” of an operating system is a collection of software that is the minimum needed to run various hardware. It has complete access and control over everything in and pertaining to the system and is the first thing loaded of your system. It also contains drivers, the bits of code that communicate with and control hardware. [ref: /r/explainlikeimfive ]

Why use Docker?

In short, with Docker, only our application and it’s dependencies are contained, which makes it extremely fast and extremely portable, and we don’t have to worry about the OS.

Step 1 — Installing Docker on Ubuntu

Update your package index:

Install the necessary packages so apt can use packages over HTTPS:

Add the official Docker repository’s GPG key to your system:

Add the Docker APT repository to your system’s APT sources:

Update your list of packages again, since we just added the Docker repository:

Now we can install the latest version of Docker CE (Community Edition):

After the installation is complete, the Docker service will start automatically. You can check it’s status by running the following command:

The output will look something like this:

sudo systemctl status docker
● docker.service -- Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-11-25 09:25:30 PST; 44min ago
     Docs: https://docs.docker.com
 Main PID: 442 (dockerd)
    Tasks: 19
   CGroup: /system.slice/docker.service
           ├─442 /usr/bin/dockerd -H fd://
           └─470 docker-containerd --config /var/run/docker/containerd/containerd.toml
lines 1-19/19 (END)

If you find that the terminal is locked in the systemctl command’s output as it displays the status information of the requested service ( docker in this case ), then just press q to quit.

You’ve now installed Docker. This gives you the Docker service and also the Docker Client, or docker command line utility. We can start using the docker command to check our Docker version. Here’s my version at the time of writing:

docker -v
Docker version 18.09.0, build 4d60db4

Step 2 — Allowing Non-sudo Access (Optional)

This step is optional.

If you’d like to execute the docker command without being root or always prefixing it with sudo, then you’ll have to add your username to the docker group:

To apply the new membership, you’ll have to log out of your current user by typing exit or CTRL+D and logging back in. You can then test if the changes applied by checking if the user is now part of the docker group:

id -nG
vlad sudo docker

NOTE: In the rest of this tutorial we’ll assume that you’re running the docker command as a user that’s not part of the docker group, so we will prefix sudo for every command, but you don’t have to if you’ve added your current user to the docker group.

The reason we continue this way is because some readers may just scan this tutorial for commands.

Step 3 — Docker Command Basics

The docker command has the following structure:

To view all the available commands, just run:

The list of commands and subcommands in Docker 18 includes:

To get help on a specific [subcommand] then just run:

For system-wide information on Docker, run:

Step 4 — Intro to Docker Images

Docker containers are built from Docker images.

What is a Docker image? To put it simply, a Docker image is basically a template that you use for your container. It’s a binary that includes all the requirements and instructions necessary to run a Docker container.

Most images are kept at Docker Hub, which is a Docker registry managed by Docker, the company behind the Docker project. By default, Docker is configured to automatically pull images from there when you request them. It’s easy to use and a great resource featuring excellent Docker images. There you’ll find most applications and Linux distributions that you need.

Check if you have access and can download images from Docker Hub with the mandatory hello world example:

The output will look something like this:

sudo docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.

As you may tell from the output, docker was initially unable to find the hello-world image locally, so it downloaded it from Docker Hub, created a container from it, and ran the application within the container, which displayed the Hello from Docker [...] message.

Searching for Docker Images

You can also search for various images, using the search subcommand. The script will search Docker Hub and list all images matching the string you used:

Output

OK in the OFFICIAL column indicates that the image is created and supported by the company behind the software

Downloading (Pulling) Docker Images

If you decide what image you want to use, you can download it to your machine by using the pull subcommand.

For example, we’ll download the official WordPress image. To do this run:

After it finishes downloading the image, the output will look something like this:

After the image is downloaded, you can run a container using it using the run command, as we’ve done in the hello-world example. If the image has not yet been downloaded, Docker will attempt to pull it from Docker Hub, and then run a container using it.

Listing Docker Images

To list all of our downloaded images just run:

Example output

sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              75835a67d134        6 weeks ago         200MB
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB

Further in this tutorial, we’ll see how downloaded images can be modified and generated into new images, which we can then upload to Docker Hub or other Docker registries.

Step 5 — Intro to Docker Containers

Earlier we played around with the hello-world container. We downloaded the image and used it to run the hello-world container which just displayed a message and exited after showing us the message. That was just a basic example. Docker containers can do more than that, and we can use them interactively, just like we would with a VPS, but much more resource friendly.

Let’s see an example of how a container can be similar to a VPS. Given the fact that we’re using Ubuntu, let’s go a little crazy and run a container using the latest image of CentOS. Don’t worry if you’ve never used CentOS – what we’re doing is similar to what we’d do on Ubuntu.

We’ll use the combination of the -i and -t switches to get interactive shell access to the container:

Docker will most likely not find the image on your machine and pull it from Docker Hub. After it’s downloaded and the container is run, your command prompt will change, reflecting the fact that you’re working inside the container:

The seemingly random sequence of letters and numbers, 35e04744a977, represents the container ID. We use that to identify the container and need it should we want to remove the container.

Now let’s run some basic commands, like update the package database inside the container. On CentOS, to do this, we use yum instead of apt. You also don’t need to use sudo, as you’re now acting as root in this container.

Now let’s install an application on it, such as PHP:

When the installation, if I check the version, I get the following output:

php -v
PHP 5.4.16 (cli) (built: Apr 12 2018 19:02:01)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Any changes you make in the container only apply to that container.

To exit the container just run exit.

List Docker Containers

After you’ve used Docker for a while, you’ll most likely be managing a few Docker containers, active and inactive.

To view your active containers run the following:

Since we’ve got our demo containers inactive, we’ll get the following output:

So far we’ve run two containers. One from the hello-world image, and the other from the centos image, but now they’re both stopped.

To view all of the containers, active and inactive, run the following:

This is the output I’m seeing:

To view the latest created container, run the following:

Output

Start/Stop Docker Containers

To start a stopped container use docker start, followed by the container’s ID (35e04744a977) or name (pensive_northcutt). In our example I’ll start by using its’ name:

Now let’s see if it started by listing our active containers:

Output

To stop the running container, you run docker stop followed by the container name or ID:

Useful Basic Docker Commands

Running commands inside an active

When you use docker start you’ll notice that it’s started, but you’re not interacting with it, such as when you first ran it. To run commands inside it, you can use docker exec -it, followed by the ID or name of the container, and followed by /bin/bash. You’ll notice your prompt change, letting you know that you’ve opened up a new terminal in it:

sudo docker exec -it pensive_northcutt /bin/bash
[root@35e04744a977 /]#

The -t switch opens a terminal, the -i switch makes it interactive, and the /bin/bash option opens a bash shell to the running container.

Exit a container without stopping it

You may find yourself needing to exit the container but wanting it to remain active. To do this, you can type Ctrl+P followed by Ctrl+Q. You’ll see something like the following output:

Renaming a container

To rename a container, you can use docker rename, followed by the current name or ID of the container, and then the desired name.

For example, to better organize my containers, I may want to rename my container named pensive_northcutt to a name similar to the naming convention that Docker seems to use, so I’ll call it mr_tiny_nipples.

If I run sudo docker ps (because my container is still active), I get the output:

Additionally, you can give a container a name of your choosing, when creating it, by using the --name switch.

Remove Docker Containers

When you no longer have use for a container you can remove it by using docker rm, followed by its’ name or ID:

Conclusion

In this tutorial, you’ve hopefully learned to install Docker and a little about using it and managing containers. We’ve just covered a few of the basics, however, and there’s a lot more you can do with it. There’s a reason some of the top companies in the world leverage Docker, and we’ll try to cover more in future tutorials.

To learn more about Docker, we recommend that you check out the official Docker documentation.

Should you encounter any issues when following this tutorial, or would like specific aspects covered, don’t hesitate to contact us and we’ll get back to you as soon as possible.

If you’re in need of a VPS to run Docker on, then feel free to check out our KVM SSD VPS plans. Our VPS offer starts at 2GB RAM + 10GB SSD at only $5/mo.