Docker is an open source application similar to a virtual machine. It is used to simplify the process of automation of applications deployment with its runtime dependencies within Linux containers. Containers, which work independently from the host operating system, enable users to perform quick application deployment, troubleshooting and maintenance with improved security.
A Docker container is simply an instance of an application where all the components and libraries necessary for an application to run, are stored. They are resource friendly, portable and self sufficient that can run virtually anywhere.
Docker offers a Docker CLI command line utility for the lifecycle management and handling of containers which are image based.
Highly popular among developers, Docker is relatively new but receives continuous updates and improvements. It can be installed very easily without any issues in your favorite Linux distribution.
In this article, we will install Docker on Ubuntu 18.04. We will also work with Docker CLI and how to use images.
Installing Docker package from the default Ubuntu repository may not bing the most recent version. In order to acquire the latest version, we would need to install it from the official Docker repository. To achieve that, we first have to add a new package source before adding the GPG key which will guarantee the validity of downloads. Once this is done, we shall proceed to the installation of the package.
As usual and before installing any new application, it is best to update your package list index by running the command:
1 sudo apt update
2 Next, to enable https package transport, run the command below:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
3 Now you need to import the Docker repository GPG key to your system by executing the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
4 Add the Docker repository (using add-apt-repository) to APT sources ( either /etc/apt/sources.list or to a different file in the directory /etc/apt/sources.list.d ) by running :
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable”
You should now be able to install any Docker version you require since the Docker repository is now enabled.
Run the command below in order to install the most recent version of Docker.
sudo apt install docker-ce
Once the command above is finished, Docker should be installed, the Docker service (daemon) started and the process enabled to launch on boot. In order to check that it is running, run the command:
sudo systemctl status docker
The installation of Docker provides you with the docker command line tool or the Docker client.
To use docker, you would just need to pass some options and commands along with arguments. The syntax is as follows:
docker (option) (command) [arguments]
To list out all subcommands, run:
You can use the option –help to get help on specific subcommand as shown below:
docker [subcommand] –help
To display system-wide information related to Docker, use the command:
In Docker, everything is built up or based on Images. An image consists of a file system (libraries, binaries..) and parameters (like for instance the instructions necessary to run the application). Containers are actually instances of Docker images that can be executed using the Docker built-in run command. Running containers is the basic purpose of Docker.
Let’s look for example at the Docker command below which checks if you can remotely access and download images from the Docker Hub:
sudo docker run hello-world
The run command above tells Docker that an instance of an image would have to be created, in other words, a container.
Here is an example output of the command above :
From the snapshot above, Docker could not find the hello-world image on the local machine, so it proceeded to download it from the Docker Hub, .i.e. is the default repository.
Once the image retrieved, a container was created from the image and the application that lies within the container was executed displaying therefore the message. In a nutshell, ‘hello-world’ is actually the image from which the resulting container is created.
Docker containers are constructed from Docker images which are pulled by default from Docker Hub ( a Docker cloud based registry managed by Docker ). Every user can host his own Docker images on the Docker Hub. Most Linux distributions and applications you will need, will actually have images hosted on the Docker Hub.
As an example, to run CentOS by using the CentOS image available in the Docker Hub can be achieved by running the following command :
sudo docker run -it centos /bin/bash
This shows that that Docker will download the image with the name centos from the Docker Hub and then install it locally in order to run it in an interactive mode.
Once CentOS is running, /bin/bash is used to execute the bash shell.
Read: How to speed up Linux
To display the list of Docker images currently installed on the system, you can run the command :
An example output is shown below :
This will show images with the following available attributes :
TAG − used for logical image tagging
Image ID − Uniquely identifies the image
Created − The image age in days
Virtual Size − The image size
To search for instance for the Ubuntu image, type in:
docker search ubuntu
The script will access and crawl the Docker Hub and will return all images with the name that matches the search string. Here is an example output :
In the third column named OFFICIAL, [OK] shows that an image is built and supported by the software company responsible for the project. The pull sub-command will help download the image you have identified on your computer.
In order to download the official ubuntu image, execute the following command:
docker pull ubuntu
This will show an output similar to :
You can afterwards run a container using the image that you downloaded with the run command.
As was alluded at above, the Docker run command is used to download images from the Docker Hub.
docker run image
Where image will be used to run the container. The output will execute the command in the chosen container.
Here is an example :
sudo docker run centos
If the centos image is not already available locally, it will be downloaded as was shown previously. The command will run centos as a container.
If you now run :
sudo docker images
You should be able to find the centos image as well.
Images used to run containers can be altered and used to generate yet new images. These new images may be uploaded to the Docker Hub or other Docker based registries.
To delete a Docker image, simply use the m switch in the command below :
docker image rm [image_name]
For example : docker image rm centos
Docker provides an API for container handling as well as an image format with the ability to remotely access a registry for container sharing.
Both system administrators and developers will see the following benefits from using this scheme :
- Sharing – a remote repository can be used to share a container with other users.
- Version control – multiple versions of a container can be tracked to compare differences, or switch back to previous versions.
- Portability – an application along with its dependencies can be packed into a single host-independent container which can be transferred to a different machine that runs Docker and invoked without any compatibility issues. The container is also independent of the platform distribution or the deployment model.
- Rapid application deployment – containers allow applications to be deployed quickly. Since Docker images are small, it makes rapid delivery easier and deployment time of new application containers reduced .
You have learned how to install Docker on your Ubuntu 18.04 machine, how to use Docker CLI and how to work with Docker images. We have just scratched the surface of Docker. Stay tuned for more articles on Docker.