Project Icon

wiremock-docker

WireMock官方Docker镜像实现快速API模拟

wiremock-docker项目提供WireMock官方Docker镜像,用于快速部署和配置独立服务。基于Java版WireMock,支持命令行、环境变量和配置文件自定义。兼容WireMock扩展和HTTPS,提供多架构和版本镜像。适用于API模拟的开发测试场景,是一个灵活高效的解决方案。

WireMock Docker images

Main Nightly Docker Pulls a

The official Docker image for WireMock Standalone deployments. It includes WireMock for Java under the hood, and fully compatible with its features. You can learn more about WireMock standalone on the WireMock docs site.

Quick Start

In a temporary directory, checkout the demo repository, pull the Docker image, and start the WireMock instance.

docker pull wiremock/wiremock:latest
git clone https://github.com/wiremock/wiremock-docker.git
docker run -it --rm \
  -p 8080:8080 \
  -v $PWD/wiremock-docker/samples/hello/stubs:/home/wiremock \
  wiremock/wiremock:latest

You will get a CLI output like this one:

██     ██ ██ ██████  ███████ ███    ███  ██████   ██████ ██   ██
██     ██ ██ ██   ██ ██      ████  ████ ██    ██ ██      ██  ██
██  █  ██ ██ ██████  █████   ██ ████ ██ ██    ██ ██      █████
██ ███ ██ ██ ██   ██ ██      ██  ██  ██ ██    ██ ██      ██  ██
 ███ ███  ██ ██   ██ ███████ ██      ██  ██████   ██████ ██   ██

----------------------------------------------------------------
|               Cloud: https://wiremock.io/cloud               |
|               Slack: https://slack.wiremock.org              |
----------------------------------------------------------------

port:                         8080
enable-browser-proxying:      false
no-request-journal:           false
verbose:                      false
extensions:                   response-template,webhook

Supported architectures

  • x64
  • armv7
  • armv8

Supported tags

There are multiple image tags provided for end users. These tags are available on DockerHub and GitHub Packages, see the full list here. The most important tags are listed below.

Latest images

Latest WireMock 2.x images

Deprecated and experimental tags

Using WireMock in Docker

To start WireMock with the default settings:

docker run -it --rm -p 8080:8080 wiremock/wiremock

By default, the image exposes the 8080 port for HTTP. To verify the WireMock state, access http://localhost:8080/__admin/health to display the health status and the information. The /__admin/health endpoint is available for WireMock 3.1.0 or above.

A HEALTHCHECK is also built into the docker image to allow direct querying of the docker container's health status. Under the hood, this uses the same method as above to verify the status of the container.

Configuring WireMock

You can configure WireMock using the following ways:

  • Passing the CLI arguments
  • Passing Environment variables
  • Passing configuration files via volumes
  • Building a custom image using wiremock/wiremock as a base image

Passing the CLI arguments

To start with these WireMock arguments, you can add them to the end of the command line. For example, to enable HTTPs: --https-port 8443 --verbose

docker run -it --rm -p 8443.9443 wiremock/wiremock --https-port 8443 --verbose

Using environment variables

The following environment variables are supported by the image:

  • uid : the container executor uid, useful to avoid file creation owned by root
  • JAVA_OPTS : for passing any custom options to Java e.g. -Xmx128m
  • WIREMOCK_OPTIONS: CLI options to be passed to WireMock (starting from 3.2.0-2).

Example for passing the CLI options:

docker run -it --rm \
  -e WIREMOCK_OPTIONS='--https-port 8443 --verbose' \
  -p 8443.9443 \
  --name wiremock \
  wiremock/wiremock:latest

Passing configuration files as volumes

Inside the container, the WireMock uses /home/wiremock as the root from which it reads the mappings and __files directories. This means you can mount a directory containing these from your host machine into Docker and WireMock will load the stub mappings.

To mount the current directory use -v $PWD:/home/wiremock e.g.:

docker run -it --rm \
  -p 8080:8080 \
  --name wiremock \
  -v $PWD:/home/wiremock \
  wiremock/wiremock:{{ site.wiremock_version }}

Building your own image

Inside the container, the WireMock uses /home/wiremock as the root from which it reads the mappings and __files directories. This means you can copy your configuration from your host machine into Docker and WireMock will load the stub mappings.

WireMock utilizes a custom entrypoint script that passes all provided arguments as WireMock startup parameters. To modify the WireMock launch parameters it is recommended to override the entrypoint in your custom Docker image.

# Sample Dockerfile
FROM wiremock/wiremock:latest
COPY wiremock /home/wiremock
ENTRYPOINT ["/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose"]

