How can I pass a multi-line variable to a docker container?

According to this comment, multi-line variables are supported with docker compose:

environment:
  KEY: |-
    line1
    line2

However, when I execute echo $KEY in the container, it has replaced the newline with spaces:

  • How can I find the docker name from a program running inside Docker
  • tar command not found in Dockerfile
  • Calling Git in Jenkins build script in Docker
  • Start a docker container based on condition
  • Docker: Unable to get the latest Boot2Docker ISO release version on initial setup behind corporate proxy
  • Understanding Mounted Directories with Fig and Docker
  • line1 line2

    Am I missing something? My docker version is 1.12.1.

  • How do I serve a Harp/nginx page from Docker?
  • Docker caching for travis builds
  • Permission Denied while trying to connect to Docker Daemon while running Jenkins pipeline in Macbook
  • Mulitple Docker Containers on Port 80 with Same Domain
  • kube-addons.service failed on CoreOS-libvirt installation
  • How can I make /etc/hosts writable by root in a Docker Container?
  • 2 Solutions collect form web for “How can I pass a multi-line variable to a docker container?”

    The YAML syntax is correct. The shell command wasn’t:

    echo "$KEY"

    prints the string with newlines.

    Had the same problem a couple of days ago and solved it via:

    KEY: "line1\nline2"
    

    Hope that helps in your case as well.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.