0

I am trying to build docker container which should include startup scripts in container's /etc/my_init.d directory via ansible. I have difficulty finding any documentation how to do this. Here is relevant portion of my yaml file:

- name: Create container
  docker:
    name: myserver
    image: "{{ docker_repo }}/myserver:{{ server.version }}"
    state: started
    restart_policy: always
    docker_api_version: 1.18
    registry: "{{ docker_repo }}"
    username: "{{ registry_user }}"
    password: "{{ registry_password }}"
    links:
    - "mywebservices"
    ports:
    - "8000:8000"
    - "9899:9899"
    volumes:
    - "{{ myserver_home_dir }}/logs:/var/log/my_server"
    env:
      MY_ENVIRONMENT: "{{ my_environment }}"
  when: myserver_action == "create"
        or (myserver_action == "diff-create" and myserver.changed)
        or myserver_action == "update"

What should I add in here to tell ansible to put my files into container's /etc/my_init.d during build?

Dimon Buzz
  • 1,130
  • 3
  • 16
  • 35
  • Which files are you talking about here? – Tarun Lalwani Aug 11 '17 at 04:07
  • Any files in general. Executable, data this doesn't really matter. I want to achieve a very simple task. After container is started, I want to execute my startup script. It is somewhat an anti-pattern in containers, but I have a requirement to inject specific workflow upon first container startup. – Dimon Buzz Aug 15 '17 at 13:15

2 Answers2

0

First of all, you can't build container (you can start it), you build images.

Second, docker module is deprecated, use docker_image to build images.

You should copy your files into build directory (with copy or synchronize modules), for example:

/tmp/build

Then create Dockerfile that will take them from build directory and add into your image.

After that call docker_image:

docker_image:
  path: /tmp/build
  name: myimage

Finally start your container:

docker_container:
  image: myimage
  name: mycontainer
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
0

Unsure if it's relevant, as I don't know what your startup Ansible content is doing, but it's probably worth looking at the Ansible Container project.

https://github.com/ansible/ansible-container

You can build your container images using Ansible roles instead of a Dockerfile, orchestrate them locally, and deploy them to production Kubernetes or Red Hat OpenShift.

j00bz
  • 71
  • 5