Running parse server with docker compose

Viewed 2338

I have an scenario in which I need parse server to be run from a Dockerfile, and that docker file should be run inside docker-compose. Also, I need to externalize configuration to a json file.

The main problem is that if I run this Dockerfile inside docker-compose, config file is not found.

This is my dockerfile declaration:

FROM parseplatform/parse-server:4.2.0

COPY config.json config.json

EXPOSE 1337

CMD ["/parse-server/bin/parse-server", "config.json"]

This is my config.json file:

{
  "appId": "appId",
  "masterKey": "masterKey",
  "databaseURI": "mongodb://usr:pass@mongodb:27017"
}

This is working properly, as if I build it and the run this:

docker build -f Dockerfile -t parse  .

docker run --name parse  -p 1337:1337 -d parse config.json

It works properly, and in a nutshell, the important line from the docker logs is this:

Configuration loaded from /parse-server/config.json

But if I try to run it inside a docker-compose file this way:

version: '3.5'

services:
  db:
    image: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: usr
      MONGO_INITDB_ROOT_PASSWORD: pass

  parse:
    build: .
    ports:
      - 1337:1337
    depends_on:
      - db

It doesn't work, it just prints parse-server help, along with this line:

Configuration loaded from /parse-server/bin/parse-server

I've tested a couple of scenarios (note: doesn't work means here that the config file is not found):

  1. Declaring a command inside docker-compose to run parse-server, and give the --appId, --masterKey and --databaseURI switches manually, it works.
  2. Declaring a command inside docker-compose pointing to the config.json file doesn't work.
  3. Mount config.json file inside docker-compose container via volumes, doesn't work.
  4. Running a command with the previously mentioned switched, and then inspect the container itself to check if the file is there, and yes, it is.
  5. Copy the file to where parse-server should take it by default ( /parse-server/bin )

Questions:

  1. There is a slighty chance that config file is being copy after the parse-server command execution?
  2. How can it be that the Dockerfile alone works fine, and it doesn't work inside a docker-compose file? As far as I know, it wouldn't be that harder, as this is the proper way to run it.
  3. Any hints about what is wrong with this?

Thanks a lot!

2 Answers

Can you try the below docker-compose.yml

version: '3.5'

services:
  db:
    image: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: usr
      MONGO_INITDB_ROOT_PASSWORD: pass

  parse:
    build: .
    command: "config.json"
    ports:
      - 1337:1337
    depends_on:
      - db

command will override the CMD in Dockerfile.

Or if you don't want to specify the command over here, you can change your Dockerfile.

FROM parseplatform/parse-server:4.2.0

COPY config.json config.json

EXPOSE 1337

CMD ["config.json"].  # Changed

To go through your questions:

  1. No, the actual parse-server command is executed at runtime of the container, the copy command is run at build-time of the image
  2. The command you are running through docker is different than the one you are running in your compose command: config.json and /parse-server/bin/parse-server config.json, respectively

I'd change your docker-compose to mirror what you are running with docker:

version: '3.5'

services:
  db:
    image: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: usr
      MONGO_INITDB_ROOT_PASSWORD: pass

  parse:
    build: .
    ports:
      - 1337:1337
    depends_on:
      - db
    # this is exactly what you are handing to vanilla docker
    command: "config.json"

and run it like docker-compose up -d

However, I think your suggestion of using a volume is a bit better, that way a configuration change doesn't require re-building the image:

version: '3.5'

services:
  db:
    image: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: usr
      MONGO_INITDB_ROOT_PASSWORD: pass

  parse:
    build: .
    ports:
      - 1337:1337
    depends_on:
      - db
    volumes:
      - parse_vol:/path/to/config  # config is a folder in the container, mind you
    command: "/path/to/config/config.json"


volumes:
  parse_vol:
    driver: local
    driver-opts:
      o: bind
      type: none
      device: /local/path/to/config  # this is also a folder that contains config.json

Related