- Published on
Setup Docker on Ubuntu
- Authors
- Name
- Emil Moe
Introduction
Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. In this guide, we'll walk through the process of setting up Docker on Ubuntu. By the end, you'll have Docker installed and ready to use, allowing you to run and manage containers on your server. We'll cover the essentials to get you started, leaving more advanced configurations for future updates.
Install Docker
Before diving in, make sure Docker and Docker Compose are installed on your system. This guide assumes you're using Ubuntu, which is a popular choice for web servers. If you're on Debian or a similar distribution, these instructions should work for you too.
To install or upgrade Docker and Docker Compose on Ubuntu, simply copy and paste the following command:
apt-get update && apt-get upgrade -yq && apt-get install -yq docker.io docker-compose
First we ensure your server is updated with the latest patches. Next Docker and Docker Compose are installed. If you are not logged in as root
remember to add sudo
in front of each command – they are split by &&
.
Docker Compose
Docker Compose is a tool that simplifies the process of managing multi-container Docker applications. With Docker Compose, you can define and run complex applications with multiple services using a simple YAML file. This file specifies the services, networks, and volumes required, making it easy to configure and start all the services with a single command. Whether you're setting up a web server, database, or other microservices, Docker Compose streamlines the workflow and ensures consistency across different environments. It's a great tool for orchestrating multi-container applications without setting up a cluster such as Kubernetes.
Manage Log Files
Without changing config files over time your log files might extend to take up all disk space and forcing all your services to fail. Believe me, it's a frustrating incident and usually not one you catch at first guess. To overcome this you should limit the log files to something that make sense in your setup.
Logs are stored in /etc/docker/daemon.json
Reference: https://docs.docker.com/config/containers/logging/configure/
Size
For most cases you would only look in most recent logs and won't need to go back days or weeks. I advice you to start with 250mb and adjust it if it's not enough. How long the retention is is hard to say as it depends on how much output you have to the logs files.
Retention
Logs can also be configured with retention rules, for example keeping 14 days at most. Like above, depending on the amount you output to your logs, the size over those 14 days is not fixed.
Rotation
I would strongly suggest to rotate the logs, to avoid 1 huge log file. The number should be a reflection of your size and retention rules. For example 14 days you might want 14 files, 100mb you might want 10 files and so on.
Bringing it Together
My advice is to use a combination of all above. Which numbers make sense is up to your use case, but with above in mind you can use this configuration as inspiration:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "250m",
"max-age": "14d",
"max-file": "14"
}
}
Move Docker Data
This is defining where you are storing the data for your containers, images, etc. When you are downloading an image or starting up a container, it's internal storage is placed here. When you attach an external volume, you define a patch hence it's a different location than here.
If you are using DigitalOcean or similar with block storage, I suggest attaching a Volume and use that as your data root.
Before changing this option, remember to stop Docker first:
service docker stop
If you are already running Docker, you need to copy your existing data to the new folder. The default data-root is /var/lib/docker
. If it's a new setup you don't have to copy anything.
The config is in /etc/docker/daemon.json
{
"data-root": "/mnt/docker-data"
}
Now you can start Docker again:
service docker start
Delegate Access
It's best to avoid managing Docker as the root user to enhance security and reduce the risk of unintended system-wide changes. Running Docker commands as root can expose your system to vulnerabilities and potential misuse. Instead, create a dedicated user called docker
:
sudo adduser docker
Then, add this user to the docker
group to grant it the necessary permissions to manage Docker without needing root access:
sudo usermod -aG docker docker
By doing this, you minimize security risks and follow best practices for system administration. Additionally, for better organization and ease of access, you can keep docker-compose
projects in this user’s home directory, such as /home/docker/projects/<project name>
. This setup ensures a cleaner, more secure environment for managing your Docker applications.
Conclusion
That was how to install Docker and Docker Compose and a few key essentials to remember in the configurations.
For sake of clarity I have combined the config we went through in 1 snippet:
{
"data-root": "/mnt/docker-data",
"log-driver": "json-file",
"log-opts": {
"max-size": "250m",
"max-age": "14d",
"max-file": "14"
}
}