Posted by Ben Watson on 2nd January 2020

If you don’t know what docker is then where have you been? Docker is a tool which allows you to provision containers based on docker images.

Why is this useful and in particular; why is this useful for not just production and stage environments but for local dev environments? It’s because it allows you to write a simple file that essentially extends other docker images and create an easily replicable and fast spawning containerised environment.

So why should we use docker for a local dev environment?

Because getting developers to all agree on a particular set up and getting everybody on the same page is difficult. With the use of Docker, we can eliminate the need to set up a new starts machine by shipping the environment (along with the infrastructure) in a single file which is unique to the particular project.

It encourages developers to take ops in to consideration. Rather than just blaming it on different environments, a developer can perfect it on their machine and easily distribute it to all project stakeholders.

Changes to infrastructure is a walk in the park thanks to Docker. Want to switch PHP version? Require some dodgy code a client demands? No worries. Simply update your project Dockerfile and be content in the knowledge that your changes will only affect the container it creates and leave the rest of the server alone.

The goal.

Each project should have a docker-compose.yml file which tells docker what image to use and how to run it. It should automatically proxy secure https requests from a local virtual hosts to running containers and finally, it should be able to communicate with a central database(s) which s managed by docker.

Other than the initial set up of the Docker Dev Environment, the only command for all of this functionality is docker-compose up. No need to bother looking through a readme for complex install, build, run and deploy instructions.

Okay! I’m sold. How does it work?

For your local dev environment, we’re going to make use of a few awesome libraries provided by the awesome source community!

  • jwilder/nginx-proxy – “Generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.
  • FiloSottile/mkcert – “mkcert is a simple tool for making locally-trusted development certificates. It requires no configuration.”

Creating the virtual host proxy.

The virtual host proxy allows us to associate a virtual host with a docker-compose project simply by attaching labels to the container. No need to edit your hosts file, no restarting an entire VM and witng a few minutes. It just works. This is all done thanks to jwilder/nginx-proxy.

Installing dnsmasq.

DNSMASQ is the only other bit of software that you actually have to install on your host machine. Everything else runs inside of containers.

To install use the following brew commands:

brew install nss and brew install dnsmasq

Now configure your local proxy domain. We use a wildcard for this to make SSL easier in later steps. Replace

with your domain. E.g; local.<your company>.com

sudo echo 'address=/{$PROXY_DOMAIN}/' >> /usr/local/etc/dnsmasq.conf

Now create a DNS resolver by running the following command:

sudo mkdir -p /etc/resolver && echo 'nameserver' | sudo
    tee /etc/resolver/${PROXY_DOMAIN}

Great! All that’s needed now is a system restart and that’s DNS side of things done!

Installing the proxy

So now we just need to install the proxy but before we do this, we need to create a docker network it can run on.

docker network create proxy-network

All we have to do to install and start the proxy is run docker-compose up -d for the following file and we’re done! (Replace $PROXY_DOMAIN)

Congratulations! You now have an automatic reverse proxy and a database any container running on the docker proxy-network can access!

You can test the proxy is working by visiting https://whoami.local.<your company>.com. You should see a message saying I’m <container name>

Let’s get our project running!

So, let’s get a simple php application running. We’ll use the example docker image from the official Docker PHP Image page.

Create your Dockerfile:

And now create a docker-compose file for the project.

And then run docker-compose up -d

That’s it! We’re done. You can now visit your php application in the browser by going to example.local.<your company>.com

Next up setting up SSL

Scroll up