> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/Celaya55/app-cr/llms.txt
> Use this file to discover all available pages before exploring further.

# Docker Deployment

> Learn how to deploy App CR using Docker Compose

# Docker Deployment

App CR uses Docker Compose to orchestrate the PostgreSQL database and can be easily extended to containerize the entire application.

## Docker Compose Setup

The project includes a `docker-compose.yml` file that defines the PostgreSQL service:

```yaml docker-compose.yml theme={null}
version: '3.8'

services:
  postgres:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_USER: user_admin
      POSTGRES_PASSWORD: password123
      POSTGRES_DB: mi_db_crud
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
```

## Services Overview

### PostgreSQL Database

* **Image**: postgres:15
* **Port**: 5432 (mapped to host port 5432)
* **Database**: mi\_db\_crud
* **Persistent Storage**: postgres\_data volume

## Getting Started with Docker

<Steps>
  <Step title="Install Docker">
    Ensure you have Docker and Docker Compose installed on your system:

    * [Docker Desktop](https://www.docker.com/products/docker-desktop) (Windows/Mac)
    * [Docker Engine](https://docs.docker.com/engine/install/) (Linux)
  </Step>

  <Step title="Start the Database">
    Run the following command in your project root:

    ```bash theme={null}
    docker-compose up -d
    ```

    The `-d` flag runs containers in detached mode (background).
  </Step>

  <Step title="Verify the Database is Running">
    Check the status of your containers:

    ```bash theme={null}
    docker-compose ps
    ```

    You should see the postgres service running on port 5432.
  </Step>

  <Step title="Run Database Migrations">
    Apply Prisma migrations to set up your database schema:

    ```bash theme={null}
    cd backend
    npx prisma migrate dev
    ```
  </Step>
</Steps>

## Docker Commands Reference

<CodeGroup>
  ```bash Start Services theme={null}
  # Start all services defined in docker-compose.yml
  docker-compose up -d
  ```

  ```bash Stop Services theme={null}
  # Stop all running services
  docker-compose down
  ```

  ```bash View Logs theme={null}
  # View logs from all services
  docker-compose logs -f

  # View logs from specific service
  docker-compose logs -f postgres
  ```

  ```bash Restart Services theme={null}
  # Restart all services
  docker-compose restart

  # Restart specific service
  docker-compose restart postgres
  ```
</CodeGroup>

## Database Management

### Accessing the PostgreSQL Container

You can connect to the PostgreSQL database directly:

```bash theme={null}
# Access PostgreSQL CLI
docker-compose exec postgres psql -U user_admin -d mi_db_crud
```

### Backup and Restore

<CodeGroup>
  ```bash Backup Database theme={null}
  # Create a backup of your database
  docker-compose exec postgres pg_dump -U user_admin mi_db_crud > backup.sql
  ```

  ```bash Restore Database theme={null}
  # Restore from a backup file
  docker-compose exec -T postgres psql -U user_admin -d mi_db_crud < backup.sql
  ```
</CodeGroup>

## Volume Management

<Note>
  The `postgres_data` volume persists your database data between container restarts. This ensures you don't lose data when updating or restarting containers.
</Note>

### View Volumes

```bash theme={null}
docker volume ls
```

### Remove Volumes

<Warning>
  Removing volumes will permanently delete all database data. Only do this if you want to start fresh.
</Warning>

```bash theme={null}
# Stop services and remove volumes
docker-compose down -v
```

## Extending the Setup

You can extend the `docker-compose.yml` to include the backend application:

```yaml docker-compose.yml theme={null}
version: '3.8'

services:
  postgres:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_USER: user_admin
      POSTGRES_PASSWORD: password123
      POSTGRES_DB: mi_db_crud
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network

  backend:
    build: ./backend
    restart: always
    ports:
      - "3000:3000"
    environment:
      - PORT=3000
      - DATABASE_URL=postgresql://user_admin:password123@postgres:5432/mi_db_crud
      - JWT_SECRET=${JWT_SECRET}
    depends_on:
      - postgres
    networks:
      - app-network

volumes:
  postgres_data:

networks:
  app-network:
    driver: bridge
```

## Troubleshooting

### Port Already in Use

If port 5432 is already in use, modify the port mapping in `docker-compose.yml`:

```yaml theme={null}
ports:
  - "5433:5432"  # Map to host port 5433 instead
```

Then update your `DATABASE_URL` accordingly.

### Connection Issues

If you can't connect to the database:

1. Verify the container is running: `docker-compose ps`
2. Check container logs: `docker-compose logs postgres`
3. Ensure your `DATABASE_URL` matches the Docker configuration

### Reset Everything

To completely reset your Docker setup:

```bash theme={null}
# Stop containers, remove volumes, and rebuild
docker-compose down -v
docker-compose up -d --build
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Environment Variables" icon="gear" href="/deployment/environment">
    Configure environment variables for your application
  </Card>

  <Card title="Production Deployment" icon="rocket" href="/deployment/production">
    Learn best practices for production deployments
  </Card>
</CardGroup>
