How to install and use docker on ubuntu 18.04

Introduction

Docker is an open source project that helps people to run and manage containers. For developers, Docker helps in different stages of the development lifecycle.

During the development phase, Docker enables developers to spin up different infrastructure services such as databases and service discoveries like Consul without installation in the current system operational.

Sometimes, developers need to install the exact version. Docker helps developers at this stage. It is quite simple to run a container.

Installing Docker

First, update your existing list of packages:

sudo apt update

Next, The easiest way to install Docker is to download the script found at https://get.docker.com:

curl -fsSL get.docker.com -o get-docker.sh

After the download is completed, we will execute the script as follows:

sh get-docker.sh

Wait for the script execution and then check the Docker installation using the following command:

docker -v

Run Docker commands without sudo (Optional)

We will add the current user to the Docker group, and this enables us to use the Docker command line without the sudo keyword. Type the following command:

sudo usermod -aG docker $USER

We need to log out to effect these changes.

su - ${USER}

Confirm whether the command works as expected by typing the following. Make sure that the sudo keyword is not present:

docker ps

Docker concepts

Now, we will introduce some Docker concepts. Docker is a de facto tool that is used to manage containers.

Docker images

The Docker image is like a template for a Docker container. It contains a set of folders and files that are necessary to start the Docker container. We will never have an image in execution mode. The image provides a template for Docker Engine to start up the container. We can create an analogy with object orientation to understand the process better. The image is like a class that provides an infrastructure to instantiate some objects, and instances are like a container. Also, we have a Docker registry to store our images. These registries can be public or private. Some cloud vendors provide these private registries.

Containers

Docker containers are a lightweight virtualization. The term lightweight means that Docker uses the SO functionalities to cage the system process and manager memory, processors, and folders. This is different from virtualization with VMs because, in this mode, the technology needs to simulate the whole SO, drivers, and storage. This task consumes a lot of computational power and can sometimes be inefficient.

Docker networks

A Docker network is a layer that provides runtime isolation for containers. It is a kind of sandbox in which to run containers that are isolated from other containers. When the Docker is installed, by default it creates three networks that should not be removed. These three networks are as follows:

  • bridge
  • none
  • host

Also, Docker provides the user with an easy way to create your network. For this purpose, Docker offers two drivers : bridge and overlay.

Bridge : bridge can be used for the local environment, and it means this kind of network is allowed on a single host. It will be useful for our applications because it promotes isolation between containers regarding security. This is a good practice. The name of the container attached to this kind of network can be used as a DNS for the container. Internally, Docker will associate the container name with the container IP.

Overlay : The overlay network provides the ability to connect containers to different machines. This kind of network is used by Docker Swarm to manage the container in a clustered environment. In the newest version, the Docker Compose tool natively supports Docker Swarm.

Docker volumes

Docker volumes are the suggested way to persist data outside of a container. These volumes are fully managed by Docker Engine, and these volumes can be writable and readable depending on the configuration when they are used with a Docker command line. The data of these volumes is persisted on a directory path on a host machine.

There is a command-line tool to interact with volumes. The base of this tool is the docker volume command; the –help argument on the end shows the help instructions.

Docker commands

Now we will take a look at Docker commands. These commands are used mainly in the development life cycle, commands such as spin up container, stop containers, remove, and inspect.

Docker run

docker run is the most common Docker command. This command should be used to start the containers. The basic structure of a command is as follows:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

The options arguments: (options)

  • –name : argument permits you to configure a name for a container. It is important for DNS when the container is running in a bridge network.
  • –net : This enables us to configure the network to which the container will be attached.
  • -d : The -d parameter instructs Docker to run a container in the background.
Docker container

The docker container command permits you to manage the containers. There are many commands, as shown in the following list:

  • docker container attach
  • docker container commit
  • docker container cp
  • docker container create
  • docker container diff
  • docker container exec
  • docker container export
  • docker container inspect
  • docker container kill
  • docker container logs
  • docker container ls
  • docker container pause
  • docker container port
  • docker container prune
  • docker container rename
  • docker container restart
  • docker container rm
  • docker container run
  • docker container start
  • docker container stats
  • docker container stop
  • docker container top
  • docker container unpause
  • docker container update
  • docker container wait

There are some important commands here. The docker container exec permits you to run commands on a running container. This is an important task to debug or look inside the container files.

The docker container prune removes the stopped containers. It is helpful in the development cycle. There are some known commands, such as docker container rm, docker container start, docker container stop, and docker container restart.

Docker network

The docker network commands enable you to manage the Docker network stuff via the command line. There are six basic commands, and the commands are self-explanatory:

  • docker network create
  • docker network connect
  • docker network ls
  • docker network rm
  • docker network disconnect
  • docker network inspect

docker network create, docker network ls, and docker network rm are the main commands. It is possible to compare them with the Linux commands, where the rm command is used to remove things and the ls command is usually used to list things such as folders. The create command should be used to create networks.

The docker network connect and docker network disconnect commands allow you to connect the running container to the desired network. They may be useful in some scenarios.

Finally, the docker network inspect command provides detailed information on the requested network.

Docker volume

The docker volume command permits you to manage the Docker volumes via the command-line interface. There are five commands:

  • docker volume create
  • docker volume inspect
  • docker volume ls
  • docker volume prune
  • docker volume rm

The docker volume create, docker volume rm and docker volume ls commands are effectively used to manage the docker volume by Docker Engine. The behaviors are quite similar to those of the networks, but for volumes. The create command will create a new volume with some options allowed. The ls command lists all volumes and the rm command will remove the requested volume.

Add Comment