Add CLI option -mocks-directory
If not empty loads all the mocks from files (YAML/JSON) in a folder at startup.
Can be used to create dedicated mocks docker image from the smocker official image.
This is exactly what we need to fit our use case of deploying smocker as a mock server on k8s
+1 IMO very good idea for quick mock deployments.
This is exactly what we need to fit our use case of deploying smocker as a mock server on k8s
While not ideal, this is the interim solution I've implemented to solve this.
The file in question is mounted as a sidecar in K8s, so it can be updated without needing to update the image itself.
Dockerfile
# Necessary to wrap the smocker image so that we can install curl and inotify-tools
# This allows us to post the mocks to smocker without having to restart the container
# Use the original image
FROM --platform=linux/amd64 thiht/smocker
USER root
# Install curl and inotify-tools
RUN apk --no-cache add curl inotify-tools sed
RUN cd /opt
# Add a script to run smocker and watch for file changes
COPY run-smocker-and-watch.sh .
RUN chmod +x run-smocker-and-watch.sh
RUN sed -i 's/\r$//' run-smocker-and-watch.sh # Trim Windows line endings
RUN chown 1000:1000 run-smocker-and-watch.sh
EXPOSE 8080
EXPOSE 8081
# Set the script as the default command
CMD ["/opt/run-smocker-and-watch.sh"]
Script
#!/bin/sh
echo "Starting script..."
set -x
# Start smocker in the background
/opt/smocker --history-retention=100 &
echo "Smocker started..."
# Wait for smocker to start up
sleep 5
echo "Sending mocks..."
# Define the file to watch
FILE_TO_WATCH="/scripts/mock-definitions.yaml"
echo "File to watch: $FILE_TO_WATCH"
ls -l "$FILE_TO_WATCH"
curl -X POST -H "Content-Type: application/x-yaml" http://localhost:8081/mocks --data-binary @"$FILE_TO_WATCH"
echo "Mocks sent..."
# Watch for changes to the file and re-split and send when it changes
inotifywait -m -e modify "$FILE_TO_WATCH" | while read -r path event file; do
echo "Mocks file changed, reloading mocks"
curl -X POST http://localhost:8081/reset
curl -X POST -H "Content-Type: application/x-yaml" http://localhost:8081/mocks --data-binary @"$FILE_TO_WATCH"
done