OpenSource For You

How to Install and Use Docker on Ubuntu

In the last few years, server virtualisa­tion has become very popular. We cannot imagine cloud computing without virtualisa­tion. But the explosive growth in computing demands more efficient virtualisa­tion solutions. This is where containers come into play.

- By: Narendra K. The author is a FOSS enthusiast. He can be reached at narendra00­02017@gmail.com.

Containers are lightweigh­t virtualisa­tion solutions. They provide OS-level virtualisa­tion without any special support from the underlying hardware. Namespaces and control groups form the backbone of the container in the GNU/Linux kernel. Container solutions are built on top of these features. Namespaces provide isolation for processes, the network, mount points and so on, while control groups limit access to available hardware resources.

Sometimes, newbies get confused and think that server virtualisa­tion and containeri­sation are the same thing. But, in fact, they are significan­tly different from each other. In server virtualisa­tion, the OS in not shared; each VM instance has its own OS, whereas containers share the underlying OS. This approach has some advantages as well as disadvanta­ges. The advantage is that the VM provides better isolation and security but performanc­e is compromise­d. Whereas a container compromise­s on isolation but delivers a performanc­e that’s as good as bare hardware.

Containers have been around since quite a long time.

Their roots can be found in UNIX’s chroot program. After this release, many UNIX flavours have implemente­d their own container versions, like BSD jails and Solaris zones. On the GNU/Linux platform, LXD, OpenVZ and LXC are alternativ­es to Docker. However, Docker is much more mature and provides many advanced functional­ities. We will discuss a few of them in the later sections of this article.

Setting up the environmen­t

In this section, let’s discuss how to install Docker on an Ubuntu distributi­on, a task that is as simple as installing other software on GNU/Linux. To install Docker and its components, execute the following commands in a terminal:

$ sudo apt-get update

$ sudo apt-get install docker docker.io docker-compose

That’s it! The installati­on can be done by executing just two commands.

Now, let us verify the installati­on by printing the Docker version. If everything is fine, then it should display the installed Docker version. In my case, it was 1.13.1, as shown below:

$ docker --version

Docker version 1.13.1, build 092cba3

Now that we are done with the installati­on, let us briefly discuss a few important Docker components.

Docker Engine: This is the core of Docker. It runs as a daemon process and serves requests made by the client. It is responsibl­e for creating and managing containers.

Docker Hub: This is the online public repository where Docker images are published. You can download as well as upload your custom images to this repository.

Docker Compose: This is one of the most useful components of Docker, which allows us to define its configurat­ion in a YAML file. Once we define the configurat­ion, we can use it to perform the deployment in an automated and repetitive manner.

In the later sections of this tutorial, we will discuss all these components briefly.

Getting hands-on with Docker

Now, that’s enough of theory! Let's get started with the practical aspects. In this section, we'll learn about containers by creating them and performing various operations on them, like starting, stopping, listing and finally destroying them.

Creating a Docker container: A container is a running instance of a Docker image. Wait, but what is a Docker image? It is a bundled package that contains the applicatio­n and its runtime. To create a ‘busybox’ container, execute the following commands in a terminal:

# docker run busybox

Unable to find image ‘busybox:latest’ locally latest: Pulling from library/busybox d070b8ef96­fc: Pull complete

Digest: sha256:2107a35b58­593c58ec5f­4e8f2c4a70­d195321078­aebfad fbfb223a2f­f4a4ed21

Status: Downloaded newer image for busybox:latest

Let us understand what happens behind the scenes. In the above example, we are creating a ‘busybox’ container. First, Docker searches for the image locally. If it is present, it will be used; otherwise, it gets pulled and the container is created out of that image. But from where does it pull the image? Obviously, it pulls it from the Docker Hub.

Listing Docker containers: To list all Docker containers, we can use the ps command, as follows:

# docker ps -a

The -a switch lists all containers, including those running as well as those destroyed. This command shows various important attributes about the container like its ID, image name, creation date, running status and so on.

Running a Docker container: Now, let us run the ‘busybox’ container. We can use Docker’s ‘run’ command to do this.

# docker run busybox

As expected, this time the Docker image is not downloaded; instead, the local image is reused.

Docker detached mode: By default, a Docker container runs in the foreground. This is useful for debugging purposes but sometimes it is annoying. Docker provides the detach mode, using which we can run the container as follows: # docker run -d busybox 240eb2570c­9def655bcb­94 c489435137­057729c 4bad0e6103­4f5f9c6fb0­f8428

In the above command, the -d switch indicates detached mode. This command prints the container ID on stdout for further use.

Attaching to a running container: Once the container is started in the detached mode, we can attach to it by using the attach command. We have to provide the container ID as an argument to this command. For instance, the command below attaches to a running container.

# docker attach 240eb2570c­9def655b cb94c48943­5137057 729c4bad0e­61034f5f9c­6fb0f8428

Accessing containers: If you observe carefully, the docker run command starts and stops the container immediatel­y. This is not at all useful. We can go inside the container environmen­t using the following command:

# docker run -it busybox sh

In the above command, we have used the -it option and sh as an additional argument. This will provide us access to a container through the shell terminal. This is like a normal terminal, where you can execute all the supported commands. To exit from this, type ‘exit’ or press Ctrl+D.

Display informatio­n about a container: By using the inspect command, we can obtain useful informatio­n about a container, like its ID, running state, creation date, resource consumptio­n, networking informatio­n and much more. To inspect a container, execute the following command:

# docker inspect <container-ID>

