puppetlabs-docker icon indicating copy to clipboard operation
puppetlabs-docker copied to clipboard

Using Compose with build statement a container is not detected as running and recreated with each run

Open vb-linetco opened this issue 1 year ago • 10 comments

Describe the Bug

Using docker_compose together with a docker-compose.yaml with a custom build image like for example:

  • docker-compose.yml:
version: '3.5'

services:
  example:
    build:
      context: .
      dockerfile: Dockerfile
  • Dockerfile:
FROM alpine:3.14
CMD /bin/sh
  • docker_example.pp:
class docker_example {

file { [
    "/srv/docker-configs",
    "/srv/docker-configs/example"]:
    ensure => directory,
    group  => 'root',
    owner  => 'root'
}

file { '/srv/docker-configs/example/Dockerfile':
    mode    => '644',
    source  => 'example/Dockerfile',
    require => File["/srv/docker-configs/example"]
}

exec {"docker-compose build":
    cwd => "/srv/docker-configs/example",
    path => "/usr/local/bin", # for updated docker-compose
    refreshonly => true,
    subscribe => [
      File["/srv/docker-configs/example/Dockerfile"],
    ],
    notify => Docker_compose["example"]
  }

  docker_compose { "example":
     compose_file => ["/srv/docker-configs/example/docker-compose.yml"],
     ensure => present,
     require => File["/srv/docker-configs/example/docker-compose.yml"],
  }
}

And having docker-compose >= 2.8.0 installed, one can observe the following issue.

Puppet does not detect the docker-container as already running and tries to recreates it with every puppet run. The following message can be observed in the logs of puppet with each run

puppet-agent: (/Stage[main]/Docker_example/Dockercompose[example]/Docker_compose[example]/ensure) created (corrective)

Expected Behavior

The container is not attempted to be created as a corrective measure with each puppet agent run when it is already running.

Steps to Reproduce

Steps to reproduce the behavior:

  1. using the above setup
  2. run puppet agent -t

Environment

  • Puppet Server Version 5.5.22
  • Module Version Version 3.10.2, also tried more recent versions.
  • Platform Ubuntu 22.04
  • Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1
  • Docker Compose version v2.18.0

Additional Context

During further analysis I was able to track the issue down to following changes:

  • The separator for images built by docker-compose has been changed from _ to - with this commit: Docker compose
  • The Migrate to Compose V2 guide also states that this has been changed due to _ not being a valid character for DNS-names.
  • From July 2023 Compose V1 stopped receiving updates. It’s also no longer available in new releases of Docker Desktop.

Workaround:

  • With a change of the separator https://github.com/puppetlabs/puppetlabs-docker/blob/65e36c1d77bf5212ca34601d87e32cb75e58b699/lib/puppet/provider/docker_compose/ruby.rb#L71 to -, the issue cannot be observed anymore.

vb-linetco avatar Oct 17 '23 12:10 vb-linetco

If you need more information please let me know.

vb-linetco avatar Oct 17 '23 15:10 vb-linetco

I think you are using a different module. There is no dockercompose type in this module.

kenyon avatar Oct 17 '23 23:10 kenyon

I think you are using a different module. There is no dockercompose type in this module.

Yes you were right, we had another wrapper around docker_compose, I did not notice that, I've updated the setup

vb-linetco avatar Oct 18 '23 06:10 vb-linetco

Puppet 5 is EOL, and v3.10.2 of this module is very old.

kenyon avatar Oct 18 '23 07:10 kenyon

Puppet 5 is EOL, and v3.10.2 of this module is very old.

As i stated, a more recent version was behaving the same.

vb-linetco avatar Oct 18 '23 07:10 vb-linetco

And the changed line that fixes the issue is the same for all affected version.

vb-linetco avatar Oct 18 '23 07:10 vb-linetco

I'm not sure whether the issue is a bug or just requires an enhancement for supporting docker-compose > 2.8. One could check the version of docker-compose and use _ as a separator for docker-compose prior to 2.8 and - for >= 2.8

vb-linetco avatar Oct 20 '23 09:10 vb-linetco

Sounds like this is a duplicate of #891.

kenyon avatar Oct 20 '23 17:10 kenyon

Sounds like this is a duplicate of #891.

You are probably right on this, i did not notice that docker-compose-plugin is actually like docker-compose v2.

vb-linetco avatar Oct 26 '23 13:10 vb-linetco

I still encounter this bug with v10.0.1 which uses the right docker compose.

beatreichenbach avatar Aug 22 '24 05:08 beatreichenbach