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. Running your selfhosted applications in your homelab is going to easy with 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.
To download docker images you need to pull these from a docker repository. You can create your own docker image and repository but the easiest way to start is to use the prebuild images on Docker Hub. Docker Hub is the world’s largest library and community for container images. Docker Hub can be found here: Docker Hub. Docker Hub contains loads of offical prebuild images ranging from Ubuntu, Nginx, MySQL and PostgreSQL container.
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. The best way to explain how to start a container is to explain the docker command to spin up a getting-started container.
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 automaticly 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/.
Now that you are more familiar with Docker we are going to expand this with Docker Compose. Some applications for example a dynamic website need multiple containers to run. For this example you need a webserver and a database container. Deploying these kind of applications can be simplified and sped up with Docker Compose. Docker Compose allows you to spin up a stack containing multiple containers, volumes and networks with a single YAML file.
First we need to install Docker Compose on the host that is running the Docker Engine. Like before Docker already has an excellent guide for installing Docker Compose. The guide can be found here: Install Docker Compose.
Docker Compose works with YAML files. If you read any of my Ansible blogposts you should be familiar with YAML. For this example i will be showing you my Portainer compose file. Portainer is a webbased management GUI for Docker.
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.
- Restarts 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. For this example 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.
- -p – specifies the project name. For this example portainer.
- up – brings up the stack according to the docker-compose.yml file.
- -d – run the command detached.
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. Keep posted for more blog posts where I will explain a couple of applications I host myself.