Complete Nginx + SSH Server Setup with Docker and Docker Compose on Ubuntu Image

Umar Farooque Khan
4 min read4 days ago

--

In this tutorial, we will set up a Docker container with Nginx serving static files from an html folder and SSH access using Docker Compose. This process will help you create an isolated environment for serving web content and allowing SSH connections to manage the container.

Prerequisites:

  • Docker installed on your machine.
  • Docker Compose installed.
  • Basic understanding of Docker, Nginx, and SSH.

Step-by-Step Tutorial:

Step 1: Create Project Folder Structure

Create a new folder for your project. This folder will contain:

  • Dockerfile
  • docker-compose.yml
  • nginx.conf (Nginx configuration file)
  • html/ (HTML files you want to serve)

Example folder structure:

nginx-setup/
├── Dockerfile
├── docker-compose.yml
├── nginx.conf
└── html/
└── index.html

Step 2: Create the Dockerfile

The Dockerfile will define how to set up the Nginx and OpenSSH server in the container. Create a file named Dockerfile with the following content:

# Use the official Ubuntu base imageFROM ubuntu:latest# Set the maintainer label
LABEL maintainer="your_name@domain.com"
# Install Nginx and OpenSSH server
RUN apt-get update && \
apt-get install -y nginx openssh-server && \
apt-get clean
# Create SSH directory and set root password
RUN mkdir /var/run/sshd && \
echo 'root:password' | chpasswd
# Expose necessary ports: HTTP (80) and SSH (22)
EXPOSE 80 22
# Copy custom Nginx configuration file into the container
COPY ./nginx.conf /etc/nginx/nginx.conf
# Copy HTML files (index.html, etc.) into the container
COPY ./html /usr/share/nginx/html
# Run Nginx and SSH service in the foreground (necessary for Docker containers)
CMD service ssh start && nginx -g 'daemon off;'

Step 3: Create the nginx.conf

The nginx.conf file will configure Nginx to serve files from the /usr/share/nginx/html directory and handle incoming HTTP requests. Here’s an example nginx.conf:

# /etc/nginx/nginx.conf# Main context
user www-data;
worker_processes auto;
# The error log settings
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# The events context
events {
worker_connections 1024;
}
# The HTTP context
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Enable Gzip compression
gzip on;
# Server block inside the HTTP context
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html; # The directory for serving files
index index.html index.htm;
location / {
try_files $uri $uri/ =404; # Handles file requests, showing 404 if not found
}
}
}

Step 4: Create HTML Files

Inside your html/ directory, place an index.html file (or other HTML files) that you want to serve. For example:

<!-- html/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to Nginx</title>
</head>
<body>
<h1>Hello from Nginx running in Docker!</h1>
</body>
</html>

Step 5: Create the docker-compose.yml File

Docker Compose allows you to define and run multi-container Docker applications. We will define the Nginx and SSH server in this file. Here’s an example docker-compose.yml:

version: '3.8'services:
nginx-ssh:
build: .
container_name: nginx-ssh-container
ports:
- "8080:80" # Expose HTTP port (Nginx)
- "2222:22" # Expose SSH port
volumes:
- ./html:/usr/share/nginx/html # Mount local html directory to container
- ./nginx.conf:/etc/nginx/nginx.conf # Mount Nginx configuration file
networks:
- nginx-ssh-network
restart: unless-stopped
networks:
nginx-ssh-network:
driver: bridge
  • build: . tells Docker Compose to build the image from the Dockerfile in the current directory.
  • ports maps the container ports to the host ports (HTTP on 8080 and SSH on 2222).
  • volumes mounts the local html/ directory and nginx.conf into the container.

Step 6: Build and Start the Container

In the terminal, navigate to the project directory (where your docker-compose.yml and Dockerfile are located) and run the following command:

docker-compose up --build -d
  • --build: Forces Docker to rebuild the images from the Dockerfile.
  • -d: Runs the container in detached mode (in the background).

Step 7: Access the Server

  1. Access Nginx in the browser:
  2. Open a browser and go to:
http://localhost:8080
  1. You should see the content from index.html, like:
Hello from Nginx running in Docker!
  1. SSH into the Docker container:
  2. To access the container via SSH, use the following command:
ssh root@localhost -p 2222
  1. The default password is password (as set in the Dockerfile).

Step 8: Check Logs and Troubleshoot

If there are any issues, you can check the logs with:

docker-compose logs nginx-ssh

This will show logs from the Nginx and SSH services, which can help identify errors.

Conclusion

By following this tutorial, you’ve successfully created a Docker container running both Nginx and OpenSSH. The container serves static files from an html folder using Nginx and provides SSH access for container management. Docker Compose makes it easy to manage and configure these services.

Recap of Steps:

  1. Create a project directory with Dockerfile, docker-compose.yml, nginx.conf, and html/.
  2. Set up the Dockerfile to install Nginx and OpenSSH.
  3. Configure Nginx with the nginx.conf file.
  4. Use Docker Compose to build and manage the container.
  5. Access the server via HTTP on port 8080 and SSH on port 2222.

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

Responses (1)