Andrew Ramnikov
Andrew Ramnikov

Reputation: 793

Ansible Docker: Execute shell file with container

I have the following Docker Command:

docker run -d  --name ADMIN_SERVER_TEST1 --network=soa_net --ip=172.16.1.11 -p 7001:7001 -v /Oracle_Volumes/ENVIRONMENTS/TEST1/SOA-Volume/cheetah_domain:/u01/oracle/user_projects   --env-file $ENV_HOME/adminserver-test1.env.list --env-file $ENV_HOME/soaserver-test1.env.list -u root redreply/soa-suite:12.2.1.3 ./dockertools/createDomainAndStart.sh && docker logs -f ADMIN_SERVER_TEST1

For this command, I have created the following Script with Ansible:

  - name: Create SOA container
    docker_container:
      name: "{{ name }}"
      image: "{{ image }}"
      detach: yes
      privileged: yes
      user: root
      volumes:
          - "{{ src_vol }}:{{ dest_vol }}"
      ports:
          - 7001:7001
      networks: 
        - name: soa_net
          ipv4_address: "{{ ip }}"
          env_file: 
            - "{{ env_file_1}}"
            - "{{ env_file_2}}"

But I have no clue how to add this part to Ansible script:

./dockertools/createDomainAndStart.sh && docker logs -f ADMIN_SERVER_TEST1

Upvotes: 1

Views: 1544

Answers (1)

nwinkler
nwinkler

Reputation: 54437

If you want to run your script as the command that gets executed in the container when it starts, you can specify that with the command attribute like this:

  - name: Create SOA container
    docker_container:
      name: "{{ name }}"
      image: "{{ image }}"
      detach: yes
      privileged: yes
      user: root
      command: ./dockertools/createDomainAndStart.sh
      volumes:
          - "{{ src_vol }}:{{ dest_vol }}"
      ports:
          - 7001:7001
      networks: 
        - name: soa_net
          ipv4_address: "{{ ip }}"
          env_file: 
            - "{{ env_file_1}}"
            - "{{ env_file_2}}"

I basically added the command attribute to call your ./dockertools/createDomainAndStart.sh script when the container is started.

The Ansible documentation has this description for the command attribute:

Command to execute when the container starts. A command may be either a string or a list. Prior to version 2.4, strings were split on commas.

Upvotes: 3

Related Questions