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

docker_container container_logs attribute includes Docker log entry headers #545

Open
zanecodes opened this issue Apr 26, 2023 · 0 comments

Comments

@zanecodes
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform (and docker Provider) Version

Terraform v1.3.7
on linux_amd64
+ provider registry.terraform.io/kreuzwerker/docker v3.0.2

Affected Resource(s)

  • docker_container

Terraform Configuration Files

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

resource "docker_container" "container" {
  name = "repro"
  image = "alpine:latest"
  must_run = false
  logs = true
  attach = true
  entrypoint = ["/bin/sh", "-c", "echo ''"]
}

output "logs" {
  value = docker_container.container.container_logs
}

Debug Output

https://gist.github.com/zanecodes/a3959cdc746cbea6330d91cf9aa9bfef#file-debug-log-L218

Expected Behaviour

terraform output -raw logs should output exactly what the container entrypoint echo '' outputs: a single newline character, just like docker logs repro outputs.

Actual Behaviour

terraform output -raw logs outputs the stdout prefixed with the Docker log header: 01 00 00 00 00 00 00 01 0a, which can be more easily observed by running terraform output -raw logs | xxd.

While Terraform seems to handle this fine by Unicode escaping these bytes as "\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0001\n" in the terraform.tfstate JSON, this causes GitLab's Terraform HTTP state backend to return a 400 Bad Request response when attempting to push the new state, which seems like a separate bug in GitLab that also needs to be addressed.

This can be partially worked around by using the docker_logs data source with discard_headers = true, but this should either be fixed in the docker_container resource, or the container_logs attribute should be deprecated and removed from the docker_container resource in favor of only using the docker_logs data source.

More importantly, strings in Terraform must be valid Unicode, while the stdout/stderr of a container may be an arbitrary sequence of bytes, so logs should really be base64 encoded before being returned.

Additionally, the docker_logs data source offers no way to demultiplex the stdout/stderr from the container logs. This should be done using StdCopy() as shown in the documentation comment for the client.ContainerLogs() function, and the stdout and stderr should probably be exposed as two separate attributes, e.g. stdout_base64 and stderr_base64.

Steps to Reproduce

  1. terraform apply
  2. Observe output from terraform output -raw logs | xxd
  3. Compare with docker logs repro | xxd

Important Factoids

Running docker in rootful podman on macos, but the issue is also reproducible in docker on WSL2 on Windows 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant