2

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

1 Answers1

3

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
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • thank you so much, i was missing the "make filesystem" step, one quick question: defining the volume is : "ebs_storage_path:postgres_data_dir" correct? – Fernando Castilla Ospina Sep 19 '17 at 19:06
  • alright, this works now, one detail: it doesnt work if you use the root of the mounted volume as directory, you must use some subdirectory (example: /opt/ebs_data/postgres), i believe it is worth mentioning in your answer – Fernando Castilla Ospina Sep 20 '17 at 13:59