Developing and testing mobile applications requires a reliable backend API. Running an API locally using Docker ensures consistency across different environments, making development and debugging easier. In this guide, we will cover how to set up and run an API locally using Docker and test it with Postman.

Prerequisites

Before we begin, ensure you have the following installed on your system:

  1. Docker – Install it from Docker’s official website.
  2. Postman – Download and install from Postman’s official website.
  3. Node.js and Express (Optional) – If you’re setting up a simple API for testing.

Setting Up a Sample API

Before containerizing with Docker, let’s create a basic API using Node.js and Express. If you already have an API, you can skip this step.

  1. Initialize a Node.js project:
    mkdir mobile-api && cd mobile-api
    npm init -y
  2. Install Express:
    npm install express
  3. Create an index.js file and add the following code:
    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.use(express.json());
    
    app.get('/', (req, res) => {
        res.send('Hello, World! API is running.');
    });
    
    app.get('/users', (req, res) => {
        res.json([{ id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Doe' }]);
    });
    
    app.listen(port, () => {
        console.log(`API running on http://localhost:${port}`);
    });
  4. Test the API Locally:
    node index.js

    Open http://localhost:3000 in a browser or use Postman to test the /users endpoint.

Containerizing the API with Docker

Now, let’s containerize the API so that it runs in an isolated environment.

  1. Create a Dockerfile in the project directory:
    FROM node:18
    WORKDIR /app
    COPY package.json .
    RUN npm install
    COPY . .
    CMD ["node", "index.js"]
    EXPOSE 3000
  2. Create a .dockerignore file:
    node_modules
    npm-debug.log
  3. Build the Docker image:
    docker build -t mobile-api .
  4. Run the container:
    docker run -p 3000:3000 mobile-api

    The API should now be running inside a Docker container at http://localhost:3000.

Testing the API with Postman

Once your API is running locally in Docker, you can test it using Postman.

  1. Open Postman and create a new request.
  2. Enter the API endpoint: http://localhost:3000/users
  3. Select GET and click Send.
  4. You should see the response:
    [
        { "id": 1, "name": "John Doe" },
        { "id": 2, "name": "Jane Doe" }
    ]
  5. Testing a POST Request:
    • Change the request type to POST.
    • Enter the endpoint: http://localhost:3000/users
    • Go to the Body tab, select raw, and choose JSON.
    • Enter the following JSON data:
      {
          "id": 3,
          "name": "Alice"
      }
    • Click Send to test.

Managing Containers with Docker Compose

For more complex APIs, Docker Compose can be useful to manage multiple containers. Let’s set it up.

  1. Create a docker-compose.yml file:
    version: '3'
    services:
      api:
        build: .
        ports:
          - "3000:3000"
  2. Run the API using Docker Compose:
    docker-compose up
  3. To stop the API:
    docker-compose down

Troubleshooting Common Issues

1. Docker Port Binding Issue

  • If the port 3000 is already in use, try changing it in the Docker run command:
    docker run -p 4000:3000 mobile-api

    Then access http://localhost:4000.

2. API Not Running in the Container

  • Ensure all dependencies are installed inside the container:
    docker exec -it <container_id> sh
    npm install

3. Postman Connection Issues

  • Ensure the API is running and accessible on the correct port.
  • Disable VPNs or proxies that may interfere with localhost requests.

Conclusion

Running a mobile app API locally using Docker simplifies development, ensures consistency across environments, and makes testing easier. By following the steps outlined in this guide, you can:

  1. Set up a basic API using Node.js and Express.
  2. Containerize the API with Docker for better environment isolation.
  3. Test API endpoints efficiently using Postman.
  4. Use Docker Compose for managing multiple services.

This setup provides a strong foundation for local API development, making it easier to test and debug mobile applications before deployment. By leveraging Docker, developers can ensure that their API behaves consistently across different systems, ultimately improving the reliability and scalability of their applications.