Docker Ubuntu

How to install & operate Docker on Ubuntu in the public cloud

Docker is one of the most popular products in organizations these days. It makes the process of managing applications in containers very easy. Docker provides portability, performance, agility, scalability and isolation to the applications, since it uses containers, which are more portable and require less resources than virtual machines.

In this tutorial, you will go through the steps required to set up and use Docker on an Ubuntu server. You will start by installing and configuring Docker on a Cloudsigma server. You will then work with Docker images and containers. Finally, you will also push an image to the Docker repository.

Step 1: Setting up the Instance on CloudSigma

I am using a machine with the following resources:

  • 8 GHz CPU
  • 8 GB RAM
  • 50 GB SSD

Go ahead and clone Ubuntu 18.04 from the CloudSigma library and then resize it to 50 GB. Ubuntu 18.04 on the library comes pre-installed with 64bit with VirtIO drivers, superuser, Python 2.7.17, Pip 20.0.2 OpenSSL 1.1.1d, Cloud-init and latest updates until 2019-03-03. For more information on how to set up Ubuntu 18.04, please check out this tutorial.

Now that your server is ready, let’s move towards installing Docker application.

Step 2: Installing Docker on Ubuntu

There are various ways to install Docker on Ubuntu, but the most preferable one is installing Docker’s latest version from its official repository. In the current tutorial, we are going to use this method.

Before you start installing Docker, you need to set up its repository. After that, you can install Docker from the repository.

  • First, you need to update the apt package index and install a few packages. These packages will allow apt to use a repository over HTTPS:
  • Then, you need to add GPG key of the official Docker repository to your system:
  • You can verify the fingerprint by using the following command:
    The output would contain “9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88”
  • Next, you go ahead and add the official repository using this command:
  • Finally, you can install the Docker engine. The following commands first update the apt package index and then install the latest version of Docker engine and contained:
     

Docker is now installed and it’s daemon process has started. You can check the running status of Docker using the command:

The output should look like this:
RDS licenses

Step 3: Providing Docker Permissions to User

Currently, the Docker command can only be run by a root user or a user in the ‘docker’ group. The Docker group is automatically created during the Docker installation procedure. If you try to run the Docker command without sudo or from a user who hasn’t been added to the group, you will get an error like this:
Docker Ubuntu

You can add your user – “cloudsigma” to the Docker group using the following command:

Once you run the above command, re-login to the user and try to see the user groups using the following command. Re-login is required for the command to come in effect.

You can see that Docker is now a part of the user groups and Docker command is functional for the user:
RDS licenses

Step 4: Using the Docker Command

You can find the syntax of Docker command by just executing it:

This will show you the usage and possible options and arguments you can use. For example:
RDS licenses

You can see the available options for a specific sub-command using:

You can also see system-wide information about Docker using this command:

Now let’s dive deeper into the world of Docker by working with Docker images and containers.

Step 5: Working with Docker Images in Ubuntu

Docker containers are built using Docker images. Docker Hub is an official Docker registry. Anyone can register and host their Docker images there.

To test the access to Docker hub, try this command:

Docker Ubuntu

It mentions that Docker was unable to find the image locally, so it pulled the image from Docker hub. Docker daemon created a new container from that image that runs the executable and prints this output shown in the image. The daemon then streamed the output to Docker client, which sent it to our terminal.

You can search for more images using the ‘docker search’ command. For example, you can search for Ubuntu operating system image by:

It will search Docker hub for all images that contain ‘ubuntu’ and return the list to us. You can try pulling Ubuntu image using this command:

The output should look like this:

Now that the image has been downloaded, you can run a container with this image using the ‘docker run’ command. If you haven’t downloaded the image, the ‘docker run’ command will first download it and then run it as it did in case of our ‘hello-world’ example.

You can see the Docker images stored on your server by using the command:

Currently we have two images – ‘ubuntu’ and ‘hello-world’. Now let’s see how you can run containers using these images.

Step 6: Running a Container

When we ran the hello-world container, it ran, executed some commands to display a message and exited. However, containers can also be used interactively as they are similar to virtual machines.

You can run the ubuntu container with “-it” which gives you an interactive shell access to the container.

The command prompt will change to look a little different based on the container you are in.

“743177bd9596” in the above shell is the container ID of the container which can be used to remove the container, if required.

Now that you are in the container, you can run various commands here just like you would do on a server. Let’s for example try to update your package database using this command:

You can also install any application that you want to run. Let’s take Nginx, for instance. You can use the following command to do that:

You can run the Nginx service and check its status by means of these command lines:

Any changes that you make to the container are only applied inside that container. Those won’t be saved to the images. In the following steps, you will learn how to save those changes to create new images. Please type exit to exit the container.

Step 7: Managing Containers

In this part, you will learn to manage containers in Docker.

Once you start using Docker actively, you would generally have lots of containers and images on your system. You can view active ones using this command:

But we did run two containers, “ubuntu” and “hello-world”. Where are they?
Since they are no longer running, you can’t see them here. However, you can get a list of all the containers – inactive and active – using the following command:

Also, you can see the latest container created using ‘-l’

You can start a stopped container using the command ‘docker start’ followed by container ID or container’s name. Let’s try running the Ubuntu container:

To stop a container, use the command ‘docker stop’ followed by the container ID or container’s name.

You can remove a container once your work is complete using the command ‘docker remove’.

There are various other features and attributes of Docker container management which you can check in the documentation. In order to access the documentation, run the ‘man docker’ command:

Step 8: Committing Changes to a Docker Image

Just like a virtual machine, you can make changes to the container such as removing a file, installing an application and configuring it for a use case. But the changes are only saved in that particular container. You can stop and start the container, but once you remove the container, the changes will be lost.

However, you can save the state of the container by creating a new Docker image out of it. Earlier, we installed an Nginx application on top of the Ubuntu container. You could save that image so that you don’t have to repeat the steps again. After making the changes, you can run a command of this format:

Description:
-m : Used to mention the change we made for reference so we and others can understand the change.
-a : Used to mention the name of the author.
container_id : Container ID of the container in which we made changes
repository: Generally your Docker hub username
imageName: Name of the new image

Example:

When you ‘commit’ it, the new image is saved locally. In the next step, you will learn how to save it on docker-hub.

Here is how you can list the Docker images and see the saved Docker image:

As you can see, the new image with the name “ubuntu-nginx” is now in your system locally. The size difference between both is significant and this is due to the Nginx installation on top of the base image.

Step 9 : Pushing Docker Images to Docker Repository in Ubuntu

Now that you have created an image, let’s push it to the Docker repository so that it’s available to others. To do that, you should first have an account on Docker hub.

To push an image, you first need to login with the username:

If the Docker hub’s username is different from the local username you used to create the image, you will have to tag the image with the Docker hub username. To do this, you can use this command:

After that, you can push the image to the Docker hub repository using the command:

Example:

After pushing the image to the repository, it will be listed under the account:
Docker Ubuntu

Now, you can pull the image from any server which has access to Docker hub.

Now that you have the basics, you can start using Docker for trying new applications, doing experiments and deploying various services on the cloud.