> For the complete documentation index, see [llms.txt](https://docs.release.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.release.com/reference-documentation/reference-examples/adding-database-containers-to-application-templates.md).

# Adding database containers to the Application Template

Best practices for running your application's database in a container

This document should cover the basics for adding a database to your application.

A few caveats that come with running databases in containers:

* Containers and pods in Kubernetes can be restarted, deleted, or moved at any time, so you should not rely on them for any critical services. For mySQL, the main pod cannot be running when a new one is deployed or restarted. We mitigate this risk by recommending you use a Stateful Set with `stateful: true` for mySQL. (Postres does not have this issue that we have noticed.)
* Containers are ephemeral and can move around, so it is vital to use a persistent storage volume so that restarting a container does not erase the database.
* Kubernetes may start a container before the database is completely initialized or ready, so you should use readiness probes to ensure deployments wait for the database to be ready to receive traffic.
* Passwords can be randomized and the database connections should be kept internal only so that external people cannot access the data or store their own data in your tables, even if it is a test database.

## Postgres

#### Example Postgres Application Template

```yaml
services:
- name: db
  image: public.ecr.aws/lts/postgres:12-20.04_edge
  volumes:
  - type: persistent
    name: db_data
    mount_path: "/var/lib/postgresql/data"
  ports:
  - type: container_port
    port: '5432'
  readiness_probe:
    exec:
      command:
      - psql
      - "-h"
      - localhost
      - "-U"
      - postgres
      - "-c"
      - SELECT 1
    period_seconds: 2
    timeout_seconds: 2
    failure_threshold: 30
  storage:
    size: 10Gi
    type: aws-efs
```

#### Example Postgres Environment Template

<pre class="language-yaml"><code class="lang-yaml">mapping:
<strong>  DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}"
</strong>services:
  db:
<strong>  - key: POSTGRES_PASSWORD
</strong>    value: secretPassword
<strong>    secret: true
</strong>  - key: POSTGRES_USER
    value: postgres
  - key: POSTGRES_DB
    value: postgres
  - key: POSTGRES_HOST_AUTH_METHOD
    value: trust
  - key: PGDATA
    value: "/var/lib/postgresql/data/pgdata"
</code></pre>

## MySQL

#### Example MySQL Application Template

```yaml
- name: db
  image: mysql:5.7
  stateful: true
  volumes:
  - type: persistent
    name: db-data
    mount_path: "/var/lib/mysql"
  ports:
  - type: container_port
    port: '5432'
  liveness_probe:
    exec:
      command: ["mysqladmin", "ping"]
    initial_delay_seconds: 30
    period_seconds: 10
    timeout_seconds: 5
    failure_threshold: 10
  readiness_probe:
    exec:
      # Check we can execute queries over TCP (skip-networking is off).
      command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
    initial_delay_seconds: 5
    period_seconds: 2
    timeout_seconds: 1
    failure_threshold: 30
  storage:
    size: 10Gi
    type: aws-efs
```

#### Example MySQL Environment Template

<pre class="language-yaml"><code class="lang-yaml">services:
  db:
<strong>  - name: MYSQL_ALLOW_EMPTY_PASSWORD
</strong>    value: "1"
</code></pre>

## Notes

* Change the value for `name:` to a more specific name if you have more than one database. This is especially important for [App Imports](/guides-and-examples/common-setup-examples/app-imports-connecting-two-apps-together.md).
* Change the value for `image:` to match the version number of the database you want and the Cloud provider you use. See [AWS Public Gallery](https://gallery.ecr.aws/lts/postgres), [Dockerhub Registries](https://hub.docker.com/_/postgres), and [GCP registry](https://console.cloud.google.com/gcr/images/google-containers/GLOBAL). When using [DockerHub](/integrations/integrations-overview/docker-hub-integration.md) or a [private registry](/integrations/integrations-overview/private-registry.md), be sure to configure the integrations with your credentials.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.release.com/reference-documentation/reference-examples/adding-database-containers-to-application-templates.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
