Docker Community Forums

Share and learn in the Docker community.

Best practices for using docker compose with images that need additional setup after launch?

build
docker

(PC) #1

I am hitting a situation with Docker compose with containers that require setup after the container has started, but before other linked/dependent containers should be started. Let’s say I have an application that comprises of some webapp and a database. I have a Dockerfile that creates the webapp and the database installation (cassandra). The webapp requires a schema to be added to the database, but this can only happen after the database container has been started (we do this either by docker exec or connecting a db client after the db has started).

If I create a docker compose file with the webapp and database builds and run it, of course the database will have no schema and the webapp will crash because what it is expecting from the database isn’t there yet.

(In reality, we use several containers that require some post-build setup, such as apache storm (topologies) and elasticsearch (search indices) that cannot be done inside the Dockerfile.)

Right now rather than using docker compose, we’re using shell scripts to build, start and do post-setup on the containers and start things in the right order.

Anyway, maybe I’m missing something - is there a way to get this to work in docker compose?

If not, what there seems to be a need for, either in Compose or the Dockerfile itself is a way to specify for something (a script, whatever) to run after the main container process starts that can do additional setup (either run only the first time a container starts or run every time and let the script determine if it needs to do something) - before dependent containers are started.


(Mackenzie Burnett) #2

You should check out Kelsey Hightower’s recent article on this - the gist is that instead attempting to serialize startup when writing applications, you add retry logic that allows each component to handle its own life cycle.