How to Deploy a Website with Docker: A Step-by-Step Tutorial

Docker packages your website and its dependencies into one portable image, so it runs identically on your laptop and your production server. Here is how to deploy it in four steps.

You will need Docker installed locally, a Linux server (any VPS works), and basic command-line comfort.

Article illustration

1. Write a Dockerfile

Create a Dockerfile in your project root. Start from a base image like nginx:alpine for static files or node:20-alpine for an app. Use COPY to add your code, RUN to install dependencies, EXPOSE for the port, and CMD for the start command.

2. Build and Test Locally

Run docker build -t my-site . to create the image, then docker run -p 8080:80 my-site. Open localhost:8080. If it loads, the image works.

3. Ship the Image to Your Server

  • Push it to a registry with docker tag and docker push to Docker Hub.
  • SSH into your server and run docker pull yourname/my-site.
  • Or simply copy the project and build directly on the server.

4. Run It in Production

Start the container with docker run -d --restart unless-stopped -p 80:80 yourname/my-site. The -d flag detaches it, and --restart keeps your site alive after reboots. For multi-container setups, use Docker Compose behind a reverse proxy like Nginx or Traefik to handle HTTPS.

Conclusion

Docker reduces deployment to three repeatable commands: build, push, run. Once the image works locally, your server becomes predictable — and rolling back is just running an older tag.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *