Skip to content

Project with Dockerfile

A Dockerfile is a text file that contains all the commands required to build an image for your project. Images are used to run containers with Docker (or other container tools). Containers might sound complex if you're unfamiliar with them, but they're simpler than you might think. Try a few examples, and you'll see how they work.

How to Generate a Dockerfile Automatically

If your project doesn't have a Dockerfile, you can generate one with a tool called Nixpacks. Nixpacks doesn't work for all projects, but for most non-complex ones, it can generate a Dockerfile.

Let's see how it works:

  1. Install Nixpacks on your development machine by following the steps at nixpacks.com/docs/install.
  2. Change to your project's directory and generate a Dockerfile with Nixpacks: cd folder_with_projects nixpacks build . -o .
  3. Check if Nixpacks generated a Dockerfile in folder_with_projects/.nixpacks. If there isn't one, review the output from nixpacks build to find out why a Dockerfile couldn't be generated.
  4. If a Dockerfile exists in folder_with_projects/.nixpacks, copy it to the root folder of your project: cp ./nixpacks/Dockerfile Dockerfile

If everything worked correctly, you now have a Dockerfile for your project and can deploy it with Docker by building an image and running a container. If Nixpacks was unable to generate a Dockerfile, you can create one manually based on a template for your runtime and frameworks (if applicable). To find a template, search online for something like "Node.js Dockerfile," "Next.js Dockerfile," "PHP Dockerfile," etc.

How to Deploy a Project with a Dockerfile

We assume you have a Dockerfile in the root folder of your project (if not, follow the advice in "How to Generate a Dockerfile Automatically").

Prerequisites

  • A server with a public IP address running Ubuntu 22.04 and SSH access. You can use any cloud provider, but we recommend the ones listed in the "Getting a Cloud Server, Setting Backups" chapter. The IP address used in this example is 151.115.51.131—be sure to replace it with your server's IP.
  • A domain or subdomain with an A record pointing to your server's IPv4 address. If you're unsure how to add an A record, refer to the "Where to Buy Domains and How to Connect Them to Servers" chapter. In this example, we'll use demo-docker.turbocloud.dev.

Steps to Deploy

  • Upload the website folder (replace "folder_with_website" and "151.115.51.131" with your actual folder and server IP):
cd folder_with_website
scp -r . root@151.115.51.131:project
  • SSH into the server (replace "151.115.51.131" with your server IP):
ssh root@151.115.51.131
  • Install Caddy (a reverse proxy that forwards requests to containers based on domain names):
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
  • Install Docker:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • Build and start a Docker container using the Dockerfile (replace project_port with the port you defined in the Dockerfile):
sudo docker image rm project
sudo docker build project -t project
sudo docker container rm project --force

sudo docker run -it -d --restart unless-stopped -p 5000:project_port project
  • Create a Caddy configuration file to reverse proxy requests to your Docker container:
sudo rm /etc/caddy/Caddyfile
sudo echo -e "demo-docker.turbocloud.dev {\nreverse_proxy * localhost:5000\n}" >> /etc/caddy/Caddyfile
sudo caddy reload -c /etc/caddy/Caddyfile
  • Verify your website. After 20–30 seconds, open your domain (e.g., https://demo-docker.turbocloud.dev) to make sure it’s working.