Sobes.tech
Middle

How to use a JSON file instead of a YAML file in Docker Compose?

sobes.tech AI

Answer from AI

Docker Compose by default expects configuration in YAML format. Using JSON is not directly supported. Despite the syntactic similarity between YAML and JSON, Docker Compose does not parse JSON files as the primary format.

To work with a JSON file, it must be converted to YAML beforehand. The most common way is to use a utility that performs this conversion.

Example using jq:

# Install jq (if not already installed)
# apt-get update && apt-get install -y jq

# Convert JSON to YAML and run docker-compose
jq -c '.' your_docker_compose.json | docker-compose -f - up

In this example:

  • jq -c '.' your_docker_compose.json reads the JSON file your_docker_compose.json and outputs its content in a compact JSON format to standard output.
  • | redirects the output of jq to the standard input of the docker-compose command.
  • docker-compose -f - up tells Docker Compose to read the configuration from standard input (-) and start the services.

It is important to note that this approach is a workaround and not a "native" use of JSON in Docker Compose. The original YAML format is more readable and is directly supported by Docker Compose.