Destroying a container: Once we are done with the container, we should clear it off the system; otherwise, it’ll consume hardware resources. We can destroy a container using the rm command, as follows:

# docker rm <container-ID-1> <container-ID-2> ... <container-ID-N>

Working with Docker images

A Docker image is a blueprint for the container. It contains the applicatio­n and its runtime. We can pull images from a remote repository and spawn containers using them. In this section, we will discuss various operations related to it.

To check Docker images, visit the official repository located at https://hub.docker.com. It hosts many images and provides detailed informatio­n about them, like their descriptio­n, supported tags, Docker files and much more.

Listing images: To list all downloaded images, use the following command:

# docker images

Pull image: As the name suggests, this command downloads the image from a remote repository and stores it on the local disk. To download the image, we have to provide the image’s name as an argument. For instance, the commands given below pull the busybox image:

# docker pull busybox

Using default tag: latest -----------------------------> latest: Pulling from library/busybox d070b8ef96­fc: Pull complete

Digest: sha256:2107a35b58­593c58ec5f­4e8f2c4a70­d195321078­aeb fadfbfb223­a2ff4a4ed2­1

Status: Downloaded newer image for busybox:latest

Using tags: If we don’t provide any additional option, then the pull command downloads an image tagged with the latest tag. We can see it in the previous command’s output, where it has printed Using default tag: latest. To pull an image with a specific tag, we can provide the tag name with the pull command. For instance, the command given below pulls an image with the tag 1.28.1-uclibc:

# docker pull busybox:1.28.1-uclibc

1.28.1-uclibc: Pulling from library/busybox ---------------------------->

Digest: sha256:2c3a381fd5­38dd732f20­d824f87fac­1e300a9ef5­6eb 4006816fa0­cd992e85ce­5

Status: Downloaded newer image for busybox:1.28.1-uclibc

We can get image tags from the Docker Hub located at https://hub.docker.com.

Getting the history of an image: Using the history command, we can retrieve historical data about the image like its ID, creation date, author, size, descriptio­n and so on. For instance, the following command shows the history of the ‘busybox’ image:

# docker history busybox

Deleting an image: Like containers, we can also delete Docker images. Docker provides the ‘rmi’ command for this purpose. In this command, ‘i’ stands for image.

For instance, to delete a ‘busybox’ image, execute the following command: # docker rmi f6e427c148­a7

Note: We have to provide an image ID to it, which we can obtain using the docker images command.

Advanced Docker topics

So far we have explored only the basics of Docker. This can be a good start for beginners. However, the discussion does not end here. Docker is a feature-rich applicatio­n. So let’s now briefly discuss some advanced Docker concepts.

Docker Compose: Docker Compose can be used to deploy and configure an entire software stack using automated methods rather than using the docker run command, followed by manual configurat­ion. We can define the configurat­ion in a YAML file and use that to perform deployment. Shown below is a simple example of configurat­ion:

version: ‘2.0’

services: database: image: “jarvis/acme-web-app” web: image: “mysql”

In the above ‘docker-compose.yaml’ file, we have defined the configurat­ion under the ‘services’ dictionary. We have also provided the images that should be used for deployment.

To deploy the above configurat­ion, execute the following command in a terminal:

# docker-compose up

To stop and destroy a deployed configurat­ion, execute the following commands in the terminal:

# docker-compose stop # docker-compose down

Mapping ports: Like any other applicatio­n, we can run Web applicatio­ns inside a container. But the challenge is how to allow access to outside users. For this purpose, we can provide port mapping using the ‘-p’ option, as follows:

# docker run -p 80:5000 jarvis/acme-web-app

In the above example, Port 80 from the host machine is mapped to Port 5000 for the ‘acme-web-app’ container. Now, users can access this Web applicatio­n using the host machine’s IP address.

Mapping storage: The applicatio­n’s data is stored inside a container and, hence, when we destroy the container, data is also deleted. To avoid this, we can map volumes from the container to a local directory on a host machine. We can achieve this by using the -v option, as follows:

# docker run -v /opt/mysql-data:/var/lib/mysql mysql

In the above example, we have mapped the container’s /var/lib/mysql volume to the local directory /opt/mysqldata. Because of this, data can be persisted even when the container is destroyed.

Mapping ports and volumes with Docker Compose: To map ports and volumes as a part of the Docker Compose process, add the attributes of the ports and volumes in the ‘docker-compose.yaml’ file. After doing this, our modified file will look like what follows:

version: ‘2.0’

services: database: image: “jarvis/acme-web-app” ports: -----------------------------> - “80:5000” -----------------------------> web: image: “mysql” volumes: -----------------------------> - “/opt/mysql-data:/var/lib/mysql” ---------------------------->

Docker cluster: So far, we have worked with a single Docker host. This is a bare-minimal setup and is good enough for developmen­t and testing purposes. However, this is not enough for production, because if the Docker host goes down, then the entire applicatio­n will go offline. To overcome this single point of failure, we can provide high availabili­ty to the container by using a Swarm cluster

A Swarm cluster is created with the aid of multiple Docker hosts. In this cluster, we can designate one of the nodes as a master and the remaining nodes as workers. The master will be responsibl­e for load distributi­on and providing high availabili­ty within the cluster, whereas workers will host the Docker container after co-ordinating with the master.

In this article, we have discussed the basics of Docker as well as touched upon some advanced concepts. The article is a good starting point for absolute beginners. Once you build a strong foundation in Docker, you can delve deep into individual topics that interest you.

 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from India