A simple REST API server that returns random JSON things.
docker pull ghcr.io/mitchallen/random-server:latest
docker pull mitchallen/random-server:latest
This will pull the image down from the repo if you didn't already.
This example runs the server locally on port 1220.
docker run -p 1220:3100 --name random-server ghcr.io/mitchallen/random-server:latest
If you are using something like an M1 Mac you can add the platform tag:
docker run --platform linux/amd64 -p 1220:3100 --name random-server ghcr.io/mitchallen/random-server:latest
From the doc:
The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command. That is, docker run is equivalent to the API /containers/create then /containers/(id)/start. A stopped container can be restarted with all its previous changes intact using docker start. See docker ps -a to view a list of all containers.
-
Generate a token for reading packages here: https://github.com/settings/tokens/new
-
Here's how to allow docker to access the GitHub Container Registry:
First, create a Personal Access Token (PAT) in GitHub with the appropriate permissions:
- Go to GitHub Settings → Developer Settings → Personal Access Tokens
- Select "Fine-grained tokens" or "Classic tokens"
- Ensure it has read:packages and write:packages permissions
Log in to ghcr.io using Docker:
docker login ghcr.io -u YOUR_GITHUB_USERNAME
When prompted for password, enter your PAT (not your GitHub password).
You could also store the token in (SECURE!) env var and pipe it in.
If you are familiar with gh that can also help with login.
The sleep statement is only needed if you paste this all in at once:
docker network create tempnet
docker run --pull=always -itd \
--name rando --hostname rando \
--network tempnet \
ghcr.io/mitchallen/random-server
sleep 5
docker ps
docker inspect tempnet
docker run --rm --network tempnet \
curlimages/curl:latest \
-s -H "Accept: application/json" \
http://rando:3100 | jq
docker run --rm --network tempnet \
curlimages/curl:latest \
-s -H "Accept: application/json" \
http://rando:3100/v1/people/1 | jq
docker run --rm --network tempnet \
curlimages/curl:latest \
-s -H "Accept: application/json" \
http://rando:3100/v1/people | jq
docker stop rando
docker rm rando
docker network rm tempnet
docker stop random-server
docker rm random-server
docker run -p 1220:3100 --name random-server ghcr.io/mitchallen/random-server:latest
docker ps
Assumes container is running and set to port 1220.
Note that if you are observing the console, the examples on the screen will show the docker containers internal port - you must use the one you mapped the container to.
The random values generated are seeded when the server first starts up.
This is to allow consitency when a record is returned.
You can generate new values by restarting the server.
curl http://localhost:1220
curl http://localhost:1220/v1
curl http://localhost:1220/v1/people/count
curl http://localhost:1220/v1/people
curl http://localhost:1220/v1/people/1
If you have jq
installed:
curl -s http://localhost:1220/ | jq
curl -s http://localhost:1220/v1/people/1 | jq
curl -s http://localhost:1220/v1/people | jq
curl -s http://localhost:1220/v1 | jq
{
"type": "people",
"prefix": "Mr.",
"first": "Augustus",
"last": "Gomez",
"age": 42,
"birthday": "7/8/1959",
"gender": "male",
"zip": "74948-0928",
"ssnFour": "0791",
"phone": "(509) 504-8066",
"email": "[email protected]"
}
Handy for testing what happens when an empty array is returned.
curl http://localhost:1220/
curl http://localhost:1220/v1
curl http://localhost:1220/v1/empty
curl http://localhost:1220/v1/empty/count
curl http://localhost:1220/v1/empty/1
curl http://localhost:1220/
curl http://localhost:1220/v1
curl http://localhost:1220/v1/words
curl http://localhost:1220/v1/words/count
curl http://localhost:1220/v1/words/1
{
"type": "words",
"value": "cezuwdi"
}
curl http://localhost:1220/
curl http://localhost:1220/v1
curl http://localhost:1220/v1/values
curl http://localhost:1220/v1/values/count
curl http://localhost:1220/v1/values/1
{
"type": "values",
"name": "dafe",
"value": -415365907192.2176
}
curl http://localhost:1220/
curl http://localhost:1220/v1
curl http://localhost:1220/v1/coords
curl http://localhost:1220/v1/coords/count
curl http://localhost:1220/v1/coords/1
{
"type": "coords",
"latitude": 88.43647,
"longitude": -93.31203
}
You can run multiple containers on multiple ports like this:
docker run -p 8101:3100 --name random1 ghcr.io/mitchallen/random-server:latest
docker run -p 8102:3100 --name random2 ghcr.io/mitchallen/random-server:latest
Each server should have a unique set of values.
docker stop random-server
docker stop random1
docker stop random2
docker start random-server
docker start random1
docker start random2
docker stop random-server
docker rm random-server
docker stop random-server
docker rm random-server
docker rmi ghcr.io/mitchallen/random-server:latest
make publish
New builds of the image were originally created automatically using Docker Cloud.
Current builds are built and hosted via GitHub.
To trigger a new build via a github tag I do the following:
NOTE: using annotated tags didn't trigger a new build. Use the simpler format.
Tags must match this format to trigger a build: /v[0-9.]+$/
git checkout main
git tag v1.1.0
git push origin --tags
This triggers two new builds of the Docker image: v1.1.x and latest
Docker Cloud:
My Docker Hub page for older projects:
Docker Hub page for the older image:
Docker Hub page for older image tags (before porting to github):