docker.md 5.9 KB

Docker

Information on how to install and use Docker.

Docker is an open-source project that automates the deployment of applications inside software containers.

Prerequisites

Ensure you have the following:

  • a (fresh) install of Ubuntu LTS with SSH enabled and VSCode installed including a sudo user
  • SSH: sudo apt install openssh-server
  • VSCode: download .deb package from this URL
  • OR a Windows machine including a Administrator user with VSCode installed
  • VSCode: download Windows installer package from this URL

Install Docker on Ubuntu

Step 1 - Installing Docker

Add the docker-ce repo:

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt update

Install docker:

sudo apt install docker-ce

Step 2 - Executing the Docker Command Without Sudo

sudo usermod -aG docker ${USER}

Reboot your machine:

sudo reboot

Step 3 - Installing docker-compose

Docker Compose is a tool that allows you to run multi-container application environments based on definitions set in a YAML file. It uses service definitions to build fully customizable environments with multiple containers that can share networks and data volumes.

mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

chmod +x ~/.docker/cli-plugins/docker-compose

To verify that the installation was successful, you can run:

docker compose version

You'll see output similar to this:

Docker Compose version v2.3.3

Setting Up a docker-compose.yml file

To demonstrate how to set up a docker-compose.yml file and work with Docker Compose, you'll create a postgres environment using the official postgres image from Docker Hub, the public Docker registry. This containerized environment will serve a postgres service.

Start off by creating a new directory in your home folder, and then moving into it:

mkdir ~/docker-repo
cd ~/docker-repo

In this directory, set up an application folder to serve as the document root for your postgres environment and an running folder to serve as persistent "rundir" root:

mkdir postgres
mkdir runnning

Next, create the docker-compose.yml file:

vi docker-compose.yml

Insert the following content in your docker-compose.yml file:

version: '3.7'

services:
  postgres:
    image: postgres:11
    container_name: postgres
    network_mode: bridge
    restart: always
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=my_password
    volumes:
      - /home/$USER/docker-repo/running/postgres/data:/var/lib/postgresql/data

Save and close the file.

Running docker-compose

With the docker-compose.yml file in place, you can now execute Docker Compose to bring your environment up. The following command will download the necessary Docker images, create a container for the postgres service, and run the containerized environment in background mode:

docker-compose -f docker-compose.yml up -d

Docker Compose will first look for the defined image on your local system, and if it can't locate the image it will download the image from Docker Hub.

Your environment is now up and running in the background. To verify that the container is active, you can run:

docker ps

Usefull commands

Go inside the container:

docker exec -it postgres /bin/bash -l

Stop, start or restart the container:

docker stop postgres
docker start postgres
docker restart postgres

Tips for Daniele

docker exec -it postgres /bin/bash -l
apt update
apt install postgis
exit

Tips for Daniele (Configure a database and dump the content from dbexport.pgsql)

psql -h localhost -p 5432 -U postgres

CREATE ROLE livestock WITH SUPERUSER CREATEDB CREATEROLE LOGIN ENCRYPTED PASSWORD 'happycattle';
CREATE DATABASE livestock_db;
GRANT ALL PRIVILEGES ON DATABASE livestock_db TO livestock;
\q

psql -h localhost -p 5432 -U livestock livestock_db

CREATE SCHEMA postgis;
CREATE EXTENSION postgis SCHEMA public;
\q

psql -h localhost -p 5432 -U livestock livestock_db < dbexport.pgsql

psql -h localhost -p 5432 -U livestock livestock_db

SELECT * FROM livpop ORDER BY livpop DESC LIMIT 10;
\q

Install Docker on Windows

Install interactively

  • Downloaded the installer (Docker Desktop Installer.exe) from Docker Hub
  • Double-click Docker Desktop Installer.exe to run the installer
  • When prompted, ensure the Use WSL 2 instead of Hyper-V option on the Configuration page is selected or not depending on your choice of backend > If your system only supports one of the two options, you will not be able to select which backend to use
  • Follow the instructions on the installation wizard to authorize the installer and proceed with the install
  • When the installation is successful, click Close to complete the installation process

Start Docker Desktop

Docker Desktop does not start automatically after installation. To start Docker Desktop:

  • Search for Docker, and select Docker Desktop in the search results
  • The Docker menu displays the Docker Subscription Service Agreement window.
  • Select Accept to continue. Docker Desktop starts after you accept the terms