Docker: Installation and Basic Usage on Ubuntu 18.04 LTS
This tutorial exists for these OS versions
- Ubuntu 18.04 (Bionic Beaver)
- Ubuntu 16.04 (Xenial Xerus)
On this page
Docker is an open-source project that provides an open platform for developers and sysadmins to build, package, and run applications anywhere as a lightweight container. Docker automates the deployment of applications inside software containers. Docker development was started by Solomon Hykes as an internal project at dotCloud, an enterprise PaaS (platform as a service), but the software is now maintained by the docker community and Docker Inc. More about Docker can be found in the documentation here https://docs.docker.com/.
Docker requires a 64-bit architecture for installation and Linux Kernel must be 3.10 or newer. I will use Ubuntu Linux here.
Things to know about Docker
Here the basic terms in the docker world that you should know.
A Docker image is the basic template for a Docker container. An image usually contains the OS and applications that are readily installed. The Docker image is used to run the container, you can find many images with a variety of operating systems and software that has been installed in the Docker Hub https://hub.docker.com/. You can also create your own Docker images using a Dockerfile.
Docker Container is an image which can be read and written to that runs on top of the Docker image. Docker is using the union-file-system as backend for the container; any changes that are made in the container will be saved in a new layer above the base image. The container is the layer where we install applications. Each container runs isolated in the host machine and therefore, provides a secure application platform.
Docker registry is a repository for Docker images. It provides public and private repositories. The public Docker registry is called the Docker Hub. Here we can push and pull our own images.
Installing Docker on Ubuntu 18.04
In this section, you will be guided to install docker. Before you install the software, check the kernel version and the OS architecture. The next commands have to be run as root user. Run this command to become root on your Ubuntu system.
Then run uname -a to check the version of the currently running Linux kernel.
You can see that I`m using the kernel version 4.15.0 with a 64Bit Kernel (x86_64).
To check the Ubuntu version, run:
The command shows that the Ubuntu version is 18.04.
It is recommended to update Ubuntu before you install new software. Run the following command to fetch the latest updates from the Ubuntu repository and install them.
sudo apt-get update
sudo apt-get upgrade
Now we're ready to install Docker on to the server.
To install Docker, we can use the packages from the 'Ubuntu Repository' or using packages from the 'Docker Repository'.
Install Docker from Ubuntu Repository
If you want to install docker version from the Ubuntu repository, you can run the apt command below.
sudo apt install docker.io
Wait until the installation has been completed, then you can start Docker and add it to the boot time with the systemctl command:
systemctl start docker
systemctl enable docker
You might also want to check the docker version:
And you will get the docker 17.12 installed from the Ubuntu Repository.
Install Docker from the Docker Repository
Before installing the docker-ce from the Docker repository, install some dependencies using apt command as shown below.
sudo apt install \
When the installation is complete, add the docker key and docker 'nightly' repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic nightly" > /etc/apt/sources.list.d/docker-nightly.list
and update the repository.
sudo apt update
Now the docker repository has been added to the system.
Check all docker packages provided by the docker repository using apt-cache command.
sudo apt search docker-ce
sudo apt-cache policy docker-ce
And you will see different docker versions as below.
Install it using the apt command below.
sudo apt install docker-ce
When the installation is complete, start the docker service and enable it to launch every time at system boot.
systemctl start docker
systemctl enable docker
Now check the docker version installed on the system.
And you will get the docker 18.x installed on the system. Now you can start making a container by downloading a Docker Image from the Docker Registry.
Running Docker as a non-root User
In order to run docker as a normal/non-root user, we need to add a new system user. We will add new user named 'hakase', and then add it to the 'docker' group.
Add 'hakase' user.
useradd -m -s /bin/bash hakase
Add 'hakase' use to the 'docker' group.
usermod -aG docker hakase
Now login as a 'hakase' user and run the docker command.
su - hakase
docker run hello-world
And you will get the hello world from docker.
Basic Usage of Docker
In this section, I will show you commonly used options of the docker command. E.g. how to download a docker image, build a container and how to access the container.
To create a new container, you should start by choosing a base image with the OS, e.g. ubuntu or centos or another. You can search for a base image with the docker search command:
docker search ubuntu
This command will show you all ubuntu images. You can try by yourself docker search centos etc.
Now it's time to download the base image to our server, use the command:
docker pull ubuntu
The docker pull image-name command will download an image to your server from docker registry/DockerHub.
Now you can see all downloaded images by using the command:
The Ubuntu image was downloaded from DockerHub/Docker Registry. The next step is to create a container from that image.
To create the container, you can use docker create or docker run.
docker create ubuntu:18.04
docker create command will create a new container but not start it. So now you can use run command:
docker run -i -t ubuntu:18.04 /bin/bash
This command will create and run a container based on ubuntu 18.04 image and run a command /bin/bash inside the container, you will be automatically inside the container after running the command.
The container will stop when you leave it with the command exit. If you like to have a container that is running in the background, you just need to add the -d option in the command.
docker run -i -t -d ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
/bin/sh -c "while true; do echo hello world; sleep 1; done" this is bash script to echo "hello word" forever.
Now you can see the container running in the background by using the following command:
or if you want to see the logs result from that bash command you can use the command:
docker logs NAMES/ContainerID
How can I access the shell of container that runs in the background mode?
This command will connect you to the shell of the container:
docker exec -i -t NAMES/ContainerID /bin/bash
Enter a running Docker container.
You can see the hostname and the container ID are equal, this means that you are inside of the container shell. When you type `exit` on that shell you will leave that shell but the container is still running.
Another command that you will use often is:
docker stop NAME/ContainerID
This will stop the container without deleting it, so you can start it again with the command:
docker start NAME/ContainerID
If you like to remove the container, stop it first and then remove it with the command:
docker rm NAME/ContainerID
This is just a short introduction on the installation and basic usage of Docker on Ubuntu, you can find the detailed Docker documentation page here.
An in-depth introduction to Docker is available in this Howtoforge tutorial series: https://www.howtoforge.com/tutorial/how-to-use-docker-introduction/
Docker is an open source container virtualization platform which helps developers to deploy their applications and system administrators to manage applications in a safe virtual container environment. Docker runs on the Intel / AMD 64-bit architecture and kernel should be higher 3.10 version. With Docker, you can build and run your application inside a container and then move your containers to other machines running docker without any worries.