How to Install Portainer with docker-compose on Ubuntu

How to Install Portainer with docker-compose on Ubuntu

Managing Docker containers can sometimes feel overwhelming, especially when juggling multiple services. Portainer is a fantastic tool designed to simplify the process by providing an intuitive web-based interface for managing your Docker environment. In this guide, we'll walk through installing Portainer using Docker Compose on an Ubuntu system.


Prerequisites

  1. Docker: Ensure Docker is installed on your Ubuntu system. If not, you can install it by following the install docker on Ubuntu.
  2. Docker Compose: Make sure Docker Compose is installed. If it's not, you can install it using:
sudo apt update
sudo apt install docker-compose

Step-by-Step Installation Guide

Step 1: Create a Docker Compose File

First, we'll set up a docker-compose.yml file that defines the Portainer service. This file should be created in a directory where you want to store your Portainer configuration.

Create and open the file:

mkdir portainer && cd portainer
nano docker-compose.yml

Then, add the following configuration to your docker-compose.yml file:

version: '3.9'

services:
  portainer:
    container_name: portainer
    image: portainer/portainer-ce:2.21.4
    restart: always
    ports:
      - "8001:8000"
      - "9443:9443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data

volumes:
  portainer_data:

This configuration sets up Portainer to automatically restart (even after reboot), uses the container name portainer, and makes it accessible on ports 8001 and 9443.


Step 2: Adjust Docker Permissions

To let Portainer has permission to access /var/run/docker.sock, add your current user to the Docker group:

sudo usermod -aG docker $USER

After running this command, you’ll need to log out and log back in for the changes to take effect.


Step 3: Start the Portainer Service

With the Docker Compose file in place and permissions configured, you can start Portainer using Docker Compose:

docker-compose up -d

The -d flag runs the service in detached mode, meaning it runs in the background.


Step 4: Access the Portainer Interface

Once Portainer is running, open a web browser and navigate to:

  • https://your-server-ip:9443

Replace your-server-ip with the actual IP address of your Ubuntu server.

You'll be prompted to set up an admin user and password on first access. After completing the initial setup, you can start managing your Docker containers using Portainer’s interface.


Bonus: Monitor Docker Across Other Instances

For those interested in monitoring Docker activities on other servers, you can install the Portainer Agent. Here's a quick command to get it up and running:

sudo docker run -d -p 9001:9001 --name portainer_agent --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker/volumes:/var/lib/docker/volumes -v /:/host portainer/agent:2.21.4

Or use docker-compose.yml:

version: '3.8'  
services:
  portainer_agent:
    container_name: portainer_agent
    image: portainer/agent:2.21.4
    restart: always
    ports:
      - "9001:9001"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /var/lib/docker/volumes:/var/lib/docker/volumes
      - /:/host

Conclusion

By integrating Portainer with Docker Compose on your Ubuntu server, you've taken the hassle out of managing Docker containers. With its user-friendly interface, Portainer enables you to effortlessly view, stop, start, and monitor your containers—all from your web browser.

Feel free to explore Portainer’s extensive features, such as managing stacks and networks, to optimize your Docker ecosystem further. If you encounter any issues or have questions, don't hesitate to reach out in the comments. Happy containerizing!

Do you enjoy this blog post?

Read more