# Unable to find file with docker\_container exec module

**URL:** <https://forum.ansible.com/t/unable-to-find-file-with-docker-container-exec-module/7953>\
**Category:** Get Help\
**Created:** [August 19, 2024, 5:11pm UTC](https://forum.ansible.com/t/unable-to-find-file-with-docker-container-exec-module/7953 "2024-08-19T17:11:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [August 19, 2024, 5:11pm UTC](https://forum.ansible.com/t/unable-to-find-file-with-docker-container-exec-module/7953/1 "2024-08-19T17:11:04Z")

</div>

Bonjour 👋

My task:

```auto
- name: "MongoDB Operate | Create user - {{ mongo_metrics_exporter_user }}"
  community.docker.docker_container_exec:
    container: "{{ app_name }}-{{ allocation_id.stdout }}"
    command: "mongosh admin -u {{ mongo_root_username }} -p {{ mongo_root_password }} < metrics_exporter_user.js"
    chdir: "/data/db"

```

Result:

```auto
FAILED - RETRYING: [pc09.quanticware.com]: MongoDB Operate | Create user - exporter (9 retries left).Result was: changed=true 
  attempts: 2
  invocation:
    module_args:
      api_version: auto
      argv: null
      ca_path: null
      chdir: /data/db
      client_cert: null
      client_key: null
      command: mongosh admin -u <user> -p <password> metrics_exporter_user.js
      container: mongo-63aab8de-4a87-75ee-84b0-690f93909cd8
      debug: false
      detach: false
      docker_host: unix:///var/run/docker.sock
      env: null
      ssl_version: null
      stdin: null
      stdin_add_newline: true
      strip_empty_ends: true
      timeout: 60
      tls: false
      tls_hostname: null
      tty: false
      use_ssh_client: false
      user: null
      validate_certs: false
  rc: 1
  retries: 11
  stderr: 'Error: ENOENT: no such file or directory, open ''/data/db/<'''
  stderr_lines: <omitted>
  stdout: ''
  stdout_lines: <omitted>

```

But when I’m connected to container:

```auto
root@f17f90a1de26:~# stat /data/db/metrics_exporter_user.js 
  File: /data/db/metrics_exporter_user.js
  Size: 147 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 850160 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-08-19 15:54:16.744183518 +0000
Modify: 2024-08-19 15:31:47.960334327 +0000
Change: 2024-08-19 15:54:12.880228729 +0000
 Birth: 2024-08-19 15:54:10.912251755 +0000

```

And if I try:

```auto
cd /data/db

mongosh admin -u <user> -p <password> metrics_exporter_user.js

```

It works!

Has anyone had this problem before? And solution? 😅

Thanks!

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 19, 2024, 6:31pm UTC](https://forum.ansible.com/t/unable-to-find-file-with-docker-container-exec-module/7953/2 "2024-08-19T18:31:07Z")

</div>

Some of the output doesn’t seem to be consistent with your task. Sometimes the `<` is there, sometimes it is not. I’m assuming it is present in `command`.

The module doesn’t pass the command to a shell, but direclty executes it. The `mongosh` command is executed with the following parameter list:

1. `admin`
2. `-u`
3. `<user>`
4. `-p`
5. `<password>`
6. `<`
7. `metrics_expoerter_user.js`

Now `mongosh` tries to open the file `<`, which doesn’t exist.

If you wrap the command as the argument of `/bin/sh -c "put command here"` then it should work.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [August 20, 2024, 6:21am UTC](https://forum.ansible.com/t/unable-to-find-file-with-docker-container-exec-module/7953/3 "2024-08-20T06:21:49Z")

</div>

Thanks a lot! 🥳

```auto
- name: "MongoDB Operate | Create user - {{ mongo_metrics_exporter_user }}"
  community.docker.docker_container_exec:
    container: "{{ app_name }}-{{ allocation_id.stdout }}"
    command: /bin/sh -c "mongosh admin -u {{ mongo_root_username }} -p {{ mongo_root_password }} < metrics_exporter_user.js"
    chdir: "/data/db"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 19, 2024, 6:22am UTC](https://forum.ansible.com/t/unable-to-find-file-with-docker-container-exec-module/7953/4 "2024-09-19T06:22:19Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
