I’ve been following the Docker movement for quite some time now, and have seen examples of Docker-Compose passing along, so I thought, why not try and setup a nice development environment where I would develop an application that uses Docker-Compose.
For all you tl;dr’s, perform:
git clone --recursive https://github.com/dennisdegreef/blog-docker-compose-infrastructure cd blog-docker-compose-infrastructure vagrant up
DISCLAIMER: This example is just to setup a docker-compose setup for development, and is currently using Symfony’s build-in webserver, which isn’t recommended for production! This is also just a try-out.
For this example, I am creating a simple Symfony application, which needs Redis instance.
First, we need a vagrant plugin to be able to use docker-compose as a provisioner
# vagrant plugin install vagrant-docker-compose
Next, we need to set up a Vagrantfile inside our infrastructure repository, and configure it using the docker provisioner. The provisioner itself will install Docker inside the vagrantbox and build the base image, on which we ‘base’ our own Dockerfile
# Vagrantfile config.vm.provision :docker do |d| d.build_image "/vagrant/docker/base", args: "-t link0/base" end
Now, we want to create an docker-compose.yml file to provide the structure and requirements of our application. This setup explains docker-compose to build the ‘frontend’ image, start it with port 80 exposed, and the current directory linked as /srv
# docker-compose.yml frontend: build: link0/example-frontend ports: - "80:8000" volumes: - .:/srv links: - redis redis: image: redis
Now that we have the docker-compose.yml, we can instruct Vagrant to use that structure upon provisioning.
config.vm.provision :docker_compose, yml: "/vagrant/docker-compose.yml", run: "always"
But before we can run `vagrant up`, we need to add our frontend application into the infrastructure project. I like to use git submodules for this.
git submodule add firstname.lastname@example.org:dennisdegreef/blog-docker-compose-frontend.git frontend
Now we can try it out, see if it works
It still breaks with the following error message
Building frontend... Cannot locate specified Dockerfile: Dockerfile
This is because our frontend application has no Dockerfile to describe how it should be running. Let’s add one.
# blog-docker-compose-frontend/Dockerfile FROM link0/base:latest MAINTAINER Dennis de Greef <email@example.com> COPY start.sh /start.sh CMD [ "/start.sh" ]
#!/usr/bin/env bash # blog-docker-compose-frontend/start.sh cd /srv/frontend; /srv/frontend/composer.phar install && \ /srv/frontend/app/console server:run 0.0.0.0:8000
And when you now run
And point your browser to http://192.168.42.10/ You should be able to see ‘Hello Docker-Compose’.
You can check (tail) the logs using the following command
vagrant ssh -c 'docker logs -f vagrant_frontend_1'
If some things are unclear, please let me know in the comments.