Sam
Sam

Reputation: 325

Running mvn clean install inside docker container using ansible playbook

I am trying to run mvn clean install command inside the docker container. But nothing seems to happen as I have mapped my container to a volume and no target folder is created. This is the playbook. Apologies if this is a silly question but I have been stuck here for some frustating time now.

---
- name: Building project
  hosts: all
  become: true
  become_user: root
  tasks:
     - name: install docker mvn
       docker_container:
          name: maven_build_direct
          image: maven
          volumes:
            - /home/user/Desktop/Training/docker/maven_task/happy/GsaJavaExample/:/proj
          command: cd /proj
          command: mvn clean install

I also tried running commands from outside the docker-container module but since the container already stops it gives error. Something like this

     - name: copy content in container to some other folder
       command: docker exec -i maven_build_direct bash -c 'echo "Hello1"; echo "Hello2"'

The error for the above is

fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["docker", "exec", "-i", "maven_build_direct", "bash", "-c", "echo \"Hello1\"; echo \"Hello2\""], "delta": "0:00:00.212534", "end": "2018-08-26 17:07:06.995110", "msg": "non-zero return code", "rc": 126, "start": "2018-08-26 17:07:06.782576", "stderr": "", "stderr_lines": [], "stdout": "OCI runtime exec failed: exec failed: cannot exec a container that has stopped: unknown", "stdout_lines": ["OCI runtime exec failed: exec failed: cannot exec a container that has stopped: unknown"]}

Upvotes: 2

Views: 1605

Answers (1)

yamenk
yamenk

Reputation: 51826

As pointed out in the comments, the first problem is that you are specifying multiple command properties, which won't work. The proper way to do this, is to set the workdir property as such:

      ...
      volumes:
        - /home/user/Desktop/Training/docker/maven_task/happy/GsaJavaExample/:/proj
      working_dir: /proj
      command: mvn clean install
      ...

Upvotes: 1

Related Questions