Docker is an open-source containerization tool that allows you to easily build, test, and deploy applications. Docker helps to package an application with all parts (code, logs, libraries, configuration settings, and other dependencies) and allows them to run as isolated containers. Containers are lighter weight, portable.self-sufficient, and easier to manage.
Docker software is maintained by the Docker community and Docker Inc.To learn more about Docker concepts and docs checkout official documentation.
In this tutorial, we’ll learn how to install Docker Community Edition (CE) on on an Ubuntu 18.04 Aws ec2 instance.You can apply this same on local ubuntu or server also.
Before We Begin
- Ubuntu 18.04 system with root or user with sudo privileges
There are two ways to install Docker on ubuntu. One way is we can install docker from official Ubuntu repositories using apt, but the version may not be the latest. The recommended way is to install the latest Docker Package from the official Docker repository.
To do that, we have to add a new package source, add the GPG key from Docker to ensure that the downloads are valid, and then we will install the package.
At first update the ubuntu existing packages:
sudo apt update
Next, we will install a few prerequisite dependencies to add new packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Then we have to add the GPG key for the official Docker repository to our system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to our system APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
Next, update the package database with the Docker packages from our newly added repo:
sudo apt update
We have to make sure installing from the Docker repo instead of the default Ubuntu repo.So check by running:
apt-cache policy docker-ce
You’ll see output like this, although the version number for Docker may be different:
500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
docker-ce is not installed.But installation candidate from Docker repository for Ubuntu bionic.
If we need to install a specific version, first list the available versions in the Docker repository:
apt list -a docker-ce
The command prints the available Docker versions in the second column.
For example, to install version
18.09.6 you would type:
sudo apt install docker-ce=5:18.09.8~3-0~ubuntu-bionic
We will install the latest version 5:19.03.12~3-0 shown from the above command not specifying specific version:
sudo apt install docker-ce
Now Docker is installed in our server. After installation automatically daemon started, and the process enabled us to start on boot.
Check Docker Status
We can check the docker status :
sudo systemctl status docker
The output should be similar to the following, showing that the service is active and running:
● docker.service – Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-07-18 07:45:17 UTC; 8min ago
Main PID: 3525 (dockerd)
└─3525 /usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock
To check Docker version Installed :
docker -v Docker version 19.03.12, build 48a66213fe
We can stop docker service as :
sudo systemctl stop docker
To start/restart the service:
sudo systemctl start docker
sudo systemctl restart docker
Executing the Docker Command Without Sudo
By default, after docker installation,the
dockercommand can only be run the root user or by a user in the docker group, which is automatically created during Docker’s package installtion. If you try to run the
dockercommand without prefixing it with
sudoor without being in the docker group, you’ll get an output like this:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/images/json: dial unix /var/run/docker.sock: connect: permission denied
To fix this ,add the user to docker group :
sudo usermod -aG docker $USER
Logout from the system and log back again to reflect the changes we made.
Now check again docker command without using sudo
ubuntu@ip-172-31-63-145:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
To test a docker container run:
docker container run hello-world
The command will download a test image, run it in a container, print a “Hello from Docker” message and exit. The output should look like the following:
On upcoming posts we will learn more about Docker images and Docker containers working with best practices
Use Docker Commands
The Docker CLI command takes this form:
docker [option] [subcommand] [arguments]
To view all available subcommands, just type docker without no parameters as below :
It will list all available subcommands .In ubuntu 18.04 we can see output as below
To view the options available to a specific command, type:
docker docker-subcommand --help
For example If we run
docker ps --help
It will list options showing how to use that particular sub-command “ps”.
In this tutorial, we learned to install Docker on ubuntu 18.04 and some basic commands to manage docker. It is just the starting. In upcoming Docker tutorials, we will learn about Docker images, Docker containers, Docker Compose, and Docker Swarm. To learn more about Docker check out the official Docker documentation
If you have any questions, please leave a comment below.