Skip to content

Commit

Permalink
Adding external volume to docker_run (#2)
Browse files Browse the repository at this point in the history
* added volume entry in tasks

* volume entryes added in default

* volume variable update to host_volume

* Update README.md

* Update main.yml

* Update main.yml

* Update README.md

* Update main.yml

* Update main.yml

* Update main.yml

* Update nginx.yml

* Update test.yml

* Update main.yml

* Set docker_run_volume variable

* Update main.yml

* set docker variable

* Update nginx.yml added variable

* Update test.yml

* Update main.yml typo

* Update README.md

* Update README.md
  • Loading branch information
alexcos78 authored Sep 20, 2023
1 parent 984c42d commit c4924fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Role docker_run
=========

The role starts a docker container from the specified image and parameters.
The role support the definition of an external volume for data storage.

Requirements
------------
Expand All @@ -18,7 +19,7 @@ Role Variables
- `docker_run_env_variables`: list of environment variables (key/value pair) - see below. Default: []
- `docker_run_ports`: list of ports to publish from the container to the host. Use the docker CLI syntax, e.g. [ "8080" ] or [ "8080:80" ]. Default: []
- `docker_run_command`: command to be executed, can be left blank

- `docker_run_volume`: volume path on the container side, see more under the "External volume" section.

Environment variables
--------------
Expand All @@ -28,6 +29,11 @@ Varaible(s) are copied down into a file .env that is used to start the container
env_file: /opt/{{ docker_run_appname }}/.env --> path to file containig a list of environment variables in the form FOO=BAR
```

External volume
--------------
Volume path on the container side is mounted by populating the variable `docker_run_volume`.
Volume path on the host side is set by default to "/data". It can be changed by modifying the `docker_run_host_volume` variable under `defaults/` folder.

Dependencies
------------

Expand All @@ -46,6 +52,7 @@ Example Playbook
docker_run_tag: latest
docker_run_ports: [ "8080:80" ]
docker_run_command: ""
docker_run_volume: ""
```


Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ docker_run_tag: latest
docker_run_env_variables: []
docker_run_ports: []
docker_run_command: ""
docker_run_volume: ""
docker_run_host_volume: "/data"
6 changes: 5 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
state: touch
mode: u=rw,g=r,o=r


- name: Set environment variables
lineinfile:
path: /opt/{{ docker_run_appname }}/.env
line: "{{ item.key }}={{ item.value }}"
create: yes
with_dict: "{{ docker_run_env_variables }}"

- name: Set docker_run_volume_on_container variable
set_fact:
docker_run_volume_on_container: "{{ docker_run_host_volume }}:{{ docker_run_volume }}:rw"
when: docker_run_volume is defined and docker_run_volume != ''

- name: Running the container
docker_container:
Expand All @@ -28,5 +31,6 @@
env_file: /opt/{{ docker_run_appname }}/.env
command: "{{ docker_run_command }}"
ports: "{{ docker_run_ports }}"
volumes: "{{ docker_run_volume_on_container | default(omit) }}"
detach: True
state: started
11 changes: 6 additions & 5 deletions tests/nginx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
remote_user: root
roles:
- role: ansible-role-docker_run
appname: nginx
image: nginx
tag: latest
ports: "8080:80"
command: ""
docker_run_appname: nginx
docker_run_image: nginx
docker_run_tag: latest
docker_run_ports: "8080:80"
docker_run_command: ""
docker_run_volumes: ""
11 changes: 6 additions & 5 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
remote_user: root
roles:
- role: ansible-role-docker_run
appname: container
image: ubuntu
tag: latest
ports: "80"
command: "printenv"
doker_run_appname: container
doker_run_image: ubuntu
doker_run_tag: latest
doker_run_ports: "80"
doker_run_command: "printenv"
docker_run_volume: ""

0 comments on commit c4924fb

Please sign in to comment.