Using Docker to Simulate Production Environments for Mobile App Testing

Introduction Testing mobile apps in a production-like environment is crucial to detect issues before deployment. However, setting up such environments manually can be complex and time-consuming. Docker simplifies this process by enabling developers to create isolated, reproducible, and scalable test environments that mimic real-world production setups. In this article, we’ll explore how Docker can be used to simulate a production environment for mobile app testing, including setting up backend APIs, databases, and third-party services in containers. Why Use Docker for Mobile App Testing? ✅ Consistency – Ensure uniform environments across dev, test, and production. ✅ Scalability – Simulate different load conditions with multiple containers. ✅ Dependency Management – Test apps with real backend APIs, databases, and caching systems. ✅ Automated Testing – Integrate with CI/CD pipelines for seamless testing. ✅ Faster Setup – Spin up complex environments in seconds with Docker Compose. Setting Up a Production-Like Environment with Docker A typical mobile app relies on: A backend API (Node.js, Python, etc.) A database (PostgreSQL, MySQL) A caching layer (Redis) An authentication service (OAuth, Firebase) We will containerize these components and run them locally using Docker Compose for testing. Step 1: Install Docker Download and install Docker Desktop from Docker’s official website. Verify installation: docker --version Step 2: Create a Sample Backend API (Node.js) We’ll create a simple Express.js API that connects to a PostgreSQL database and uses Redis for caching. Project Structure mobile-app-test-env/ │── backend/ │ ├── server.js │ ├── package.json │ ├── Dockerfile │── docker-compose.yml server.js (Backend API) const express = require("express"); const redis = require("redis"); const { Pool } = require("pg"); const app = express(); const port = process.env.PORT || 3000; // PostgreSQL Database Connection const pool = new Pool({ user: "user", host: "db", database: "testdb", password: "password", port: 5432, }); // Redis Cache Connection const redisClient = redis.createClient({ host: "redis", port: 6379 }); app.get("/", async (req, res) => { try { const { rows } = await pool.query("SELECT NOW()"); redisClient.set("lastRequest", new Date().toISOString()); res.json({ message: "Production-like API Running!", dbTime: rows[0] }); } catch (error) { res.status(500).json({ error: error.message }); } }); app.listen(port, () => { console.log(`Server running on port ${port}`); }); Step 3: Create a Dockerfile for the API # Use Node.js base image FROM node:14 # Set working directory WORKDIR /app # Copy package files and install dependencies COPY package.json ./ RUN npm install # Copy app source code COPY . . # Expose the API port EXPOSE 3000 # Start the API CMD ["node", "server.js"] Step 4: Define the Complete Environment in Docker Compose Create a docker-compose.yml file to orchestrate the backend API, PostgreSQL database, and Redis cache. version: "3.8" services: backend: build: ./backend ports: - "3000:3000" depends_on: - db - redis environment: DATABASE_URL: "postgres://user:password@db:5432/testdb" db: image: postgres:latest environment: POSTGRES_USER: user POSTGRES_PASSWORD: password POSTGRES_DB: testdb ports: - "5432:5432" redis: image: redis:latest ports: - "6379:6379" Step 5: Run the Simulated Production Environment Start all services with Docker Compose: docker-compose up --build Now, your backend API, database, and cache are running in isolated containers, replicating a production environment locally. Testing Mobile App Integration with Postman Once your backend is running in Docker, you can: ✅ Use Postman to send requests to http://localhost:3000. ✅ Simulate network latency by adding tc (traffic control) rules. ✅ Perform load testing using tools like k6 or JMeter. Example Postman request: GET http://localhost:3000 Expected response: { "message": "Production-like API Running!", "dbTime": "2025-02-22T14:45:00.123Z" } Docker Architecture for Mobile App Testing ┌────────────────────────┐ │ Mobile App (iOS) │ └─────────▲──────────────┘ │ API Calls ┌─────────▼──────────────┐ │ Dockerized Backend API │ │(Node.js in a container)│ └─────────▲──────────────┘ │ ┌─────────▼──────────────┐ │ PostgreSQL (Database) │ ├────────────────────────┤ │ Redis (Cache) │ └────────────────────────┘ Real-World Examples of Docker in Mobile Testing

