Docker

Getting started with Docker and Docker Compose

How to be Dangerous with Docker as a Data Scientist | by Kyle Stahl |  Towards Data Science

Introduction

What is Docker? Docker is a container runtime that enables you to run applications in containers. A Docker host has the Docker Engine installed and allows you to easily host multiple apps or services each in it’s own container on the host. These containers contain all the code and dependencies to run the application. The Docker host can be a physical host or a virtual machine, Windows, Linux or macOS. This all makes running applications on Docker very flexible and above all easy to do!

Installing Docker

Installing Docker is very simple. How to install it can be found at the offical Docker docs. These documentations can be found here: Install Docker Engine. The installation guides are easy to follow and explain all the steps needed to install the Docker Engine on every major Linux distribution, Windows or macOS. After following the installation guide we can begin with deploying containers.

Docker repositories

First we need to download the container we want to run. To download docker images you need to pull these from a docker repository. Docker Hub is the world’s largest library and community for container images. This library can be found here: Docker Hub. On this site you will find loads of offical prebuild images ranging from Ubuntu, Nginx, MySQL and PostgreSQL container. After downloading the Docker container image you can see all the downloaded images with the docker command:

docker image ls

After running this command you will get a list with all the downloaded Docker images.

Running your first container

After installing the Docker Engine you can begin with deploying your first container. Docker is managed via the command line via the docker command. Firstly we start with starting the getting-started containers with the command below:

docker run -d -p 80:80 docker/getting-started

This command has a couple of parameters:

  • docker – the docker command line tool
  • run – specifies to run a container
  • -d – specifiec to run this container detached. This will make sure you return to the original command prompt. Not running detached will put your prompt inside the container.
  • -p 80:80 – exposes the port 80 from the host to the container port 80. This makes port 80 on the container accesable on port 80 on the host.
  • docker/getting-started – specifies which container image to use and will automatically pull this image from Docker Hub if it is not available locally.

After getting setting up this getting-started container you can continu to follow the Docker orientation and setup guide at https://docs.docker.com/get-started/.

Docker Compose

In addition to Docker we have Docker Compose. After installing docker we can improve and simplify the deployment of containers with Docker Compose. Some container deployments rely on multiple containers in conjunction to server the application. For example a webserver with a database requires the webserver container and database container.

Docker Compose it not part of a standard Docker installation. So we need to install it before we can use Docker Compose. Moreover, the installation guide can be found on the Docker documentation site here: https://docs.docker.com/compose/.

Docker Compose works with YAML files. If you read any of my Ansible blogposts you should be familiar with YAML. For example here is my Portainer Docker Compose YAML file:

version: '3.5'
services:
  portainer:
    container_name: portainer
    image: portainer/portainer-ce
    networks:
      - base_network
    ports:
      - "8000:8000"
      - "9000:9000"
    volumes:
      - "portainer_data:/data:rw"
      - "/var/run/docker.sock:/var/run/docker.sock"
    restart: unless-stopped

volumes:
  portainer_data:

networks:
  base_network:
    name: base_network
    driver: bridge

This compose file does a couple of things:

  • Create a container name portainer with image portainer/portainer-ce.
  • Attach it to the network base_network.
  • Expose port 8000 and 9000.
  • Attach the volumes portainer_data and mount /var/run/docker.sock inside the container.
  • Restart the container unless it is stopped manually.
  • Create the volume portainer_data.
  • Create the network base_network with the name base_network and driver bridge.

To deploy the Docker Compose YAML file you need to run the docker-compose command. In this instance you can spin up the portainer stack with this command:

docker-compose -f docker-compose.yml -p portainer up -d
  • docker-compose – the docker compose command
  • -f – specifies the docker-compose.yml file to use. For example the docker-compose.yml file.
  • -p – specifies the project name. For example portainer.
  • up – brings up the stack according to the docker-compose.yml file.
  • -d – run the command detached.

In conclusion using Docker Compose will improve your deployment of container stacks.

Conclusion

In conclusion these are the basics to get started with Docker and Docker Compose. You should be able to spin up your own applications in Docker containers on your homelab, Raspberry Pi, VPS or physical machine. Above all keep posted for more blog posts where I will explain a couple of applications I host myself.

Leave a Reply

Your email address will not be published. Required fields are marked *