Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/etc/server/server.properties
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bin/docker_entrypoint: bin
cd go/cmd && go build -o $@
cd go/cmd && go build -o ../../$@

bin:
mkdir bin
Expand All @@ -10,5 +10,7 @@ install:
docker:
docker build --no-cache -t docker_entrypoint .


.PHONY: install docker
clean:
rm -rf bin \
docker rmi docker_entrypoint:latest
.PHONY: install docker clean
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# docker_entrypoint
Templating engine for docker images

Did you have to spend time in finding out to configure a random docker image you just downloaded from Docker registry?
Did you have to spend time in finding out to configure a random docker image you just downloaded from Docker registry?

Wouldn't be nice having the same ENV variable be the same across images without the need of replacing entirelly the whole congiguration file or having to dig `docker inspect` and just run an help command?
Wouldn't be nice having the same ENV variable be the same across images without the need of replacing entirelly the whole configuration file or having to dig `docker inspect` and just run an help command?

Docker entrypoint manages your configuration file templates rendering them from environemnt variables values.
Docker entrypoint manages your configuration file templates rendering them from environemnt variables values.

For example, setting Kafka's broker ID would be as simple as setting an environment variable
For example, setting Kafka's broker ID would be as simple as setting an environment variable
`docker run -d --name kafka -e SERVER_PROPERPERTIES_BROKER_ID = 1 mykafka-image`

and gives users an easy way to access all configuration variables with a `help` command:
and gives users an easy way to access all configuration variables with a `help` command:
`docker run -ti --rm mykafka-image --help

## Dependencies
Expand All @@ -23,20 +23,20 @@ to build the base docker_entrypoing image run

## Usage
### Build a configuration file
Create a template configuration file `server.properties.template` like the following
Create a template configuration file `server.properties.template` like the following
```
server_address={{ .LISTEN_ADDRESS }}
server_port={{ .LISTEN_PORT }}
id={{ .ID }}
```
### Example dockerfile
The build your image using docker_entrypoint as first stage of a multi-stage build:
The build your image using docker_entrypoint as first stage of a multi-stage build:
```
FROM docker_entrypoint as entrypoint
FROM alpine #OR YOU FAVOURITE BASE IMAGE
COPY --from=entrypoint /docker_entrypoint /docker_entrypoint
RUN ...
MKDIR ...
RUN mkdir ...
ENV SERVER_PROPERTIES_LISTEN_ADDRESS="localhost"
ENV SERVER_PROPERTIES_LISTEN_PORT=8080
ENV SERVER_PROPERTIES_ID=1
Expand Down
26 changes: 19 additions & 7 deletions go/render/templatebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ var templateExtension = ".template"

func ExecTemplates(paths ...string) error {
for _, path := range paths {
fmt.Println(path)
if !strings.HasSuffix(path, templateExtension) {
return fmt.Errorf("%s is not a .template file", path)
}
destPath := removeExtension(path)
envVars := getEnvironVars(replaceWithUnderscore(destPath))

filterString := strings.ToUpper(replaceWithUnderscore(destPath))
envVars, err := getEnvironVars(filterString)
if err != nil {
return fmt.Errorf("error getting environment variables for %s: %s", filterString, err)
}

t := template.New(path, destPath, envVars)
if err := t.Write(); err != nil {
return fmt.Errorf("error rendering template %s: %s", path, err)
Expand All @@ -38,14 +43,21 @@ func removeExtension(path string) string {
return path[:len(path)-len(templateExtension)]
}

func getEnvironVars(filterString string) map[string]string {
regex, _ := regexp.Compile("^" + filterString + "_")
func getEnvironVars(filterString string) (map[string]string, error) {
prefix := fmt.Sprintf("%s_", filterString)
filter := fmt.Sprintf("^%s", prefix)
regex, err := regexp.Compile(filter)

if err != nil {
return nil, err
}

vars := make(map[string]string)
for _, e := range os.Environ() {
pair := strings.SplitAfterN(e, "=", 2)
pair := strings.Split(e, "=")
if regex.MatchString(pair[0]) {
vars[pair[0][len(filterString)+1:len(pair[0])-1]] = pair[1]
vars[pair[0][len(prefix):]] = pair[1]
}
}
return vars
return vars, nil
}
6 changes: 3 additions & 3 deletions go/render/templatebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
)

func TestGetEnvironVars(t*testing.T){
func TestGetEnvironVars(t *testing.T) {
_ = os.Setenv("A_B_C", "1")
vars := getEnvironVars("A")
if vars["B_C"] != "1"{
vars, _ := getEnvironVars("A")
if vars["B_C"] != "1" {
t.Fatalf("%v", vars)
}
}
4 changes: 2 additions & 2 deletions go/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func New(templatePath string, outputPath string, vars map[string]string) Templat
t := Template{
vars,
templatePath,
trimExtension(outputPath),
outputPath,
}
return t
}
Expand Down Expand Up @@ -44,4 +44,4 @@ func (t *Template) WriteToPath(path string) error {
func trimExtension(path string) string {
var extension = filepath.Ext(path)
return path[0 : len(path)-len(extension)]
}
}
6 changes: 3 additions & 3 deletions go/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func TestMain(m *testing.M) {

func mockFile(content []byte) (string, error) {
if tempFile, err := os.Create(tempDir + "/file.temp"); err == nil {
defer func (){ _ = tempFile.Close() }()
defer func() { _ = tempFile.Close() }()
_, err = tempFile.Write(content)
return tempFile.Name(), err
}else{
} else {
return "", err
}
}
Expand All @@ -42,7 +42,7 @@ func TestTemplate(t *testing.T) {
t.Fatal(err)
}
vars := map[string]string{
testVar : testVal,
testVar: testVal,
}
template := New(templatePath, templatePath, vars)
if err := template.WriteToPath(resultPath); err != nil {
Expand Down
10 changes: 9 additions & 1 deletion test/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FROM golang:1.13-alpine

WORKDIR /src/
COPY ./go /src

CMD ls -l /src/
WORKDIR go
RUN go build -o /docker_entrypoint github.com/bejelith/docker_entrypoint/cmd

ENV SERVER_PROPERTIES_LISTEN_ADDRESS="localhost"
ENV SERVER_PROPERTIES_LISTEN_PORT=8080
ENV SERVER_PROPERTIES_ID=1

WORKDIR /src/
CMD go test ./... -v
9 changes: 6 additions & 3 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ networks:
services:
docker_entrypoint_tests:
build:
context: .
dockerfile: ./Dockerfile.test
context: ..
dockerfile: ./test/Dockerfile.test
docker_entrypoint_integration:
image: test_docker_entrypoint_tests
command: ["/docker_entrypoint","-template","/etc/server/server.properties.template","/bin/cat","/etc/server/server.properties"]
volumes:
- $PWD/../go:/src
- $PWD/etc/server:/etc/server
3 changes: 3 additions & 0 deletions test/etc/server/server.properties.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server_address={{ .LISTEN_ADDRESS }}
server_port={{ .LISTEN_PORT }}
id={{ .ID }}
3 changes: 2 additions & 1 deletion vai.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ set -euxo pipefail

cd ./test

docker-compose up --build --abort-on-container-exit
docker-compose up --build --abort-on-container-exit --scale=docker_entrypoint_integration=0
docker-compose up --build --exit-code-from docker_entrypoint_integration --scale docker_entrypoint_tests=0