Issue with Running Docker Compose on Windows via Ansible from Ubuntu

I am trying to manage a Windows machine from Ubuntu using Ansible. I am able to successfully transfer my target files, including Docker Compose files, to the Windows machine. However, when I try to start the Docker Compose files via Ansible, I encounter the following error:

error getting credentials - err: exit status 1, out: The specified session does not exist. It may have been closed earlier.

When I manually run the docker compose up -d command on the Windows machine, it works successfully.

Setup:

Ansible Playbook:

  • Install Docker on the Windows machine
  • Copy the necessary files Start
    the application with Docker Compose

Defaults:

  • I am connecting to the Windows machine via WinRM.
  • Docker Desktop is installed on the Windows machine.
  • I face issues running docker compose via Ansible, but it works when I
    run it manually on the Windows machine.

docker-compose.yml file:

---
- name: Deploy Redis on Windows
  hosts: redis_hosts
  vars_files:
    - ./vars.yml

  tasks:
    - name: Ensure Docker is installed
      win_chocolatey:
        name: docker-desktop
        state: present

    - name: Create application directory
      win_file:
        path: "{{ app_files_dest }}"
        state: directory

    - name: Copy application files
      win_copy:
        src: "{{ app_files_src }}/"
        dest: "{{ app_files_dest }}"

 
    - name: Start the application with Docker Compose
      win_command: docker compose -f redis.yml up -d
      args:
        chdir: "{{ app_files_dest }}"

inventory.ini:

[windows]
192.168.2.152 ansible_user=w10 ansible_password=1 ansible_connection=winrm ansible_winrm_transport=basic ansible_port=5985 ansible_winrm_scheme=http

vars.yml:

app_files_src: “{{ file_path }}/cache”

app_files_dest: ‘C:\app\cache’

Why do I encounter this error when running docker compose via Ansible? How can I resolve this issue?

Configuring windows via ansible is possible. but feels like gambling.

What works for me sometimes is switching to win_shell with parameter executable: powershell.exe.

1 Like

I just wanted to quote this as the best description of Windows + Ansible experience I have seen… and it put a big smile on my face :joy:

1 Like

The community.docker collection does not support Windows (see the README: Ansible Galaxy).

I don’t know what’s the current state of running Python modules on Windows (outside of WSL), but all modules in that collection are Python modules. There are no PowerShell / CSharp modules in the collection. My guess is that the error you’re seeing,

basically means that Python modules still don’t work under Windows.

1 Like

The user is using raw win cmd commands. Thats imho not a python/comminuty.docker issue.

2 Likes

Oops, I misread that… I’m going to blame that on still recovering from the flu… :slight_smile:

This would indicate that the docker.exe command is trying to access something like the user’s secret store or something else not typically available over a network logon (WinRM/SSH will all produce a network logon). You most likely have a few options to get this working:

  • Use a auth type that does cred delegation, CredSSP for winrm/psrp, password plaintext auth for ssh
  • Use become on the task with either the become user being SYSTEM or the same ansible_user/ansible_password
  • See if the docker.exe compose step has something to avoid trying to access the DPAPI store
  • Try using command through the local connection plugin

The first two options are ways to pretend to be an interactive user and run commands like it would work locally. Reading online it looks like there’s some setting like "credsStore": "secretservice" that could be the culprit in the docker config file. Maybe removing it or seeing if there are other options might work if you don’t need the credential store.

The last option works but only if the playbook is run through WSL:

- name: run docker compose through localhost
  command: docker.exe compose -f redis.yml up -d
  delegate_to: localhost 

As WSL can execute Windows binaries natively this should work, if it can’t find docker.exe in the Linux PATH env var this may work to spawn a sub shell in Windows that can find docker.exe

- name: run docker compose through localhost
  shell: docker.exe compose -f redis.yml up -d
  args:
    executable: /mnt/c/Windows/System32/cmd.exe
    chdir: /mnt/c/...
  delegate_to: localhost 

Hi,Thank you for all the answers. I found the solution to the problem and ı am using winrm. The problem was solved when I added the values ​​
become=yes
become_user=w10
to my windows host in my inventory.ini file.