DEV Community

Cover image for Docker & Docker-compose on Ubuntu 20.04 Server
Semir Teskeredzic
Semir Teskeredzic

Posted on • Edited on

Docker & Docker-compose on Ubuntu 20.04 Server

Docker is a powerful application that lets you run your applications in a contained environment so everything that happens is (more or less) isolated through the use of Containers. It is however different from Virtual machines, because it uses OS Kernel from the host machine while Virtual machines spin up their own. You can watch how Nana explains it in this video: Docker vs Virtual Machine | simply explained || Docker Tutorial 6

Prerequisite

As a prerequisite for installing and using Docker & Docker-compose on Ubuntu server you will need it initially configured and set up. I've made a post about initial configuration for Ubuntu 20.04 server, so you can read it here: Configure new Ubuntu 20.04 server

1 - Install Docker

Before installing any new package, it is recommended to update the list of packages:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

(you will need to enter your user account password because you are using sudo command for elevated privileges)

Then, you need to install a couple of packages that let apt use packages over HTTPS:

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
Enter fullscreen mode Exit fullscreen mode

Next add GPG key for the Docker repository:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Enter fullscreen mode Exit fullscreen mode

Continue by adding the Docker repository to your APT sources:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Enter fullscreen mode Exit fullscreen mode

Repeat the update of the package database:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

Tell system that you will install Docker from Docker repository:

$ apt-cache policy docker-ce
Enter fullscreen mode Exit fullscreen mode

After seeing the output with the information about candidate installation, go on with installing Docker:

$ sudo apt install docker-ce
Enter fullscreen mode Exit fullscreen mode

Check that the Docker is running with this command:

$ sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

2 - Run Docker command without Sudo (Optional)

If you try to run docker command without sudo prefix or if the user is not in the docker group you will get the message that the docker can't connect to the Docker daemon. In order to fix this, you will need to add your username to the docker group (you will use this exact command if you want to add the user that you are currently logged in with, if you want to add another user, replace ${USER} with desired username):

$ sudo usermod -aG docker ${USER}
Enter fullscreen mode Exit fullscreen mode

To apply the new membership you should log out of the server and log back in.
If you want to check whether the user is in the docker group use:

$ id -nG
Enter fullscreen mode Exit fullscreen mode

3 - Docker images & Containers

Now that the Docker is installed, you can run docker command, the syntax is:

$ docker [option] [command] [arguments]
Enter fullscreen mode Exit fullscreen mode

Containers are built from Docker images, by default Docker pulls those images from the Docker Hub (i.e. a registry for the Docker images).
When you want to access an image you simply run this command:

$ docker run postgres
Enter fullscreen mode Exit fullscreen mode

This command will first try to find image locally, if it doesn't it will pull the image from the Docker hub and create a container from the pulled image.

You can search available images with browser on Docker Hub: https://hub.docker.com/search?q=&type=image, or you can use CLI command:

$ docker search postgres
Enter fullscreen mode Exit fullscreen mode

When you want to run a Docker container it is a good idea to have shell access to the container. You enable this kind of access with -i and -t switches:

$ docker run -it postgres
Enter fullscreen mode Exit fullscreen mode

Now you can work inside the container and for example run installation of packages for that container only.

If you want to see running Docker containers you can use this command:

$ docker ps
Enter fullscreen mode Exit fullscreen mode

Additionally use -a switch to see active and inactive containers

For troubleshooting purposes, you can see logs for the container by copying CONTAINER ID obtained after docker ps and running this command:

$ docker logs [CONTAINER_ID]
Enter fullscreen mode Exit fullscreen mode

4 - Docker-compose

Before installing Docker-compose we will have to check what is the latest version, you can do that on the Release page.

At the moment of writing this post, the latest release is 1.29.2 so we will use following commands to install this release (for different release enter a different release number in the following command):

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Set permissions:

$ sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

To verify the installation run:

$ docker-compose --version
Enter fullscreen mode Exit fullscreen mode

We use docker-compose with docker-compose.yml files, in there we can define multiple services with corresponding images and configurations. We execute Docker compose with the up command (if you want to run in detached mode add -d switch which will run it in the background):

$ docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

You have to execute this command inside the directory of your docker-compose.yml file. Similar to docker commands, running:

$ docker-compose ps
Enter fullscreen mode Exit fullscreen mode

will show you information about the running containers and their state together with port redirections.

This concludes basic installation and use of Docker and Docker-compose on your Ubuntu server. Thank you for reading!

Top comments (0)