2016-07-11

Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system.

Prerequisites

To follow this tutorial, you will need the following:

64-bit Ubuntu 16.04 either as realVPS VM or on realCloud (or your own system)

Non-root user with sudo privileges

Step 1 — Installing Docker

The Docker installation package available in the official Ubuntu 16.04 repository may not be the latest version. To get the latest and greatest version, install Docker from the official Docker repository. This section shows you how to do just that.

But first, let’s update the package database:

sudo apt-get update

Now let’s install Docker. Add the GPG key for the official Docker repository to the system:

sudo apt-key adv –keyserver hkp://p80.pool.sks-keyservers.net:80 –recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Add the Docker repository to APT sources:

echo “deb https://apt.dockerproject.org/repo ubuntu-xenial main” | sudo tee /etc/apt/sources.list.d/docker.list

Update the package database with the Docker packages from the newly added repo:

sudo apt-get update

Make sure you are about to install from the Docker repo instead of the default Ubuntu 16.04 repo:

apt-cache policy docker-engine

You should see output similar to the follow:

Output of apt-cache policy docker-engine

Notice that docker-engine is not installed, but the candidate for installation is from the Docker repository for Ubuntu 16.04. The docker-engine version number might be different.

Finally, install Docker:

sudo apt-get install -y docker-engine

Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:

sudo systemctl status docker

The output should be similar to the following, showing that the service is active and running:

Output

Installing Docker now gives you not just the Docker service (daemon) but also the docker command line utility, or the Docker client. We’ll explore how to use the docker command later in this tutorial.

Step 2 — Executing the Docker Command Without Sudo (Optional)

By default, running the docker command requires root privileges — that is, you have to prefix the command with sudo. It can also be run by a user in the docker group, which is automatically created during the installation of Docker. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, you’ll get an output like this:

Output

If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

sudo usermod -aG docker $(whoami)

You will need to log out of the Droplet and back in as the same user to enable this change.

If you need to add a user to the docker group that you’re not logged in as, declare that username explicitly using:

sudo usermod -aG docker username

The rest of this article assumes you are running the docker command as a user in the docker user group. If you choose not to, please prepend the commands with sudo.

Step 3 — Using the Docker Command

With Docker installed and working, now’s the time to become familiar with the command line utility. Using docker consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:

docker [option] [command] [arguments]

To view all available subcommands, type:

docker

As of Docker 1.11.1, the complete list of available subcommands includes:

Output

To view the switches available to a specific command, type:

docker docker-subcommand –help

To view system-wide information about Docker, use:

docker info

Step 4 — Working with Docker Images

Docker containers are run from Docker images. By default, it pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anybody can build and host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need to run Docker containers have images that are hosted on Docker Hub.

To check whether you can access and download images from Docker Hub, type:

docker run hello-world

The output, which should include the following, should indicate that Docker in working correctly:

Output

You can search for images available on Docker Hub by using the docker command with the searchsubcommand. For example, to search for the Ubuntu image, type:

docker search ubuntu

The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:

Output

In the OFFICIAL column, OK indicates an image built and supported by the company behind the project. Once you’ve identified the image that you would like to use, you can download it to your computer using the pull subcommand, like so:

docker pull ubuntu

After an image has been downloaded, you may then run a container using the downloaded image with the run subcommand. If an image has not been downloaded when docker is executed with the runsubcommand, the Docker client will first download the image, then run a container using it:

docker run ubuntu

To see the images that have been downloaded to your computer, type:

docker images

The output should look similar to the following:

Output

As you’ll see later in this tutorial, images that you use to run containers can be modified and used to generate new images, which may then be uploaded (pushed is the technical term) to Docker Hub or other Docker registries.

Step 5 — Running a Docker Container

The hello-world container you ran in the previous is an example of a container that runs and exits, after emitting a test message. Containers, however, can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.

As an example, let’s run a container using the latest image of Ubuntu. The combination of the -i and -tswitches gives you interactive shell access into the container:

docker run -it ubuntu

Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form:

Output

Important: Note the container id in the command prompt. In the above example, it is 56gt563543r6

.

Now you may run any command inside the container. For example, let’s update the package database inside the container. No need to prefix any command with sudo, because you’re operating inside the container with root privileges:

apt-get update

Then install any application in it. Let’s install NodeJS, for example.

apt-get install -y nodejs

Step 6 — Committing Changes in a Container to a Docker Image

Docker filesystems are temporary by default. If you start up a Docker image, you can create, modify, and delete files just like a virtual machine. However, if you stop the container and start it up again, all your changes will be lost: any files you previously deleted will now be back, and any new files or edits you made won’t be present. This is because Docker images are more like templates than like images in the standard virtualization world.

To learn how to retain these changes within the container so that they are persistent through restarts of the container, you need to use Docker Data Volumes.

This section shows you how to save the state of a container as a new Docker image.

After installing nodejs inside the Ubuntu container, you now have a container running off an image, but the container is different from the image you used to create it.

To save the state of the container as a new image, first exit from it:

exit

Then commit the changes to a new Docker image instance using the following command. The -m switch is for the commit message that helps you and others know what changes you made, while -a is used to specify the author. The container ID is the one you noted earlier in the tutorial when you started the interactive docker session. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username:

docker commit -m “What did you do to the image” -a “Author Name” container-id repository/new_image_name

For example:

docker commit -m “added node.js” -a “Sunday Ogwu-Chinuwa” d9b100f2f636 finid/ubuntu-nodejs

Note: When you commit an image, the new image is saved locally, that is, on your computer. Later in this tutorial, you’ll learn how to push an image to a Docker registry like Docker Hub so that it may be assessed and used by you and others.

After that operation has completed, listing the Docker images now on your computer should show the new image, as well as the old one that it was derived from:

docker images

The output should be similar to this:

Output

In the above example, ubuntu-nodejs is the new image, which was derived from the existing ubuntu image from Docker Hub. The size difference reflects the changes that were made. And in this example, the change was that NodeJS was installed. So next time you need to run a container using Ubuntu with NodeJS pre-installed, you can just use the new image. Images may also be built from what’s called a Dockerfile. But that’s a very involved process that’s well outside the scope of this article.

Step 7 — Listing Docker Containers

After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:

docker ps

You will see output similar to the following:

Output

To view all containers — active and inactive, pass it the -a switch:

docker ps -a

To view the latest container you created, pass it the -l switch:

docker ps -l

Stopping a running or active container is as simple as typing:

docker stop container-id

The container-id can be found in the output from the docker ps command.

Step 8 — Pushing Docker Images to a Docker Repository

The next logical step after creating a new image from an existing image is to share it with a select few of your friends, the whole world on Docker Hub, or other Docker registry that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.

This section shows you how to push a Docker image to Docker Hub.

To create an account on Docker Hub, register at Docker Hub. Afterwards, to push your image, first log into Docker Hub. You’ll be prompted to authenticate:

docker login -u docker-registry-username

If you specified the correct password, authentication should succeed. Then you may push your own image using:

docker push docker-registry-username/docker-image-name

It will take sometime to complete, and when completed, the output will similar to the following:

Output

After pushing an image to a registry, it should be listed on your account’s dashboard.

If a push attempt results in an error of this sort, then you likely did not log in:

Output

Log in, then repeat the push attempt.

Conclusion

There’s a whole lot more to Docker than has been given in this article, but this should be enough to getting you started working with it on Ubuntu 16.04. Like most open source projects, Docker is built from a fast-developing codebase, so make a habit of visiting the project’s blog page for the latest information.

The post How To Install and Use Docker on Ubuntu 16.04 appeared first on Bamboozle.

Show more