Skip to content

Commit ce19a42

Browse files
add dockerfile. add make targets. add sections to the readme.
1 parent 14dc8bb commit ce19a42

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

Dockerfile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM golang:1.16.3-alpine AS builder
2+
3+
RUN apk add --update --no-cache make
4+
ENV PROJECT_NAME=tfdocs-format-template
5+
ENV SOURCE_DIR=/go/src/${PROJECT_NAME}
6+
# replace tdocs-format-template with actual formatter plugin name.
7+
WORKDIR ${SOURCE_DIR}
8+
9+
COPY go.mod .
10+
COPY go.sum .
11+
RUN go mod download
12+
13+
COPY . .
14+
RUN make build
15+
16+
################
17+
18+
FROM quay.io/terraform-docs/terraform-docs:latest
19+
# added because custom plugin should be installed into the $HOME
20+
ENV PROJECT_NAME=tfdocs-format-template
21+
ENV BUILD_CONTAINER_WD=/go/src/${PROJECT_NAME}
22+
ENV USER=docker
23+
ENV UID=12345
24+
ENV GID=23456
25+
# creating user in docker container
26+
RUN adduser \
27+
--disabled-password \
28+
--gecos "" \
29+
--home "$(pwd)" \
30+
--ingroup "root" \
31+
--uid "$UID" \
32+
"$USER"
33+
# after this, all commands will be executed from this user
34+
USER docker
35+
36+
COPY --from=builder ${BUILD_CONTAINER_WD}/bin/linux-amd64/${PROJECT_NAME} $HOME/.tfdocs.d/plugins/${PROJECT_NAME}
37+
38+
CMD ["/bin/sh","-c","terraform-docs","$@"]

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ PLUGIN_FOLDER ?= ~/.tfdocs.d/plugins
66
GOOS ?= $(shell go env GOOS)
77
GOARCH ?= $(shell go env GOARCH)
88

9+
CUR_VERSION ?=v1.0.0
10+
DEFAULT_TAG ?= $(shell echo "$(CUR_VERSION)" | tr -d 'v')
11+
DOCKER_REGISTRY :=quay.io
12+
PROJECT_OWNER := terraform-focs
13+
DOCKER_IMAGE := $(DOCKER_REGISTRY)/$(PROJECT_OWNER)/$(BUILD_NAME)
14+
DOCKER_TAG ?= $(DEFAULT_TAG)
15+
916
all: clean verify build
1017

1118
clean:
@@ -21,4 +28,10 @@ install: build
2128
mkdir -p $(PLUGIN_FOLDER)
2229
mv ./$(BUILD_DIR)/$(GOOS)-$(GOARCH)/$(BUILD_NAME) $(PLUGIN_FOLDER)
2330

31+
docker: ## Build Docker image
32+
docker build --pull --tag $(DOCKER_IMAGE):$(DOCKER_TAG) --file Dockerfile .
33+
34+
push: ## Push Docker image
35+
docker push $(DOCKER_IMAGE):$(DOCKER_TAG)
36+
2437
.PHONY: all clean verify build install help

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,58 @@ Note that the plugin has to be built for target OS and architecture (`make build
5454
and `make install` do that,) but if you want to redistribute the plugin for other
5555
people to use you have to cross-compile it (for example you can use [gox].)
5656

57+
## Docker
58+
59+
If you don't want cross-compile your plugin, you can pack it to the docker image.
60+
So the only dependency needed the user/or a server machine is `docker`.
61+
62+
### Building and publishing docker image
63+
64+
```bash
65+
make docker
66+
```
67+
68+
Before you will build your docker image you have to replace very few things in the `Makefile` and the `Dockerfile`.
69+
70+
Things to replace:
71+
72+
`Makefile`:
73+
- DOCKER_REGISTRY (could be any docker registry)
74+
- PROJECT_OWNER (how the the place file in your registry. In our company it's something like `devops/tools`)
75+
- BUILD_NAME (your project name should follow the patter `tfdocs-format-<uniqueName>` )
76+
77+
`Dockerfile`:
78+
- all **PROJECT_NAME** entries should be replaced with your project name( and it should follow the pattern `tfdocs-format-<uniqueName>`)
79+
80+
After all changes and `make docker` successful execution, you can push your new perfect image to the registry by:
81+
```bash
82+
make push
83+
```
84+
85+
### Using docker image with your custom plugin
86+
87+
I'm a little lazy, and I made a simple sh script for easy usage of docker image with terraform-docs and the custom plugin:
88+
89+
```shell
90+
#!/bin/bash
91+
92+
img=<link to the docker image with terraform-docs and custom plugin>
93+
94+
dir=$1
95+
out_file=$2
96+
97+
function printMarkdownTo() {
98+
pushd $1
99+
(docker run --rm -v `pwd`:`pwd` -w `pwd` $img .) > $2
100+
popd
101+
}
102+
103+
printMarkdownTo ${dir} ${out_file}
104+
105+
```
106+
107+
## Links
108+
57109
[terraform-docs]: https://github.com/terraform-docs/terraform-docs
58110
[plugin SDK]: https://github.com/terraform-docs/plugin-sdk
59111
[Go]: https://golang.org/

0 commit comments

Comments
 (0)