- Notifications
You must be signed in to change notification settings - Fork0
A simple script to wait for other docker images to be started while using docker-compose
License
videoly/docker-compose-wait
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A small command-line utility to wait for other docker images to be started while using docker-compose.
It permits waiting for:
- a fixed amount of seconds
- until a TCP port is open on a target image
- until a file or directory is present on the local filesystem
This utility should be used in the docker build process and launched before your application starts.
For example, your application "MySuperApp" uses MongoDB, Postgres and MySql (wow!) and you want to be sure that, when it starts, all other systems are available, then simply customize your dockerfile this way:
## Use whatever base imageFROM alpine## Add the wait script to the imageADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.9.0/wait /waitRUN chmod +x /wait## Add your application to the docker imageADD MySuperApp.sh /MySuperApp.sh## Launch the wait tool and then your applicationCMD /wait && /MySuperApp.sh
Done! the image is ready.
Now let's modify the docker-compose.yml file:
version:"3"services:mongo:image:mongo:3.4hostname:mongoports: -"27017:27017"postgres:image:"postgres:9.4"hostname:postgresports: -"5432:5432"mysql:image:"mysql:5.7"hostname:mysqlports: -"3306:3306"mySuperApp:image:"mySuperApp:latest"hostname:mySuperAppenvironment:WAIT_HOSTS:postgres:5432, mysql:3306, mongo:27017
When docker-compose is started (or Kubernetes or docker stack or whatever), your application will be started only when all the pairs host:port in the WAIT_HOSTS variable are available.The WAIT_HOSTS environment variable is not mandatory, if not declared, the script executes without waiting.
If you want to use the script directly in docker-compose.yml instead of the Dockerfile, please note that thecommand:
configuration option is limited to a single command so you should wrap in ash
call. For example:
command: sh -c"/wait && /MySuperApp.sh"
This is discussed furtherhere andhere.
Do note the recommended way of usingwait
is with the shell operator&&
, which implies the requirement of a shell. This introduces a requirement for Docker use where bases images likescratch not offering a shell cannot be used.
Instead the recommendation for base Docker images are ones offering a shell likealpine,debian etc. and if you want to aim forminimalism, evaluate something like:busybox
The behaviour of the wait utility can be configured with the following environment variables:
- WAIT_LOGGER_LEVEL : the output logger level. Valid values are:debug,info,error,off. the default isdebug.
- WAIT_HOSTS: comma-separated list of pairs host:port for which you want to wait.
- WAIT_PATHS: comma-separated list of paths (i.e. files or directories) on the local filesystem for which you want to wait until they exist.
- WAIT_TIMEOUT: max number of seconds to wait for all the hosts/paths to be available before failure. The default is 30 seconds.
- WAIT_HOST_CONNECT_TIMEOUT: The timeout of a single TCP connection to a remote host before attempting a new connection. The default is 5 seconds.
- WAIT_BEFORE: number of seconds to wait (sleep) before start checking for the hosts/paths availability
- WAIT_AFTER: number of seconds to wait (sleep) once all the hosts/paths are available
- WAIT_SLEEP_INTERVAL: number of seconds to sleep between retries. The default is 1 second.
The simplest way of getting thewait executable is to download it from
https://github.com/ufoscout/docker-compose-wait/releases/download/{{VERSION}}/wait
This is a pre-built executable for Linux x64 systems which are the default ones in Docker.In addition, it is built withMUSL for maximum portability.
If you need it for a different architecture, you should clone this repository and build it for your target.
As it has no external dependencies, an being written in the mightyrustprogramming language, the build process is just a simplecargo build --release
(well... of course you need to install the rust compiler before...)
For everything involving cross-compilation, you should take a look atCross.
For example, to build for araspberry pi, everything you have to do is:
- Install the latest stable rust toolchain using rustup
- Correctly configure Docker on your machine
- Open a terminal and type:
cargo install crosscross build --target=armv7-unknown-linux-musleabihf --release
Use your shiny new executable on your raspberry device!
This utility was explicitly written to be used with docker-compose; however, it can be used everywhere since it has no dependencies on docker.
About
A simple script to wait for other docker images to be started while using docker-compose
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Rust98.8%
- Shell1.2%