Lab #24: Use JSON instead of YAML compose file in Docker?

Yaml is a superset of json so any JSON file should be valid Yaml. To use a JSON file with Compose, specify the filename to use. In this lab we are going to bringup our same nginx and mysql containers using docker-compose file in json format.


Tested Infrastructure

Platform Number of Instance Reading Time
Play with Docker 1 5 min


Setup environment:

$ mkdir Myapp
$ cd Myapp

Now lets create passowrd file for our DB:

$ openssl rand -base64 32 > db_password.txt
$ openssl rand -base64 32 > db_root_password.txt

Create a myapp.json file:

   "version": "3.1",
   "services": {
      "webserver": {
         "image": "nginx:alpine",
         "container_name": "webserver",
         "restart": "unless-stopped",
         "ports": [
      "db": {
         "image": "mysql:5.7",
         "container_name": "Mysqldb",
         "restart": "unless-stopped",
         "volumes": [
         "ports": [
         "environment": {
            "MYSQL_ROOT_PASSWORD_FILE": "/run/secrets/db_root_password",
            "MYSQL_DATABASE": "wordpress",
            "MYSQL_USER": "wordpress",
            "MYSQL_PASSWORD_FILE": "/run/secrets/db_password"
         "secrets": [
   "secrets": {
      "db_password": {
         "file": "db_password.txt"
      "db_root_password": {
         "file": "db_root_password.txt"
   "volumes": {
      "db_data": null

Bringup the container and run as daemon

$ sudo docker-compose -f myapp.json up -d

List out the compose services:

$ docker-compose -f myapp.json ps
  Name                Command             State                    Ports
Mysqldb mysqld   Up>3306/tcp, 33060/tcp
webserver   nginx -g daemon off;          Up>443/tcp,>80/tcp

Verify the nginx is running:

$ curl http://localhost

Verify the Mysql db:

$ docker exec -it Mysqldb mysql -u root -p

Enter the root password which is in db_root_password.txt

Contributor -

Savio Mathew