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?
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!
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!
First, you have to setup Docker on your Droplet. You can do that using the 1-Click Marketplace image:
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:
You can do this by running:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
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
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.
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
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:
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:
Dockerfile
for more control.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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.