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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • GenAI: From Prompt to Production
  • 5 Ways Docker Can Improve Security in Mobile App Development
  • Mobile Backend With Docker, Kubernetes, and Microservices
  • Container Checkpointing in Kubernetes With a Custom API

Trending

  • Ensuring Configuration Consistency Across Global Data Centers
  • AI-Based Threat Detection in Cloud Security
  • How Trustworthy Is Big Data?
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Running a Mobile App API Locally With Docker and Postman

Running a Mobile App API Locally With Docker and Postman

Learn how to run a mobile app API locally using Docker and test it with Postman for seamless development and debugging in this article.

By 
Swapnil Patil user avatar
Swapnil Patil
·
Mar. 07, 25 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
36.0K Views

Join the DZone community and get the full member experience.

Join For Free

In the development of mobile applications, a well-defined API is crucial for enabling seamless communication between the mobile front end and the backend services. Running this API locally can significantly enhance the development workflow, allowing developers to test and debug their applications without deploying them to a remote server. In this article, we will explore how to run a mobile app API locally using Docker and how to test it effectively with Postman.

Why Use Docker?

Docker provides a lightweight environment for running applications in containers, ensuring consistency across development, testing, and production environments. Using Docker, developers can isolate dependencies, manage versions, and streamline the deployment process.

Setting Up the Project

Before we begin, ensure that you have Docker and Postman installed on your machine.

1. Create a Simple API with Node.js and Express

Let’s create a simple RESTful API using Node.js and Express. We will implement endpoints for a mobile app that manages a list of tasks.

Step 1: Project Structure

Create a new directory for your project:

Shell
 
mkdir mobile-app-api
cd mobile-app-api
mkdir src


Inside the src directory, create the following files:

  • server.js
  • package.json

Step 2: Define the API

package.json:

JSON
 
{
  "name": "mobile-app-api",
  "version": "1.0.0",
  "description": "A simple API for managing tasks",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.17.3"
  }
}


server.js:

JavaScript
 
const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

let tasks = [
  { id: 1, title: 'Task One', completed: false },
  { id: 2, title: 'Task Two', completed: false },
];

// Get all tasks
app.get('/api/tasks', (req, res) => {
  res.json(tasks);
});

// Create a new task
app.post('/api/tasks', (req, res) => {
  const newTask = {
    id: tasks.length + 1,
    title: req.body.title,
    completed: false,
  };
  tasks.push(newTask);
  res.status(201).json(newTask);
});

// Start the server
app.listen(port, () => {
  console.log(`API running at http://localhost:${port}`);
});


Step 3: Create a Dockerfile

In the root directory, create a file named Dockerfile:

Dockerfile
 
# Use the official Node.js image
FROM node:18

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY src/package*.json ./
RUN npm install

# Copy the rest of the application files
COPY src/ .

# Expose the API port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]


Step 4: Create a Docker Compose File

To simplify running the API, create a docker-compose.yml file in the root directory:

YAML
 
version: '3.8'
services:
  api:
    build: .
    ports:
      - "3000:3000"


2. Building and Running the API With Docker

To build and run your API, execute the following commands in your terminal:

Shell
 
# Build the Docker image
docker-compose build

# Run the Docker container
docker-compose up


You should see output indicating that the API is running:

Plain Text
 
API running at http://localhost:3000


3. Testing the API With Postman

Now that your API is running locally, you can test it using Postman.

Step 1: Open Postman

Launch Postman and create a new request.

Step 2: Test the GET Endpoint

  1. Set the request type to "GET."
  2. Enter the URL: http://localhost:3000/api/tasks.
  3. Click "Send." You should see the list of tasks returned in JSON format.

Step 3: Test the POST Endpoint

  1. Set the request type to "POST."
  2. Enter the URL: http://localhost:3000/api/tasks.
  3. In the Body tab, select "raw" and set the format to JSON.
  4. Enter the following JSON to create a new task:
    JSON
     
    {
      "title": "Task Three"
    }
  5. Click "Send." You should see the newly created task in the response.

4. Visualizing the API Architecture

Here’s a simple diagram representing the architecture of our API:

Plain Text
 
                ┌─────────────────────┐
                │     Mobile App      │
                └──────────▲──────────┘
                           │
                ┌──────────┴──────────┐
                │   Postman Client     │
                └──────────▲──────────┘
                           │
                 ┌─────────┴───────────┐
                 │    Docker Container   │
                 │  (Node.js + Express) │
                 └─────────▲───────────┘
                           │
                  ┌────────┴────────┐
                  │      API        │
                  └─────────────────┘


Conclusion

Running a mobile app API locally using Docker allows developers to create a consistent and isolated development environment. Using for containerization and Postman for testing, you can efficiently build, run, and debug your API, leading to a smoother development experience. This setup accelerates development and ensures that the application behaves consistently across various environments.

Next Steps

  • Explore Docker Networking to connect multiple services.
  • Implement a database (e.g., MongoDB or PostgreSQL) for persistent storage.
  • Integrate API documentation tools like Swagger for better API management.
  • Consider deploying the API to a cloud service once it’s production-ready.

You should now be able to streamline your mobile application development workflow, ensuring your API is robust and reliable.

API Docker (software) mobile app

Opinions expressed by DZone contributors are their own.

Related

  • GenAI: From Prompt to Production
  • 5 Ways Docker Can Improve Security in Mobile App Development
  • Mobile Backend With Docker, Kubernetes, and Microservices
  • Container Checkpointing in Kubernetes With a Custom API

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!