DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Guide for Deploying .NET 10 Applications Using Docker's New Workflow
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues
  • Docker Offload: One of the Best Features for AI Workloads
  • A Guide to Container Runtimes

Trending

  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Pragmatica Aether: Let Java Be Java
  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Running Docker Containers in HashiCorp Nomad: A Beginner’s Guide

Running Docker Containers in HashiCorp Nomad: A Beginner’s Guide

Learn to seamlessly deploy and orchestrate Docker containers with HashiCorp Nomad using simple configurations and tools.

By 
Nilesh Jain user avatar
Nilesh Jain
·
Dec. 04, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
12.8K Views

Join the DZone community and get the full member experience.

Join For Free

Nomad, a flexible and lightweight orchestrator developed by HashiCorp, is an excellent tool for managing containerized applications like Docker. This guide walks you through running Docker containers with Nomad, designed specifically for beginners. Whether you're deploying a simple web server or experimenting with microservices, this guide will provide you with the foundation to get started.

What Is Nomad?

Nomad is a simple, flexible, and scalable workload orchestrator that supports running containerized and non-containerized applications. Though it is not as popular as Kubernetes, which currently dominates the container orchestration space, Nomad has its advantages: ease of use, lightweight architecture, and support for mixed workloads.

Prerequisites

Before running Docker containers in Nomad, ensure the following:

  1. Nomad Installed: Download and install Nomad from HashiCorp's website.
  2. Docker Installed: Install Docker and confirm it is running with:
    Plain Text
     
    docker --version


  3. Nomad Agent Running: Start a Nomad agent in development mode for simplicity. The -dev flag starts a local Nomad cluster in development mode:
    Plain Text
     
    nomad agent -dev

Step 1: Write a Nomad Job File

Nomad uses job specification files written in HashiCorp Configuration Language (HCL) to define the jobs it manages. Below is a basic example of a Nomad job file (docker.nomad) to run an NGINX web server.

Example: Simple Nomad Job File

YAML
 
job "nginx-job" {
  datacenters = ["dc1"]

  group "web-group" {
    count = 1

    task "nginx" {
      driver = "docker"

      config {
        image = "nginx:latest"  # Docker image to run
        ports = ["http"]
      }

      resources {
        network {
          port "http" {
            static = 8080  # Expose container on host's port 8080
          }
        }
      }
    }
  }
}



Explanation of the Job File

  • job: Defines the job name and datacenter to deploy to.
  • group: Groups related tasks (containers) together.
  • task: Specifies a single container to run, including the driver (docker) and configuration.
  • config: Contains Docker-specific settings like the image to use and ports to expose.
  • resources: Defines resource limits and networking settings for the container.

Step 2: Run the Nomad Job

Submit the job file to the Nomad cluster using the nomad run command:

YAML
 
nomad run docker.nomad


This will schedule the NGINX container on the Nomad agent. If successful, you’ll see output indicating that the job has been successfully deployed.

Step 3: Verify the Job

Check the status of the job using:

YAML
 
nomad status nginx-job


You should see the job details, including its allocation ID and deployment status.

Step 4: Access the Running Container

  1. Find the IP address of the host running the container. If you're running locally, it’s likely 127.0.0.1.
  2. Open a web browser and visit http://localhost:8080. You should see the default NGINX welcome page.

Step 5: Stop the Job

To stop the container, use the nomad stop command:

YAML
 
nomad stop nginx-job


This will cleanly shut down the container managed by Nomad.

Advanced Examples

1. Add Environment Variables

You can pass environment variables to your Docker container using the env block in the task section:

YAML
 
task "nginx" {
  driver = "docker"

  config {
    image = "nginx:latest"
    ports = ["http"]
  }

  env {
    APP_ENV = "production"
  }
}


2. Mount Volumes

To mount a host directory into the container, use the volumes option in the config block:

YAML
 
task "nginx" {
  driver = "docker"

  config {
    image = "nginx:latest"
    ports = ["http"]
    volumes = ["/host/path:/container/path"]
  }
}


3. Scale Containers

To scale the container to multiple instances, modify the count parameter in the group section:

YAML
 
group "web-group" {
  count = 3  # Run 3 instances of the container
}


Nomad will distribute the instances across available nodes in your cluster.

Tips for Beginners

  1. Test in Development Mode: Start with the nomad agent -dev command for quick local testing before deploying to a production cluster.
  2. Leverage Nomad's Web UI: Use the Nomad UI (enabled by default in dev mode) to monitor jobs and allocations visually. Access it at http://localhost:4646.
  3. Use Logs for Debugging: Check logs for troubleshooting with:
    Plain Text
     
    nomad logs <allocation-id>

Conclusion

Running Docker containers with HashiCorp Nomad is a straightforward and powerful way to orchestrate workloads. By defining jobs in simple HCL files, you can easily deploy, monitor, and scale containers across your infrastructure. Whether you're just starting or looking for an alternative to Kubernetes, Nomad’s simplicity and flexibility make it an excellent choice for managing containerized applications.

Docker (software) Container

Opinions expressed by DZone contributors are their own.

Related

  • A Guide for Deploying .NET 10 Applications Using Docker's New Workflow
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues
  • Docker Offload: One of the Best Features for AI Workloads
  • A Guide to Container Runtimes

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook