Skip to content

Commit c477713

Browse files
committed
build: use scratch base image and add Docker CI workflow
Switch Dockerfile from alpine to scratch with static Go binary. Add GitHub Actions workflow to build and push to Docker Hub.
1 parent 1c02dbc commit c477713

3 files changed

Lines changed: 83 additions & 13 deletions

File tree

.github/workflows/docker.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Docker
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
tags:
8+
- 'v*'
9+
10+
jobs:
11+
docker:
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 10
14+
steps:
15+
- uses: actions/checkout@v6
16+
17+
- name: Log in to Docker Hub
18+
uses: docker/login-action@v3
19+
with:
20+
username: ${{ secrets.DOCKER_USERNAME }}
21+
password: ${{ secrets.DOCKER_TOKEN }}
22+
23+
- name: Set Docker tag
24+
id: tag
25+
run: |
26+
if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then
27+
echo "tag=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
28+
else
29+
echo "tag=beta" >> "$GITHUB_OUTPUT"
30+
fi
31+
32+
- name: Build and push
33+
uses: docker/build-push-action@v6
34+
with:
35+
context: .
36+
push: true
37+
tags: impostermocks/cli:${{ steps.tag.outputs.tag }}

Dockerfile

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.23-alpine AS builder
1+
FROM golang:1.25-alpine AS builder
22

33
WORKDIR /app
44

@@ -9,23 +9,25 @@ RUN go mod download
99
# Copy source code
1010
COPY . .
1111

12-
# Build the binary
13-
RUN go build -tags lambda.norpc -o imposter-cli
12+
# Build a statically linked binary
13+
RUN CGO_ENABLED=0 go build -tags lambda.norpc -ldflags="-s -w" -o imposter-cli
1414

15-
# Final stage
16-
FROM alpine:latest
17-
18-
RUN apk --no-cache add ca-certificates curl openjdk17-jre docker-cli
15+
# Create an empty directory to use in the scratch stage
16+
RUN mkdir /empty
1917

20-
WORKDIR /app
18+
# Final stage
19+
FROM scratch
2120

22-
# Copy the binary from builder stage
21+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
2322
COPY --from=builder /app/imposter-cli /usr/local/bin/imposter
23+
COPY --from=builder /empty /mocks
24+
COPY --from=builder /empty /tmp
25+
26+
WORKDIR /mocks
2427

25-
# Create directory for output
26-
RUN mkdir -p /output
28+
ENV IMPOSTER_ENGINE=golang
2729

2830
EXPOSE 8080
2931

30-
# Default command shows help
31-
CMD ["imposter", "--help"]
32+
ENTRYPOINT ["imposter"]
33+
CMD ["--help"]

docs/docker.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Docker
2+
3+
You can run the Imposter CLI as a Docker container, without installing it locally.
4+
5+
The container image is available at [`impostermocks/cli`](https://hub.docker.com/r/impostermocks/cli) on Docker Hub.
6+
7+
## Start mocks
8+
9+
To start a mock server using configuration in your current directory:
10+
11+
```shell
12+
docker run --rm -v $PWD:/mocks -p 8080:8080 impostermocks/cli up
13+
```
14+
15+
Your mock will be available at http://localhost:8080.
16+
17+
## Scaffold configuration
18+
19+
To generate mock configuration files in your current directory:
20+
21+
```shell
22+
docker run --rm -v $PWD:/mocks impostermocks/cli scaffold
23+
```
24+
25+
## Other commands
26+
27+
You can pass any CLI command and flags after the image name:
28+
29+
```shell
30+
docker run --rm -v $PWD:/mocks impostermocks/cli --help
31+
```

0 commit comments

Comments
 (0)