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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • A Guide to Container Runtimes
  • Docker vs Kubernetes: Which to Use and When?
  • Docker Bake: A Modern Approach to Container Building
  • Containerization of a Node.js Service

Trending

  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  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
9.4K 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 to Container Runtimes
  • Docker vs Kubernetes: Which to Use and When?
  • Docker Bake: A Modern Approach to Container Building
  • Containerization of a Node.js Service

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!