Question

How Do I Run a Basic PHP Application with Docker on a DigitalOcean Droplet?

Ok maybe this is a basic question, but I’m new to the whole cloud world and Docker. I am just getting started with PHP as well.

What I want is to run a simple PHP application on my DigitalOcean Droplet using Docker. I know that I can have PHP installed and not use Docker, but I want to learn Docker and I’ve heard Docker makes it easier to set up and manage environments, but I’m not sure where to start.

Can someone guide me through how to create a basic Docker container to run a PHP application? Do I need a Dockerfile, and how do I get it running on my Droplet?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 16, 2024

Hey there! 👋

Welcome to the world of Docker! Running a PHP application with Docker on your DigitalOcean Droplet is a great way to manage environments efficiently.

I will try to walk you through this step by step! Let me know if anything is not clear!

Option 1: Running PHP with Docker on a Droplet

First, you have to setup Docker on your Droplet. You can do that using the 1-Click Marketplace image:

https://marketplace.digitalocean.com/apps/docker

Or you can follow the steps here:

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04

Or you can follow these steps here:

  1. You can do this by running:

    sudo apt update
    sudo apt install docker.io
    sudo systemctl start docker
    sudo systemctl enable docker
    
  2. Once you have Docker installed, you can then create a Dockerfile for your PHP app as you mentioned in your question. The Dockerfile is like a recipe for building your container. Here’s a simple example to get you started:

    # Use the official PHP image
    FROM php:7.4-apache
    
    # Copy your PHP files into the container
    COPY ./your-php-app /var/www/html/
    
    # Expose the port that Apache is using
    EXPOSE 80
    
  3. After creating your Dockerfile, run these commands in your Droplet’s terminal:

    docker build -t my-php-app .
    docker run -d -p 80:80 my-php-app
    

    This will pull the PHP image, build your container, and expose it on port 80.

  4. Now your PHP app should be accessible by visiting your Droplet’s IP address in your browser.

You would also want to install an SSL certificate to secure your site:

https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu


Option 2: Using DigitalOcean App Platform (Easier)

If you’re looking for a simpler solution, DigitalOcean’s App Platform is a great alternative. You don’t need to manage servers, and it handles scaling, updates, SSL certificates and backups for you automatically!

Basically, you get the following out fo the box:

  • No server management: No need to worry about maintaining or securing the underlying server.
  • Auto-scaling: Your app can automatically scale as traffic increases.
  • Built-in SSL: Secure your site with a free SSL certificate.
  • Automatic deployments: Simply push your code to GitHub, and it’ll deploy automatically.

You can even use the native PHP buildpack in the App Platform without needing a Dockerfile! But, if you want to, you can still use a Dockerfile in App Platform for more control, just like on your Droplet.

Here’s how to deploy your PHP app on App Platform:

  1. Go to the App Platform and create a new app.
  2. Connect your GitHub repository or upload your PHP code.
  3. Select PHP as the runtime or add your own Dockerfile for more control.
  4. Deploy! 🚀

Check out more about using App Platform with PHP here.


So, if you want to have more control over your environment, Docker on a Droplet is a solid choice. But, if you’re looking for an easier, fully-managed setup, App Platform is the way to go!

Hope this helps, and happy coding! Let me know if you need any further help! 😄

If you’re getting started with Docker, check out this free Docker ebook here: Docker eBook – it’s packed with simple, practical examples to help you get up and running in no time on DigitalOcean.

- Bobby

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.