Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Restart Container with Log Consumer is not working #2572

Closed
hahahannes opened this issue Jun 11, 2024 · 3 comments · Fixed by #2576
Closed

[Bug]: Restart Container with Log Consumer is not working #2572

hahahannes opened this issue Jun 11, 2024 · 3 comments · Fixed by #2576
Assignees
Labels
bug An issue with the library

Comments

@hahahannes
Copy link

Testcontainers version

0.31.0

Using the latest Testcontainers version?

Yes

Host OS

Linux Ubuntu 22.04

Host arch

x86

Go version

1.22

Docker version

Client: Docker Engine - Community
 Version:           20.10.18
 API version:       1.41
 Go version:        go1.18.6
 Git commit:        b40c2f6
 Built:             Thu Sep  8 23:11:43 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.18
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.6
  Git commit:       e42327a
  Built:            Thu Sep  8 23:09:30 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.9.1-docker)
  compose: Docker Compose (Docker Inc., v2.12.2)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 21
  Running: 21
  Paused: 0
  Stopped: 0
 Images: 750
 Server Version: 20.10.18
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux nvidia runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.19.0-76051900-generic
 Operating System: Pop!_OS 22.04 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 20
 Total Memory: 62.48GiB
 Name: pop-os
 ID: P6NI:BJKY:SNIH:SXGM:VHDO:AI5W:56BU:OSUH:43E4:EG72:Z3IM:ISTY
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Default Address Pools:
   Base: 172.22.0.1/16, Size: 24

What happened?

Restarting a container with a log consumer results in a error after the second start command.
In the following example, I restart a simple hello world container with a custom log consumer.
I expected that the log consumer wont cause an error.

package main

import (
	"context"
	"time"
	"fmt"
	"github.com/testcontainers/testcontainers-go"
)


type LogConsumer struct {
	LogChannel chan string
}

func (c LogConsumer) Accept(rawLog testcontainers.Log) {
	log := string(rawLog.Content)
	c.LogChannel <- log
}

func NewLogConsumer() *LogConsumer {
	return &LogConsumer{
		LogChannel: make(chan string),
	}
}

func main() {
	logConsumer := NewLogConsumer()

	ctx := context.Background()
	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: testcontainers.ContainerRequest{
			Image:           "hello-world",
			AlwaysPullImage: true,
			LogConsumerCfg: &testcontainers.LogConsumerConfig{
				Consumers: []testcontainers.LogConsumer{logConsumer},
			},
		},
		Started: false,
	})

	err = container.Start(ctx)
	if err != nil {
		fmt.Println(err)
	}
	d := 30*time.Second
	err = container.Stop(ctx, &d)
	if err != nil {
		fmt.Println(err)
	}
	err = container.Start(ctx)
	if err != nil {
		fmt.Println("Cant start container: " + err.Error())
	}
}

Relevant log output

[...]
For more examples and ideas, visit:
 https://docs.docker.com/get-started/


Cant start container: log production already started


### Additional information

_No response_
@hahahannes
Copy link
Author

hahahannes commented Jun 11, 2024

I guess this caused by starting log production when Start() is called. And one has to call container.StopLogProducer() before Start() to prevent this error.

@mdelapenya
Copy link
Collaborator

@hahahannes thanks for opening this issue. I think I have a proper fix for it here: https://github.com/testcontainers/testcontainers-go/pull/2576/files

I'd appreciate your feedback and testing if possible.

Cheers!

@mdelapenya mdelapenya self-assigned this Jun 12, 2024
@hahahannes
Copy link
Author

Thanks a lot @mdelapenya!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue with the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants