• Stars
    star
    177
  • Rank 211,014 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated 5 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Utility to simplify running applications in docker containers

dockerize

Release Docker Automated Build CI/CD Go Report Card Coverage Status

Utility to simplify running applications in docker containers.

About this fork: This fork is supposed to become a community-maintained replacement for not maintained original repo. Everyone who has contributed to the project may become a collaborator - just ask for it in PR comments after your PR has being merged.

Table of Contents

Overview

dockerize is a utility to simplify running applications in docker containers. It allows you to:

  • generate application configuration files at container startup time from templates and container environment variables
  • Tail multiple log files to stdout and/or stderr
  • Wait for other services to be available using TCP, HTTP(S), unix before starting the main process.

The typical use case for dockerize is when you have an application that has one or more configuration files and you would like to control some of the values using environment variables.

For example, a Python application using Sqlalchemy might not be able to use environment variables directly. It may require that the database URL be read from a python settings file with a variable named SQLALCHEMY_DATABASE_URI. dockerize allows you to set an environment variable such as DATABASE_URL and update the python file when the container starts. In addition, it can also delay the starting of the python application until the database container is running and listening on the TCP port.

Another use case is when the application logs to specific files on the filesystem and not stdout or stderr. This makes it difficult to troubleshoot the container using the docker logs command. For example, nginx will log to /var/log/nginx/access.log and /var/log/nginx/error.log by default. While you can sometimes work around this, it's tedious to find a solution for every application. dockerize allows you to specify which logs files should be tailed and where they should be sent.

See A Simple Way To Dockerize Applications

Installation

Dockerize is a statically compiled binary, so it should work with any base image.

To download it with most base images all you need is to install curl first:

### alpine:
apk add curl

### debian, ubuntu:
apt update && apt install -y curl

and then either install the latest version:

