Deploy your web app with Docker (Beginner)

quangle profile image Quang Son Le Updated on ・3 min read

During my internship at BeCode, I've been able to handle all deployements with GitHub pages or Heroku. 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.

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 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, 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 (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.


Editor guide
seanwcom profile image

I'm not sure how different distro's handle the Docker installation, but here in my office we use RHEL7. When installing Docker, the daemon doesn't start automatically, we have to manually enable and start it. This may explain why you think you need to reboot. It may just be as simple as starting the daemon instead. For me on RHEL7, I just run this:

sudo systemctl start docker
sudo systemctl enable docker

Then I'm set (well there's much more we do, but in this context that's it). But again, I'm not familiar with how other distro's handle it, so some research may be needed for your distro.