Which deployment option is best is largely a matter of personal preference.
If you have Docker installed, then building and running the container locally is a simple option.
If you use Dev Containers in VS Code, there is a Dev Container definition in the project that includes all the required dependencies. You can simply "Re-open in container" from VS Code and run from there.
If you prefer to run locally, make sure you have Node.js installed.
-
First, build the image
-
Then run the image as follows:
docker run -d -p <port>:80 marketplace-api-emulator -
The emulator will be listening on
http://localhost:<port>on the host
If you plan to connect from another container (eg connecting to the emulator container from a Dev Container), you need to create a shared Docker network. For more details see Docker Networking
For additional configuration options see configuration examples
- Note, there is a cost for running an Azure Container Instance
- Either
- build the image locally, then tag and push the image to a suitable container registry (eg Azure Container Registry, Docker Hub)
- If you are using Azure Container Registry, you can build and push the image using the Azure CLI
- Create a new Container Instance
- Ensure port 80 is exposed
- The emulator will be listening on port 80 on the public IP address of the container instance
- You will need to update the
LANDING_PAGE_URLwith this IP or FQDN- the default value (
localhost) only works when running locally - eg
LANDING_PAGE_URL=http://1.2.3.4/landing.html - You can set this as an env variable or through the config UI
- the default value (
- Consider additional network security measures as this port is open on the internet
- Make sure you have Node.js installed (tested with Version 18)
- In VS Code, open the repo
- Select "Run & Debug -> Launch Program"
- The emulator will be listening on
http://localhost:3978 - You can set environment variables by creating a .env file in the root folder
- There is a .devcontainer environment included as part of this repo
- The dev container contains all the dependencies required
- In VS Code, open the repo
- When prompted, opt to "Re-open in container" or select "Dev Containers: Reopen in Container" from the command palette
- You should now be able to "Run & Debug"
- The emulator will be listening on:
http://localhost:3978on the hosthttp://emulator:80on a shared Docker bridge network (if connecting from another container)
- You can set environment variables by creating a .env file in the root folder
If you plan to connect from another container (eg connecting to the emulator container from a Dev Container), you need to create a shared Docker network. For more details see Docker Networking
- Make sure you have Node.js installed (tested with Version 18)
- Use
npm run buildto install dependencies and build - Use
npm run startto run the emulator- Port details will be displayed the in the console
- You can set environment variables by creating a .env file in the root folder
Building as a Docker image is as simple as:
-
Clone this repo
-
cdinto the repo folder -
Build the Docker image with:
docker build -t marketplace-api-emulator -f docker/Dockerfile . -
This will create a Docker image tagged
marketplace-api-emulator
The repo includes a workflow publish-docker-image.yml to automatically build and push the Docker image to your choice of container registry (Azure Container Registry, GitHub or Docker Hub).
The workflow can be manually triggered from the GitHub Actions UI. Depending on your target registry, you may need to set some GitHub Action secrets (Settings > Secrets and variables) before running the workflow.
- Required secrets
- GitHub container registry
- No secrets necessary
- Azure container registry
REGISTRY_ACRsecret value - set to the ACR login server egmarketplaceapiemulator.azurecr.ioACR_USERNAMEsecret value - set to the ACR username (can be found in Settings > Access keys)ACR_PASSWORDsecret value - set to the ACR password (can be found in Settings > Access keys)
- Docker Hub
DOCKER_USERNAMEsecret value - set to your Docker Hub usernameDOCKER_PASSWORDsecret value - set to your Docker Hub password
- GitHub container registry
Having set the relevant secrets, launch the workflow and select the target registry in the dropdown. The container image will be built and pushed to the selected registry.
This can make the launch process as simple as
docker run <registry-name>/<repo-name>:latest
When running in a container, we have two network stacks to deal with; a host network and a container network. Docker (and therefore Dev Containers) does some clever things to make sure that an application on the host (eg your browser) can talk to applications configured in the container by allowing you to map specific ports in your container to ports in the host. When we do:
docker run -d -p 3978:80 marketplace-api-emulator
We are telling Docker to map port 3978 on the host machine to port 80 in the container. The allows us to access the emulator index page on the URL http://localhost:3978 from the host machine.
However, if we are trying to access the emulator from inside the container itself, the URL would be http://localhost:80. And if we're trying to access the emulator from a different container, http://localhost wont work as it points to the local container. We need a shared network for container <-> container; this doesn't happen by default.
It's important to be aware of this for two specific scenarios which only apply if you are running in a container (either docker run... or a Dev Container)
-
If you are developing your application in a container (eg using Dev Containers) then you need to make sure it's on the same Docker bridge network as the emulator. If you're running the emulator in a Dev Container, a Docker bridge network called
emulator-netwill be created and a hostname will be set for the emulator. This is defined in the first few lines of devcontainer.json. If you're usingdocker run..., you will need to set the hostname and create the network and bind it to the emulator container yourself. egdocker network create emulator-net docker run -d -p <port>:80 --network=emulator-net --hostname=emulator marketplace-api-emulator
You can then connect to the emulator from your container on
http://emulator -
Wherever you're developing your application, if the emulator is running in a container, you may find you need to set different
host/portfor landing page / webhook in the emulator config if you're using the built-in landing page / webhook. The landing page is accessed from a remote host while the webhook is being called from the emulator. Thus you may find yourself configuring along these lines:LANDING_PAGE_URL='http://localhost:3978' WEBHOOK_URL='http://emulator:80'