Skip to content

Commit

Permalink
docker: Assign port number to static docker container during automate…
Browse files Browse the repository at this point in the history
…d deployment (#3511)

* Assign port number to static docker container

* satisfy linter

* modify docker_port variable

---------

Co-authored-by: Martijn Verburg <[email protected]>
  • Loading branch information
Haroon-Khel and karianna authored Apr 11, 2024
1 parent 9892a79 commit 58d8783
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
- name: Build {{ docker_image }} docker images
command: docker build --cpu-period=100000 --cpu-quota=800000 -t aqa_{{ docker_image }} --memory=6G -f /tmp/Dockerfile.{{ docker_image }} /tmp/

# Without specifying a port here, docker will give the container a random unused port
- name: Run {{ docker_image }} docker container
command: docker run --restart unless-stopped -p 22 --cpuset-cpus="0-3" --memory=6G --detach --name {{ docker_image | upper }}.PORT aqa_{{ docker_image }}
# Finds the highest port number already assigned and +1
- name: Find available port
shell: docker ps --format \"\{\{\.Ports\}\}\" | awk -F[:-] '{print $2}' | sort | tail -n 1
register: docker_port_output

- name: Set docker_port variable if empty
set_fact:
docker_port: 32000
when: docker_port_output.stdout == ""

# Now we want to rename the running container with the port name
- name: Find assigned port of {{ docker_image }} container
shell: docker port {{ docker_image | upper }}.PORT | head -n 1 | cut -d ':' -f 2
register: docker_port
- name: Set docker_port variable when non empty
set_fact:
docker_port: "{{ docker_port_output.stdout | int + 1 }}"
when: not (docker_port_output.stdout == "")

- name: Rename {{ docker_image }} container to {{ docker_image | upper }}.{{ docker_port.stdout_lines[0] }}
command: docker rename {{ docker_image | upper }}.PORT {{ docker_image | upper }}.{{ docker_port.stdout_lines[0] }}
- name: Run {{ docker_image }} docker container
command: docker run --restart unless-stopped -p {{ docker_port }}:22 --cpuset-cpus="0-3" --memory=6G --detach --name {{ docker_image | upper }}.{{ docker_port }} aqa_{{ docker_image }}

0 comments on commit 58d8783

Please sign in to comment.