PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Docker & Docker-compose on Ubuntu 20.04 Server

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

Prerequisites

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

(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

Next add GPG key for the Docker repository:

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

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"

Repeat the update of the package database:

$ sudo apt update

Tell system that you will install Docker from Docker repository:

$ apt-cache policy docker-ce

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

$ sudo apt install docker-ce

Check that the Docker is running with this command:

$ sudo systemctl status docker

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}

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

3 - Docker images & Containers

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

$ docker [option] [command] [arguments]

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

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

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

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

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]

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

Set permissions:

$ sudo chmod +x /usr/local/bin/docker-compose

To verify the installation run:

$ docker-compose --version

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

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

$ docker-compose ps

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!

Comments (0)

loading comments