Search code examples
postgresqlamazon-web-servicesdockeransibleinfrastructure

ansible docker postgres volume


i am doing some postgres deployment with docker, ansible and terraform in aws

things are going relatively well, i start the instance with terraform, provision the instance with docker using ansible, start my postgres container with ansible also, and attach a ebs volume to my instance, which i intend to use as the main data storage.

but i am confused as to how to attach the volume to the docker (not to the instance as i am able to do that using terraform)

i imagine it is possible using ansible or modifiying the dockerfile, but the documentation of the "volume" which seems to be the answer is not that clear to me.

so if i had an ansible playbook like this:

name: Start postgis
docker_container:
name: postgis
image: "{{ ecr_url }}"    
network_mode: bridge
exposed_ports:
  5432
published_ports:
  5432:5432
state: started

how would i specify the ebs volume to be used for the data storage of Postgres?

resource "aws_volume_attachment" "ebs-volume-postgis-attach" {
device_name = "/dev/xvdh"
volume_id = "${aws_ebs_volume.ebs-volume-postgis.id}"
instance_id = "${aws_instance.postgis.id}"
}

that was the code used to attach the ebs volume, in case someone is interested

please ask any kind of info that you need, all help is deeply apreciated


Solution

  • Here is a checklist:

    1. Attach EBS volume (disk) to EC2 instance (e.g. /dev/xvdh)

    2. Make partition (optional) (e.g. /dev/xvdh1)

    3. Make filesystem on the partition/disk

    4. Mount filesystem inside your EC2 instance (e.g. /opt/ebs_data)

    5. Start Docker-container with volume (e.g. /opt/ebs_data:/var/lib/postgresql/data)

    In Ansible's docker_container module, volumes is a list, so:

    - docker_container:
        name: postgis
        image: "{{ ecr_url }}"    
        network_mode: bridge
        exposed_ports:
          - 5432
        published_ports:
          - 5432:5432
        state: started
        volumes:
          - /opt/ebs_data:/var/lib/postgresql/data