Feb 23, 2025 - 10:38
 0
Using Docker to Simulate Production Environments for Mobile App Testing

Introduction

Testing mobile apps in a production-like environment is crucial to detect issues before deployment. However, setting up such environments manually can be complex and time-consuming. Docker simplifies this process by enabling developers to create isolated, reproducible, and scalable test environments that mimic real-world production setups.

In this article, we’ll explore how Docker can be used to simulate a production environment for mobile app testing, including setting up backend APIs, databases, and third-party services in containers.

Why Use Docker for Mobile App Testing?

✅ Consistency – Ensure uniform environments across dev, test, and production.
✅ Scalability – Simulate different load conditions with multiple containers.
✅ Dependency Management – Test apps with real backend APIs, databases, and caching systems.
✅ Automated Testing – Integrate with CI/CD pipelines for seamless testing.
✅ Faster Setup – Spin up complex environments in seconds with Docker Compose.

Setting Up a Production-Like Environment with Docker

A typical mobile app relies on:

  • A backend API (Node.js, Python, etc.)
  • A database (PostgreSQL, MySQL)
  • A caching layer (Redis)
  • An authentication service (OAuth, Firebase)

We will containerize these components and run them locally using Docker Compose for testing.

Step 1: Install Docker

Download and install Docker Desktop from Docker’s official website.

Verify installation:

docker --version

Step 2: Create a Sample Backend API (Node.js)

We’ll create a simple Express.js API that connects to a PostgreSQL database and uses Redis for caching.

Project Structure

mobile-app-test-env/
│── backend/
│   ├── server.js
│   ├── package.json
│   ├── Dockerfile
│── docker-compose.yml

server.js (Backend API)

const express = require("express");
const redis = require("redis");
const { Pool } = require("pg");

const app = express();
const port = process.env.PORT || 3000;

// PostgreSQL Database Connection
const pool = new Pool({
    user: "user",
    host: "db",
    database: "testdb",
    password: "password",
    port: 5432,
});

// Redis Cache Connection
const redisClient = redis.createClient({ host: "redis", port: 6379 });

app.get("/", async (req, res) => {
    try {
        const { rows } = await pool.query("SELECT NOW()");
        redisClient.set("lastRequest", new Date().toISOString());
        res.json({ message: "Production-like API Running!", dbTime: rows[0] });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

Step 3: Create a Dockerfile for the API

# Use Node.js base image
FROM node:14

# Set working directory
WORKDIR /app

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

# Copy app source code
COPY . .

# Expose the API port
EXPOSE 3000

# Start the API
CMD ["node", "server.js"]

Step 4: Define the Complete Environment in Docker Compose

Create a docker-compose.yml file to orchestrate the backend API, PostgreSQL database, and Redis cache.

version: "3.8"

services:
  backend:
    build: ./backend
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
    environment:
      DATABASE_URL: "postgres://user:password@db:5432/testdb"

  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: testdb
    ports:
      - "5432:5432"

  redis:
    image: redis:latest
    ports:
      - "6379:6379"

Step 5: Run the Simulated Production Environment

Start all services with Docker Compose:

docker-compose up --build

Now, your backend API, database, and cache are running in isolated containers, replicating a production environment locally.

Testing Mobile App Integration with Postman

Once your backend is running in Docker, you can:

✅ Use Postman to send requests to http://localhost:3000.
✅ Simulate network latency by adding tc (traffic control) rules.
✅ Perform load testing using tools like k6 or JMeter.

Example Postman request:

GET http://localhost:3000

Expected response:

{
    "message": "Production-like API Running!",
    "dbTime": "2025-02-22T14:45:00.123Z"
}

Docker Architecture for Mobile App Testing

  ┌────────────────────────┐
  │     Mobile App (iOS)   │
  └─────────▲──────────────┘
            │ API Calls
  ┌─────────▼──────────────┐
  │ Dockerized Backend API │
  │(Node.js in a container)│
  └─────────▲──────────────┘
            │
  ┌─────────▼──────────────┐
  │  PostgreSQL (Database) │
  ├────────────────────────┤
  │     Redis (Cache)      │
  └────────────────────────┘

Real-World Examples of Docker in Mobile Testing