Deploy your web app with Docker (Beginner)
Tags: docker, php, lamp, xamp
Published: 2018-09-12 14:50:21

---
title: Deploy your web app with Docker (Beginner)
published: true
description: beginner's guide to deploying your first web app with docker
tags: docker, php, lamp, xamp
---
During my internship at BeCode, I've been able to handle all deployements with [GitHub pages](https://pages.github.com/) or [Heroku](https://www.heroku.com/). And then, last week, came the databases.

Long stories short, we had to develop a basic client register with a mySQL database (so goodbye Heroku). Everything was working as expected locally, but - I'm pretty sure you can see what's coming - the whole thing collapsed when we tried to deploy.

Let me share a post-mortem of what went wrong, before sharing a few tips on how to make your life easier with [Docker](www.docker.com).

The details of the app don't matter, but here's a tiny bit of context:

- We didn't anticipate that deployement would require a large amount of time, so we didn't start until the very last minute, which made the chaos and panic even worse.
- I had fiddled with Docker on a server in my free time, but couldn't make it work

So we chose a free hosting service, moved all the files on their server, and then nothing worked.

What seems to have happened is that the server was not running the same version of PHP we used to develop the website. It could have been avoided if we had done our research earlier, but hey, we learn from our mistakes.

Which brings me to the meat of this post: Deploying with Docker.

The little story above shows that the tricky thing with deploying your site or your app is that the environement provided by your host might differ considerably from your development environment. As a result, you need to anticipate and allocate time for deployment when planning your project.

Now, if you're reading this post and have already heard about Docker, you know where this is going: using Docker can make your life easier by replicating your development environment on your server, quite literally.

I'll let you follow the link if you want to learn what [Docker](https://www.docker.com/) is and how it does what it does. Let me just share how I used it to redeploy our site elsewhere and saved us some tedious work:


1. Choose your host.
I went for [Digital Ocean](https://www.digitalocean.com/), because it was recommended by someone I trust. It's not free though.

2. Install Docker on the server
It turns out Digital Ocean allows you to create VMs with Docker already installed. If you want to do it manually, just find a Ubuntu server and follow [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04) (don't forget to install docker-compose).

3. Import your `docker-compose.yml` into your server
That's the magic part: if you used Docker to set up your developement environment, *you already have this file*. All you need to do is to put it on a repository (GitHub, BitBucket, whatever you like) and clone it on your server.

4. Create the containers
Go to the folder that will contain your app, put the `docker-compose.yml` in it, then:

`docker-compose up`

That's it. Well almost. At this point, you might need to restart the server for things to work. I don't know why, but I had to do it.

Now, all you need to do is clone your app's repository in the right folder on your server, and your app will run *exactly as it did locally* because it will be running in the exact same environment. 

That should save you some time.