Featured image of post Portainer (2): The Best Practice Guide for Docker——docker-compose and Portainer

Portainer (2): The Best Practice Guide for Docker——docker-compose and Portainer

The best way to use docker——manage containers with docker-compose and Portainer

Motivation

In [“Container(1): Introduction to Container-related Knowledge——Containerization, Docker, Docker-compose, Kubernetes / K8s, etc.”](« relref “../docker/index.en.md” »), we introduced the concept of containerization and how to use Docker for containerization. When using Docker in practice, we may encounter some problems, such as:

  • After running multiple containers, how can we clearly know where these containers are, and how is their running status?
  • When we need to update the container, how can we ensure that the updated container is consistent with the original container?
  • When we need to run multiple containers, how can we ensure the dependency between these containers?

At the beginning, when I used Docker, some containers were started with the docker run command, and some containers were started with docker-compose, and the location of the docker-compose.yml file was not unified, which made me very confused when managing these containers. Later, I reorganized them, started all containers with docker-compose, and placed the docker-compose.yml file in a unified directory, which made it much easier to manage.

Of course, calling it the “best practice guide” may be a bit exaggerated, but this is indeed the best way I think to use Docker at the current stage.

Prerequisites

  • Docker and docker-compose are installed
  • Understand the basic concepts and basic usage of Docker

If you have not installed Docker and docker-compose, or do not understand the basic concepts and basic usage of Docker and containerization, you can refer to [“Container(1): Introduction to Container-related Knowledge——Containerization, Docker, Docker-compose, Kubernetes / K8s, etc.”](« relref “../docker/index.en.md” »), which provides a more detailed introduction.

Docker Best Practices

Manage containers with docker-compose

  1. Create a directory to store the docker-compose.yml file and related files, such as ~/docker.
  2. Create a subdirectory in this directory for each container that needs to be run, such as ~/docker/nginx.
  3. Create a docker-compose.yml file in the subdirectory to define the configuration of the container, such as ~/docker/nginx/docker-compose.yml.

We can define some templates for creating docker-compose.yml files, so that each time a new container is created, you only need to copy the template and modify the configuration. For example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
version: '3'

services:
  service-name:
    container_name: container-name
    image: image-source/image-name:tag
    environment:
      - SOME_ENV_VAR=some_value
    ports:
      - "host_port:container_port"
    deploy:
      resources:
        reservations:
          devices:
          - driver: nvidia
            capabilities: ["gpu"]
            count: all
    volumes:
      - /path/on/host:/path/in/container
    restart: always

This template defines a service, the service name is service-name, the container name is container-name, uses the image-source/image-name:tag image, sets the environment variable SOME_ENV_VAR, maps the port host_port:container_port, sets resource limits, mounts volumes, and sets the container restart policy. And you can use Nvidia’s GPU.

Start containers with docker-compose

After defining the docker-compose.yml file in the subdirectory, you can start the container using the docker-compose command. Execute in the subdirectory:

1
docker-compose up -d

If it is the first time to start the container, or you need to rebuild the container, you can use:

1
docker-compose up -d --build

If you need to stop the container, you can use:

1
docker-compose down

If you want to delete the container’s data volume, you can use:

1
docker-compose down -v

Manage containers with Portainer

Portainer is a lightweight container management tool that can be used to manage Docker containers, images, networks, etc. It includes viewing the running status, logs, resource usage, etc. of all containers, and can start, stop, and delete containers through the Web interface.

Portainer has a paid commercial version (Portainer BE) and a free community version (Portainer CE), and we can use the free community version.

We can start Portainer with docker-compose and manage containers through the Web interface. The specific steps are as follows:

  1. Create a subdirectory portainer in the ~/docker directory, and create a docker-compose.yml file in the portainer directory with the following content:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    version: '3'
    services:
    portainer:
        image: portainer/portainer-ce:latest
        ports:
        - "8001:8001"
        - "9443:9443"
        restart: always
        volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - ./portainer_data:/data
    

    Note that when I used Portainer, the latest version of the official image portainer/portainer-ce:latest is 2.19.4, but the 2.19 version of Portainer does not fully support Docker 26 and above. And the Docker version I use is 27.0.3, so I use the 2.20.1 version of Portainer, that is, portainer/portainer-ce:2.20.1.

  2. Execute in the portainer directory:

    1
    
    docker-compose up -d
    
  3. Open a browser, visit http://localhost:8001, enter the username and password (you need to set the username and password for the first login), and you can enter the Portainer management interface.

    Portainer

    Click Local, you can view all containers, images, networks, etc.:

    Portainer Local

  4. If you want to remotely manage Docker on a machine through Portainer, you can set up a reverse proxy with Nginx according to the method introduced in the previous article “Access Personal Website from Public Network——Nginx Reverse Proxy Configuration”, bind it with your domain name, and then access Portainer through the domain name.

comments powered by Disqus