Q:

How to import dump with Ansible into postgres inside a docker container

I want to import a dump into Postgres docker contaner. Here is my Task definition:
- name: Import DB
  shell: >
    cat /tmp/db.backup |
    docker-compose exec -T postgres \
    psql --username my_user
  args:
    chdir: "{{ working_directory }}"

Here is the 
fatal: [staging_host]: FAILED! => {"changed": true, "cmd": "cat /tmp/db.backup | docker-compose exec -T postgres psql --username my_user", "delta": "0:00:00.392214", "end": "2019-03-26 15:26:07.443355", "msg": "non-zero return code", "rc": 1, "start": "2019-03-26 15:26:07.051141", "stderr": "psql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\nread unix @->/var/run/docker.sock: read: connection reset by peer", "stderr_lines": ["psql: could not connect to server: No such file or directory", "\tIs the server running locally and accepting", "\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?", "read unix @->/var/run/docker.sock: read: connection reset by peer"], "stdout": "", "stdout_lines": []}

There is no problem if I run the command in "cmd" manually: 
cat /tmp/db.backup | docker-compose exec -T postgres psql --username my_user

I would like so less dependencies as possible, because of that I wouldn't like to use postgresql_db

A:

A solution is to create a new temporary container mounting the file to import and overwriting the command:
- name: Import DB
  shell: >
    docker-compose run -T --rm -v /tmp/db.backup:/tmp/db.backup postgres \
    psql --host postgres --username postgres < /tmp/db.backup
  args:
    chdir: "{{ working_directory }}"