Using WireMock extensions

You can use any WireMock extension with the Docker image. They can be added via CLI and volumes, but for most of the use-cases it is recommended to build a custom image by extending the official one.

Using extensions in CLI

For old style extensions (that don't have Java service loader metadata) you need to add the extension JAR file into the extensions directory and specify the name of the extension's main class via the --extensions parameter:

# prepare extension folder
mkdir wiremock-docker/samples/random/extensions
# download extension
wget https://repo1.maven.org/maven2/com/opentable/wiremock-body-transformer/1.1.3/wiremock-body-transformer-1.1.3.jar \
  -O wiremock-docker/samples/random/extensions/wiremock-body-transformer-1.1.3.jar
# run a container using extension 
docker run -it --rm \
  -p 8080:8080 \
  -v $PWD/wiremock-docker/samples/random/stubs:/home/wiremock \
  -v $PWD/wiremock-docker/samples/random/extensions:/var/wiremock/extensions \
  wiremock/wiremock \
    --extensions com.opentable.extension.BodyTransformer

For new style extensions the --extensions part should not be included as the extension will be discovered and loaded automatically:

# prepare extension folder
mkdir wiremock-docker/samples/random/extensions
# download extension
wget https://repo1.maven.org/maven2/org/wiremock/wiremock-grpc-extension-standalone/0.5.0/wiremock-grpc-extension-standalone-0.5.0.jar \
  -O wiremock-docker/samples/random/extensions/wiremock-grpc-extension-standalone-0.5.0.jar
# run a container using extension 
docker run -it --rm \
  -p 8080:8080 \
  -v $PWD/wiremock-docker/samples/random/stubs:/home/wiremock \
  -v $PWD/wiremock-docker/samples/random/extensions:/var/wiremock/extensions \
  wiremock/wiremock

Using extensions in the Dockerfile

git clone https://github.com/wiremock/wiremock-docker.git
docker build -t wiremock-random wiremock-docker/samples/random
docker run -it --rm -p 8080:8080 wiremock-random

Access http://localhost:8080/random to show random number

Advanced use-cases

Using HTTPs

For HTTPs, the 8443 port is exposed by default. To run with HTTPs, run the following command:

docker run -it --rm -p 8443.9443 wiremock/wiremock --https-port 8443 --verbose

To check the HTTPs on the default exposed port, use https://localhost:8443/__admin to check HTTPs working.

Using the Record Mode

In Record mode, when binding host folders (e.g. $PWD/test) with the container volume (/home/wiremock), the created files will be owned by root, which is, in most cases, undesired. To avoid this, you can use the uid docker environment variable to also bind host uid with the container executor uid.

docker run -d --name wiremock-container \
  -p 8080:8080 \
  -v $PWD/test:/home/wiremock \
  -e uid=$(id -u) \
  wiremock/wiremock \
    --proxy-all="http://registry.hub.docker.com" \
    --record-mappings --verbose
curl http://localhost:8080
docker rm -f wiremock-container

Check the created file owner with ls -alR test

However, the example above is a facility. The good practice is to create yourself the binded folder with correct permissions and to use the -u docker argument.

mkdir test
docker run -d --name wiremock-container \
  -p 8080:8080 \
  -v $PWD/test:/home/wiremock \
  -u $(id -u):$(id -g) \
  wiremock/wiremock \
    --proxy-all="http://registry.hub.docker.com" \
    --record-mappings --verbose
curl http://localhost:8080
docker rm -f wiremock-container

Check the created file owner with ls -alR test

Docker Compose

Configuration in compose file is similar to Dockerfile definition

# Sample compose file
version: "3"
services:
  wiremock:
    image: "wiremock/wiremock:latest"
    container_name: my_wiremock
    entrypoint: ["/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose"]

You can also mount your local __files and mappings files into the container e.g:

# Sample compose file
version: "3"
services:
  wiremock:
    image: "wiremock/wiremock:latest"
    container_name: my_wiremock
    volumes:
      - ./__files:/home/wiremock/__files
      - ./mappings:/home/wiremock/mappings
    entrypoint: ["/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose"]

References

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

稿定AI

稿定设计 是一个多功能的在线设计和创意平台,提供广泛的设计工具和资源,以满足不同用户的需求。从专业的图形设计师到普通用户,无论是进行图片处理、智能抠图、H5页面制作还是视频剪辑,稿定设计都能提供简单、高效的解决方案。该平台以其用户友好的界面和强大的功能集合,帮助用户轻松实现创意设计。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号