mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.61k stars 1.08k forks source link

Docker > exception while persisting expectations to persistedExpectations.json #1184

Closed ghilainm closed 2 years ago

ghilainm commented 2 years ago

Describe the issue Trying to persist expectation inside docker container run into permission defined.

What you are trying to do Description of what you are trying to do so we can understand the context of the problem

MockServer version 5.11.2

To Reproduce Use a docker-compose file. Try to upload an open API definition.

Compose snippet:

  mock-server:
    image: mockserver/mockserver:mockserver-5.11.2
    environment:
      MOCKSERVER_PERSIST_EXPECTATIONS: "true"
    ports:
      - "1080:1080"

Expected behaviour No excption

MockServer Log

2022-02-11 12:48:55 5.11.2 SEVERE 1080 exception while persisting expectations to persistedExpectations.json 
2022-02-11T12:48:55.348493906Z java.io.FileNotFoundException: persistedExpectations.json (Permission denied)
2022-02-11T12:48:55.348500316Z  at java.base/java.io.FileOutputStream.open0(Native Method)
2022-02-11T12:48:55.348504853Z  at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
2022-02-11T12:48:55.348508781Z  at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
2022-02-11T12:48:55.348512573Z  at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
2022-02-11T12:48:55.348516535Z  at org.mockserver.persistence.ExpectationFileSystemPersistence.updated(ExpectationFileSystemPersistence.java:83)
2022-02-11T12:48:55.348520674Z  at org.mockserver.mock.listeners.MockServerMatcherNotifier.lambda$notifyListeners$0(MockServerMatcherNotifier.java:27)
2022-02-11T12:48:55.348524295Z  at org.mockserver.scheduler.Scheduler.run(Scheduler.java:86)
2022-02-11T12:48:55.348528020Z  at org.mockserver.scheduler.Scheduler.lambda$submit$1(Scheduler.java:144)
2022-02-11T12:48:55.348531682Z  at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
2022-02-11T12:48:55.348535247Z  at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
2022-02-11T12:48:55.348538777Z  at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
2022-02-11T12:48:55.348542430Z  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
2022-02-11T12:48:55.348546083Z  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
2022-02-11T12:48:55.348549950Z  at java.base/java.lang.Thread.run(Thread.java:834)
ghilainm commented 2 years ago

The workaround to solve that issue is to create the config directory beforehand with a custom docker image.

FROM mockserver/mockserver:mockserver-5.11.2

COPY config config

ENV MOCKSERVER_PROPERTY_FILE "/config/mockserver.properties"
ENV MOCKSERVER_PERSISTED_EXPECTATIONS_PATH "/config/mockserverInitialization.json"
ENV MOCKSERVER_INITIALIZATION_JSON_PATH "/config/mockserverInitialization.json"
ENV MOCKSERVER_PERSIST_EXPECTATIONS "TRUE"

EXPOSE 1080
jamesdbloom commented 2 years ago

MockServer can't persistence files to a read only file system, your'll need something like the following:

  mock-server:
    image: mockserver/mockserver:mockserver-5.12.0
    environment:
      MOCKSERVER_LOG_LEVEL: DEBUG
      MOCKSERVER_PERSIST_EXPECTATIONS: "true"
      MOCKSERVER_PERSISTED_EXPECTATIONS_PATH: /config/persistedExpectations.json
    volumes:
      - ./config:/config

This will save the expectations to a config subdirectory (i.e. ./config) inside the directory where you run docker-compose from.