Containers enable organizations to expand beyond the standard server in ways that traditional technologies cannot....
With containers, you can bundle a piece of software within a complete file system that contains everything it needs to run: code, runtime, system tools, system libraries and so on. When you deploy an application or service this way, it will always run the same, regardless of its environment.
If you want to containerize a service or an app, you'll need to get up to speed with Docker, one of the most popular container tools. Here are some guidelines to install Docker on Ubuntu 16.04 servers and fulfill Docker's potential.
What to know before you install Docker on Ubuntu 16.04
Before you install Docker on Ubuntu 16.04, update the apt utility -- a package manager that includes the apt command -- and upgrade the server. If apt upgrades the kernel, you may need to reboot. If you need to reboot, do it when the server can be down for a brief period. It's important to note that you can only install Docker on 64-bit architecture, with a minimum kernel of 3.10.
To update and upgrade, enter the following commands:
sudo apt-get update
sudo apt-get upgrade
Once the update/upgrade is complete, you can install Docker with a single command:
sudo apt-get install -y docker.io
When the install completes, start the Docker engine with the command:
sudo systemctl start docker
Finally, enable Docker to run at boot with the command:
sudo systemctl enable docker
Running Docker as a standard user
Out of the box, you can only use Docker if you're the root user, or by way of sudo. Since either running Docker as the root user or with sudo can be considered a security risk, it's crucial to enable a standard user. To do that, you must add the user to the Docker group. Let's say we're going to add the user "Olivia" to the Docker group so that she can work with the tool. To do this, issue the following command:
sudo gpasswd -a olivia docker
Restart the Docker service with the command:
sudo systemctl restart docker
Once Olivia logs out and logs back in again, she can use Docker.
Before we get into the commands to work with Docker, you'll need to understand some of its terminology.
- Image: a frozen snapshot of live containers. Images are generally pulled from the Docker Hub, but you can create your own images. Images are read-only.
- Container: an active, stateful instance of an image that is read-write.
- Registry: a repository for Docker Images.
In a nutshell, you pull images from a registry and run containers from those images.
Let's say you want to run a Debian Linux container so you can test or develop a piece of software. To pull down the Debian image, you should search the registry first. Issue the command docker search debian. The results of that search (Figure A) are important.
The first two listings are marked as "official." To be safe, always pull official images. Pull down that debian image with the command:
When the image pull is complete, Docker will report the image debian:latest has been downloaded. To make sure it's there, use the command:
You are now ready to create the debian container with the command:
The above command will return a hash to indicate that you've created a container.
To run the container, issue the command:
docker run -i -t debian /bin/bash
The above command will run the debian container. It keeps STDIN (standard input) open with the -i option, allocates a pseudo-tty with the -t option, and places you in a Bash prompt so you can work. When you see the Bash prompt change, you'll know that the command succeeded.
You can work within your container and then exit the container with the command exit.
After you install Docker on Ubuntu 16.04, let's say you want to develop an image to be used later. When you exit a running container, you will lose all of your changes. If that happens, you cannot commit your changes to a new image. To commit those changes, you first need to run your container in the background (detached), by adding the -d option:
docker run -dit debian
When you run the container like this, you can't make any changes because you won't be within the container. To gain access to the container's shell, issue the command:
docker exec -i -t HASH /bin/bash
HASH is created after running the image in the background -- it will be a long string of characters.
Now, you should be inside the running container. Make your changes and then exit the running container with the command exit. If you re-enter the container, your changes should still be present.
To commit your changes to a new image, issue the command:
docker commit HASH NAME
HASH is the hash for our running container and NAME is the name you'll give the new image.
If you now issue the command docker images, your newly created image will be listed alongside the image you pulled from the Docker Hub registry.
Compare different Linux distros for servers
Learn how to perform a Docker backup
Other deployment container options outside of Docker