Docker Examples
Crons in Alpine
Nginx
Hosting a Static Website with Nginx
Hosting a static website with Docker and Nginx is straightforward. You can use an official Nginx Docker image and mount your website files into the container.
docker run \
-d \
-p 8080:80 \
-v /path/to/your/website:/usr/share/nginx/html:ro \
nginx:latest
This command starts a new container from the nginx:latest image, maps port 8080 on the host to port 80 in the container, and mounts your static website files into the correct location in the container.
WordPress
Hosting a Worpress Site
Docker makes it easy to host a WordPress site, as Docker images are available for both WordPress and MySQL (which WordPress requires). With Docker Compose, you can define both services in a single file.
This compose.yml
file defines a db service (running MySQL) and a wordpress service. It uses a volume to persist the MySQL data.
volumes:
wp_db_data:
services:
db:
image: mysql:5.7
volumes:
- wp_db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
Request Tracker
name: rt5-app
volumes:
rt5-db-data:
services:
rt5-db:
image: mariadb:11.4.2
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: rt
MYSQL_USER: rt_user
MYSQL_PASSWORD: password
volumes:
- rt5-db-data:/var/lib/mysql
ports:
- "3306:3306"
rt5-app:
build:
context: .
target: alpine-and-nginx-base
depends_on:
- rt5-db
ports:
- "8080:80"
environment:
DB_HOST: rt5-db
DB_PORT: 3306
DB_USER: rt_user
DB_PASSWORD: password
DB_NAME: rt
DBA_USERNAME: root
links:
- rt5-db
Node
Hosting a NodeJS App
Hosting a Node.js app with Docker involves creating a custom Docker image that includes your app. Here's a simple Dockerfile for a Node.js app:
# Dockerfile
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
This Dockerfile creates a new image based on the node:14 image, copies your app into the image, installs your app's dependencies, and specifies how to start your app.
Docker's flexibility allows it to be used in many other scenarios beyond the ones we've covered here. By understanding the principles and techniques we've discussed, you can adapt Docker to your specific web hosting needs. In the final section, we'll provide some additional resources to help you further your understanding and usage of Docker in web hosting.