curl -sfL $(curl -s https://api.github.com/repos/powerman/dockerize/releases/latest | grep -i /dockerize-$(uname -s)-$(uname -m)\" | cut -d\" -f4) | install /dev/stdin /usr/local/bin/dockerize

or specific version:

curl -sfL https://github.com/powerman/dockerize/releases/download/v0.11.5/dockerize-`uname -s`-`uname -m` | install /dev/stdin /usr/local/bin/dockerize

If curl is not available (e.g. busybox base image) then you can use wget:

### busybox: latest version
wget -O - $(wget -O - https://api.github.com/repos/powerman/dockerize/releases/latest | grep -i /dockerize-$(uname -s)-$(uname -m)\" | cut -d\" -f4) | install /dev/stdin /usr/local/bin/dockerize

### busybox: specific version
wget -O - https://github.com/powerman/dockerize/releases/download/v0.11.5/dockerize-`uname -s`-`uname -m` | install /dev/stdin /usr/local/bin/dockerize

PGP public key for verifying signed binaries: https://powerman.name/about/Powerman.asc

curl -sfL https://powerman.name/about/Powerman.asc | gpg --import
curl -sfL https://github.com/powerman/dockerize/releases/download/v0.11.5/dockerize-`uname -s`-`uname -m`.asc >dockerize.asc
gpg --verify dockerize.asc /usr/local/bin/dockerize

Docker Base Image

The powerman/dockerize image is a base image based on alpine linux. dockerize is installed in the $PATH and can be used directly.

FROM powerman/dockerize
...
ENTRYPOINT dockerize ...

Install in Docker Image

You can use multi-stage build feature to install dockerize in your docker image without changing base image:

FROM powerman/dockerize:0.19.0 AS dockerize
FROM node:18-slim
...
COPY --from=dockerize /usr/local/bin/dockerize /usr/local/bin/
...
ENTRYPOINT ["dockerize", ...]

Usage

dockerize works by wrapping the call to your application using the ENTRYPOINT or CMD directives.

This would generate /etc/nginx/nginx.conf from the template located at /etc/nginx/nginx.tmpl and send /var/log/nginx/access.log to STDOUT and /var/log/nginx/error.log to STDERR after running nginx, only after waiting for the web host to respond on tcp 8000:

CMD dockerize -template /etc/nginx/nginx.tmpl:/etc/nginx/nginx.conf -stdout /var/log/nginx/access.log -stderr /var/log/nginx/error.log -wait tcp://web:8000 nginx

Command-line Options

You can specify multiple templates by passing using -template multiple times:

$ dockerize -template template1.tmpl:file1.cfg -template template2.tmpl:file3

Templates can be generated to STDOUT by not specifying a dest:

$ dockerize -template template1.tmpl

Template may also be a directory. In this case all files within this directory are recursively processed as template and stored with the same name in the destination directory. If the destination directory is omitted, the output is sent to STDOUT. The files in the source directory are processed in sorted order (as returned by ioutil.ReadDir).

$ dockerize -template src_dir:dest_dir

If the destination file already exists, dockerize will overwrite it. The -no-overwrite flag overrides this behaviour.

$ dockerize -no-overwrite -template template1.tmpl:file

You can tail multiple files to STDOUT and STDERR by passing the options multiple times. (These options can't be combined with -exec.)

$ dockerize -stdout info.log -stdout perf.log

If your file uses {{ and }} as part of it's syntax, you can change the template escape characters using the -delims.

$ dockerize -delims "<%:%>" -template template1.tmpl

You can require all environment variables mentioned in template exists with -template-strict:

$ dockerize -template-strict -template template1.tmpl

HTTP headers can be specified for http/https protocols. If header is specified as a file path then file must contain single string with Header: value.

$ dockerize -wait http://web:80 -wait-http-header "Authorization:Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="

Required HTTP status codes can be specified, otherwise any 2xx status will be accepted.

$ dockerize -wait http://web:80 -wait-http-status-code 302 -wait-http-status-code 200

HTTP redirects can be ignored:

$ dockerize -wait http://web:80 -wait-http-skip-redirect

Dockerize process can be replaced with given command:

$ dockerize -exec some-command args...

Waiting for other dependencies

It is common when using tools like Docker Compose to depend on services in other linked containers, however oftentimes relying on links is not enough - whilst the container itself may have started, the service(s) within it may not yet be ready - resulting in shell script hacks to work around race conditions.

Dockerize gives you the ability to wait for services on a specified protocol (file, tcp, tcp4, tcp6, http, https, amqp, amqps and unix) before starting your application:

$ dockerize -wait tcp://db:5432 -wait http://web:80 -wait file:///tmp/generated-file

Multiple URLs can also be specified with -wait-list flag, that accept a space-separated list of URLs. The behaviour is equivalent to use multiple -wait flags. The two flags can be combined.

This command is equivalent to the one above:

$ dockerize -wait-list "tcp://db:5432 http://web:80 file:///tmp/generated-file"

Timeout

You can optionally specify how long to wait for the services to become available by using the -timeout # argument (Default: 10 seconds). If the timeout is reached and the service is still not available, the process exits with status code 123.

$ dockerize -wait tcp://db:5432 -wait http://web:80 -timeout 10s

See this issue for a deeper discussion, and why support isn't and won't be available in the Docker ecosystem itself.

Delay before retrying

You can optionally specify how long to wait after a failed -wait check by using the -wait-retry-interval # argument (Default: 1 second).

Waiting for 5 seconds before checking again of a currently unavailable service:

$ dockerize -wait tcp://db:5432 -wait-retry-interval 5s

Use custom CA for SSL cert verification for https/amqps connections

$ dockerize -cacert /path/to/ca.pem -wait https://web:80

Skip SSL cert verification for https/amqps connections

$ dockerize -skip-tls-verify -wait https://web:80

Injecting env vars from INI file

You can load defaults for missing env vars from INI file. Multiline flag allows parsing multiline INI entries. File with header must contain single string with Header: value.

$ dockerize -env /path/to/file.ini -env-section SectionName -multiline …
$ dockerize -env http://localhost:80/file.ini \
    -env-header "Header: value" -env-header /path/to/file/with/header …

Using Templates

Templates use Golang text/template. You can access environment variables within a template with .Env.

{{ .Env.PATH }} is my path

In template you can use a lot of functions provided by Sprig plus a few built in functions as well:

  • exists $path - Determines if a file path exists or not. {{ if exists "/etc/default/myapp" }}
  • parseUrl $url - Parses a URL into it's protocol, scheme, host, etc. parts. Alias for url.Parse
  • isTrue $value - Parses a string $value to a boolean value. {{ if isTrue .Env.ENABLED }}
  • jsonQuery $json $query - Returns the result of a selection query against a json document.
  • readFile $fileName - Returns the content of the named file or empty string if file not exists.

WARNING! Incompatibility with original dockerize v0.6.1! These template functions was changed because of adding Sprig functions, so carefully review your templates before upgrading:

  • default - order of params has changed.
  • contains - now it works on string instead of map, use hasKey instead.
  • split - now it split into map instead of list, use splitList instead.
  • replace - order and amount of params has changed.
  • loop - removed, use untilStep instead.

jsonQuery

Objects and fields are accessed by name. Array elements are accessed by index in square brackets (e.g. [1]). Nested elements are separated by dots (.).

Examples:

With the following JSON in .Env.SERVICES

{
  "services": [
    {
      "name": "service1",
      "port": 8000,
    },{
      "name": "service2",
      "port": 9000,
    }
  ]
}

the template expression jsonQuery .Env.SERVICES "services.[1].port" returns 9000.

More Repositories

1

go-monolith-example

Example Go monolith with embedded microservices and The Clean Architecture
Go
290
star
2

go-service-example

Example Go service using go-swagger and Clean Architecture
Go
161
star
3

asciidoc-cheatsheet

Asciidoc cheatsheet for GitHub
Perl
144
star
4

rpc-codec

JSON-RPC 2.0 codec for Go net/rpc standard library
Go
96
star
5

vim-plugin-viewdoc

Vim plugin: flexible viewer for any documentation
Vim Script
89
star
6

vim-plugin-ruscmd

Vim plugin: support command mode in Russian keyboard layout
Vim Script
60
star
7

wcwidth-icons

Support fonts with double-width icons in xterm/rxvt-unicode/zsh/vim/…
C
41
star
8

vim-plugin-autosess

Vim plugin: auto save/load sessions
Vim Script
35
star
9

dotvim

~/.vim/
Vim Script
24
star
10

structlog

Structured logger for Go
Go
19
star
11

vcprompt

Version control information in your prompt
C
14
star
12

powerman-overlay

Powerman's Gentoo overlay
Shell
12
star
13

nerd-fonts-sh

Shell variables with names for Nerd fonts icons
Shell
9
star
14

tail

Go package tail implements behaviour of `tail -F` to follow rotated log files
Go
8
star
15

inferno-re2

OS Inferno driver: re2 library
Limbo
7
star
16

Narada

Framework for ease deploy and support microservice projects
Perl
7
star
17

jquery-tbodyscroll

jQuery plugin: add scrolling for table tbody element
JavaScript
6
star
18

asciidoc-habrahabr-backend

AsciiDoc backend for generating Habrahabr friendly HTML
6
star
19

check

Helpers to complement Go testing package
Go
6
star
20

sensitive

Package sensitive provides base types who's values should never be seen by the human eye, but still used for configuration.
Go
4
star
21

inferno-opt-setup

Setup projects in /opt for OS Inferno
Shell
4
star
22

testcert

TLS certs for use in Go tests
Go
4
star
23

must

Go
4
star
24

inferno-opt-skel

Example skeleton /opt project for OS Inferno
Brainfuck
4
star
25

alpine-runit-volume

Docker base image to run microservice with a data volume
Shell
3
star
26

gh-make-labels

Make labels for GitHub repo
Go
3
star
27

inferno-opt-mkfiles

mkfiles to use in OS Inferno projects compatible with /opt
Shell
3
star
28

inferno-contrib-tap

Limbo module: Test Anything Protocol
Brainfuck
3
star
29

vcprompt-fast

Improved and faster vcprompt: tool to get VCS info in shell PS1 prompt
Go
3
star
30

inferno-cjson

OS Inferno driver: fast JSON tokenizer
Brainfuck
3
star
31

perl-Log-Fast

Perl module: Log::Fast - Fast and flexible logger
Perl
3
star
32

perl-JSON-RPC2

Perl module: JSON::RPC2 - Transport-independent implementation of JSON-RPC 2.0
Perl
3
star
33

pqx

Helpers for use with Go Postgres driver github.com/lib/pq
Go
3
star
34

gotmpl

Command line tool for processing template file using Go text/template syntax
Go
3
star
35

flazsh

Fastest ZSH you've ever seen
Shell
2
star
36

userjs-github-asciidoc

UserJS for GitHub: fix Asciidoc rendering
JavaScript
2
star
37

vim-plugin-fixtermkeys

Fix terminal Ctrl Alt Shift modifiers for keys like Tab CR Space BS cursor and others
Vim Script
2
star
38

vaultgnupg

Scripts to link ansible-vault and GnuPG
Shell
2
star
39

getenv

Parse environment variables for Go
Go
2
star
40

deepequal

Go package with improved reflect.DeepEqual
Go
2
star
41

gotest

Helpers for Go tests
Go
2
star
42

structlog-usage-example

Usage example for Go package github.com/powerman/structlog
Go
2
star
43

narada4d

Manage data schema version
Go
2
star
44

appcfg

Get valid application configuration from flags/env/config files/consul/… for Go
Go
2
star
45

perl-Test-Mock-Time

Perl module: Test::Mock::Time - Deterministic time & timers for event loop tests
Perl
2
star
46

grub2-theme-powerman

Grub2 gfxmenu theme "Powerman"
2
star
47

inferno-contrib-retrymount

Automatically retry mount if mount point become unaccessible in OS Inferno
Brainfuck
2
star
48

inferno-contrib-hashtable

Limbo module: polymorphic hash table
Brainfuck
1
star
49

perl-Async-Defer

Perl module: Async::Defer - VM to write and run async code in usual sync-like way
Perl
1
star
50

perl-IO-Stream-HTTP-Persistent

Perl module: IO::Stream::HTTP::Persistent - HTTP persistent connections plugin
Perl
1
star
51

inferno-contrib-regmonitor

Limbo module: monitor registry(4) services
Brainfuck
1
star
52

perl-MojoX-JSONRPC2-HTTP

Perl module: MojoX::JSONRPC2::HTTP - Client for JSON RPC 2.0 over HTTP
Perl
1
star
53

narada-plugin-mojo

Mojolicious webapp for Narada projects
ApacheConf
1
star
54

inferno-contrib-growing

Limbo module: dynamically growing arrays
Brainfuck
1
star
55

narada-base

Base files used to initialize new Narada projects
1
star
56

inferno-contrib-logger

Limbo module: verbose logger
Brainfuck
1
star
57

asciidoc-9man-backend

AsciiDoc backend for generating man pages for OS Inferno and Plan9
1
star
58

sqlxx

General purpose extensions to golang's github.com/jmoiron/sqlx
Go
1
star
59

inferno-contrib-iobuf

Limbo module: buffered read/write
Brainfuck
1
star
60

inferno-contrib-watchdog

Watchdog for running services in OS Inferno
Brainfuck
1
star
61

chanq

Go package provides outgoing queue for channel to use in select case
Go
1
star
62

nginx-config

Nginx boilerplate config
DIGITAL Command Language
1
star
63

perl-IO-Stream-Proxy-SOCKSv5

Perl module: IO::Stream::Proxy::SOCKSv5 - SOCKSv5 proxy plugin for IO::Stream
Perl
1
star
64

constvar

Constant values in global Go variables
Go
1
star
65

urlvalues

Go package for unmarshaling url.Values to struct with strict validation
Go
1
star
66

conv

Convert types for Go
Go
1
star
67

perl-Mojolicious-Plugin-JSONRPC2

Perl module: Mojolicious::Plugin::JSONRPC2 - JSON RPC 2.0 over HTTP
Perl
1
star
68

perl-Inferno-RegMgr

Perl module: Inferno::RegMgr - Keep connection to OS Inferno's registry(4) and it tasks
Perl
1
star
69

mysqlx

Helpers for use with Go MySQL driver github.com/go-sql-driver/mysql
Go
1
star
70

inferno-contrib-register

Keep given service addr/attrs registered in OS Inferno registry
Brainfuck
1
star
71

perl-Crypt-MatrixSSL3

Perl module: Crypt::MatrixSSL3 - Perl extension for SSL and TLS using MatrixSSL.org
Perl
1
star
72

perl-Sub-Throttler

Perl module: Sub::Throttler - Rate limit sync and async function calls
Perl
1
star
73

perl-IO-Stream-Proxy-SOCKSv4

Perl module: IO::Stream::Proxy::SOCKSv4 - SOCKSv4 proxy plugin for IO::Stream
Perl
1
star
74

perl-IO-Stream-Proxy-HTTPS

Perl module: IO::Stream::Proxy::HTTPS - HTTPS proxy plugin for IO::Stream
Perl
1
star
75

go-service-stateless-example

Example how to build and test stateless Go microservice with Docker, Consul and Nginx
Go
1
star