user2771704
user2771704

Reputation: 6202

Does Docker stack in Portainer support `Docker compose profiles`?

I am using the next tools versions:

I am trying to setup the stack from Git repository via Portainer by following the next instruction from official docs.

I am using 2 profiles: dev and prod so my docker docker-compose.yml file looks like below:

version: '3.4'

services:
  backend:
    image: ${DOCKER_REGISTRY-}myproject
    profiles: ["dev", "prod"]

  db:
    image: postgres:15.2-alpine3.17
    profiles: ["dev", "prod"]

  frontend:
    image: ${DOCKER_REGISTRY-}myproject_frontend
    profiles: ["dev", "prod"]

volumes:
  db_data:
  frontend_data:
  logs:

When I tried to use my prod profile in Portainer stack via COMPOSE_PROFILES ENV variable as was proposed in this Portainer issue like COMPOSE_PROFILES=prod docker compose up then I get the same error if I don't use any ENV variable at all. The error says:

Deployment error
failed to deploy a stack: no service selected

I tried to specify the path to the docker-compose.prod.yml file in the Additional paths field, but got the same error as above. If I try to use the path to docker-compose.prod.yml in Compose path then I got the the next error for every named volume:

 service "frontend" refers to undefined volume "frontend_data"

I thought that something wrong in my docker-compose.prod.yml file, but when I copied its content to docker-compose.yml (and also deleted COMPOSE_PROFILES ENV variable) then Stack was successfully created.

So my question about ability to use Docker compose profiles in Portainer Stacks. Is it impossible or am I doing something wrong?

Upvotes: 0

Views: 1369

Answers (1)

user2771704
user2771704

Reputation: 6202

I abandoned Docker stack (wanted to use it in the first place to automatically update containers after GitHub repository changes) and used GitHub's Container Registry + Watchtower (containrrr/watchtower).

below my working code from .github/workflows/publish.yml, maybe it will be useful for someone

on:
  # any code pushed to master and develop branch will be a trigger
  push:
    branches: ["master", "develop"]

name: Deploy
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Build Compose Images
        run: docker compose -f docker-compose.publish.yml build

      # seems that `secrets.GITHUB_TOKEN` and `github.actor` provided by Github automatically (https://docs.github.com/en/actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow)
      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

      - name: Push image to Container Registry
        run: docker compose -f docker-compose.publish.yml push

some side notes about docker-compose.publish.yml

  • used version: '3.8' at the first line
  • used directive code for db service with Postgres to avoid its publishing
    deploy:
      replicas: 0
  • used next image directive for frontend and backend services, container_name directive is the same as project_name in image
image: ghcr.io/name_of_our_organization_on_github/project_name_backend # `:latest` version used by default
container_name: project_name_backend
...
image: ghcr.io/name_of_our_organization_on_github/project_name_frontend # `:latest` version used by default
container_name: project_name_frontend

Upvotes: 1

Related Questions