Skip to content

Deploy Docker application on a server with Docker Compose

Notifications You must be signed in to change notification settings

explicit-logic/docker-module-7.6

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Module 7 - Containers with Docker

This repository contains a demo project created as part of my DevOps studies in the TechWorld with Nana – DevOps Bootcamp.

https://www.techworld-with-nana.com/devops-bootcamp

Demo Project: Deploy Docker application on a server with Docker Compose

Technologies used: Docker, Nexus, Node.js, MongoDB, MongoExpress

Project Description:

  • Copy Docker-compose file to remote server
  • Login to private Docker registry on remote server to fetch our app image
  • Start our application container with MongoDB and
  • MongoExpress services using docker compose

Prerequisites

Create Docker repository on Nexus and push an app image to it (complete all the steps):

https://github.com/explicit-logic/docker-module-7.5

Important: On Apple Silicon, add the --platform linux/amd64 flag to the docker build command to ensure compatibility with the remote Linux server

docker build --platform linux/amd64 -t my-app:1.0 .

Note: Replace <DROPLET_IP> and <NEXUS_DROPLET_IP> with your actual IP addresses throughout this guide.


Setup Instructions

1. Create and Access Your Droplet

Create a new DigitalOcean droplet with the following specifications:

  • OS: Ubuntu (latest LTS recommended)
  • RAM: 2GB minimum
  • SSH Key: Attach your public SSH key during creation

Connect to the droplet as root:

ssh root@<DROPLET_IP>

2. Create a Dedicated App User

For security best practice, create a dedicated user for running the application:

adduser app
usermod -aG sudo app

3. Install Docker and Docker Compose

Install Docker Engine and Docker Compose on the server:

Reference: Installing Docker on Ubuntu

# Update package index and upgrade existing packages
apt update
apt upgrade -y

# Install prerequisites
apt install -y ca-certificates curl gnupg lsb-release

# Add Docker's official GPG key
mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set up Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

# Install Docker Compose standalone
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

# Verify installation
docker --version
docker-compose --version

4. Grant Docker Permissions to App User

Add the app user to the docker group to allow it to run Docker commands without sudo:

usermod -aG docker app

Note: The user will need to log out and back in for the group membership to take effect.

5. Configure Docker for Nexus Registry

Configure Docker to allow connections to your insecure Nexus registry:

vim /etc/docker/daemon.json

Add the following configuration:

{
  "insecure-registries": ["<NEXUS_DROPLET_IP>:8083"]
}

Security Note: This configuration allows unencrypted connections to Nexus. For production environments, consider using TLS certificates.

Restart Docker to apply the changes:

systemctl restart docker

Verify Docker is running:

systemctl status docker

6. Configure SSH Access for App User

Set up SSH access for the app user:

# Switch to app user
su - app

# Create .ssh directory with proper permissions
mkdir -p ~/.ssh
chmod 700 ~/.ssh

# Create and edit authorized_keys file
vim ~/.ssh/authorized_keys

Paste your public SSH key into authorized_keys, then set proper permissions:

chmod 600 ~/.ssh/authorized_keys
exit

Verify SSH access works:

ssh app@<DROPLET_IP>

If successful, disconnect and continue from your local machine.

7. Prepare Docker Compose Configuration

On your local machine, modify the docker-compose.yaml file to include your application service.

Add the following service configuration:

  my-app:
    image: <NEXUS_DROPLET_IP>:8083/my-app:1.0
    ports:
      - 3000:3000
    env_file:
      - .env

8. Copy Docker Compose File to Server

Transfer the docker-compose.yaml file to the remote server:

scp docker-compose.yaml app@<DROPLET_IP>:/home/app

9. Create Environment Configuration

SSH into the server as the app user and create the .env file:

ssh app@<DROPLET_IP>
vim .env

Add the following environment variables:

MONGO_DB_HOST=mongodb
MONGO_DB_USERNAME=admin
MONGO_DB_PASSWORD=password

Security Note: In production, use strong passwords and consider using secrets management solutions.

10. Authenticate with Nexus Docker Registry

Log in to your private Nexus Docker registry:

docker login <NEXUS_DROPLET_IP>:8083

When prompted, enter the credentials created in the prerequisite steps:

  • Username: docker
  • Password: pass

You should see a Login Succeeded message upon successful authentication.

11. Deploy the Application

Start all services using Docker Compose:

docker compose up -d

Tip: Use the -d flag to run containers in detached mode (background).

View logs to verify the application is running correctly:

docker compose logs -f

Access your application at http://<DROPLET_IP>:3000

About

Deploy Docker application on a server with Docker Compose

Topics

Resources

Stars

Watchers

Forks