add: docker_server role

This commit is contained in:
mrjk 2025-07-14 21:08:39 -04:00
parent 5076e9429b
commit 8b627ca6c6
7 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# Install docker deamon on debian

View File

@ -0,0 +1 @@
docker_install_extra_scripts: True

8
roles/docker_server/files/ddu Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
docker ps --format '{{ .Size }}\t{{ .ID }}\t{{.Image}}\t{{ .Names }}' | sort -h | column -t
# Ways to clean disk space:
# docker system prune -a
# docker volume rm $(docker volume ls -qf dangling=true)

34
roles/docker_server/files/dip Executable file
View File

@ -0,0 +1,34 @@
#!/bin/bash
# From: https://gist.github.com/ipedrazas/2c93f6e74737d1f8a791?permalink_comment_id=3704504#gistcomment-3704504
function dip() {
local cols='%-13s %-48s %-40s %-80s'
_print_container_info() {
local container_id
local container_ports
local container_ip
local container_name
container_id="${1}"
container_ports=( $(docker port "$container_id" | grep -o "0.0.0.0:.*" | cut -f2 -d:) )
container_name="$(docker inspect --format "{{ .Name }}" "$container_id" | sed 's/\///')"
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}" "$container_id" )"
printf "$cols\n" "$container_id" "$container_name" "$container_ip" "${container_ports[*]}"
}
local container_id
container_id="$1"
printf "$cols\n" 'Container Id' 'Container Name' 'Container IP' 'Container Ports'
if [ -z "$container_id" ]; then
local container_id
docker ps -a --format "{{.ID}}" | while read -r container_id ; do
_print_container_info "$container_id"
done
else
_print_container_info "$container_id"
fi
}
dip

View File

@ -0,0 +1,25 @@
#!/bin/bash
# Start a docker netshoot container
set -eu
main ()
{
local container=${1-}
local img=${2:-nicolaka/netshoot}
local args=
local opts=
if [[ -n "$container" ]]; then
opts="--network container:$container"
fi
if [[ "$container" == "$img" ]]; then
img=$(docker inspect --format='{{.Image}}' "$container")
fi
docker run --rm -ti -v "$HOME/.cache/netshoot:/root" $opts $img /bin/bash
}
main $@

26
roles/docker_server/files/nip Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
main ()
{
# List all Docker networks
networks=$(docker network ls -q --format '{{.Name}},{{.ID}}' | sort | cut -d, -f 2)
# Loop through the networks and display the details
for network in $networks; do
local containers=$(docker network inspect --format='{{range $k, $v := .Containers}}{{$k}} {{end}}' "$network")
local net_name=$(docker network inspect --format '{{ .Name }}' $network)
printf "%-30s %s\n" "$net_name" "$network"
for container in $containers; do
local cont=$(docker network inspect --format="{{ index .Containers \"$container\" | json }}" "$network" )
local name=$(jq -r ".Name" <<< "$cont")
local ip=$(jq -r ".IPv4Address" <<< "$cont")
printf " %-30s %s\n" "$ip" "$name"
done
done
}
main $@

View File

@ -0,0 +1,40 @@
# Use of apt_key plugin is deprecated, see: https://www.jeffgeerling.com/blog/2022/aptkey-deprecated-debianubuntu-how-fix-ansible
- name: Add Docker apt repository key
ansible.builtin.get_url:
url: https://download.docker.com/linux/debian/gpg
dest: /etc/apt/trusted.gpg.d/docker.asc
mode: '0644'
- name: Add Docker Repository
apt_repository:
filename: docker
repo: deb https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable
state: present
- name: Ensure docker packages are installed
apt:
state: present
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- name: "Copy docker helper scripts to /usr/local/bin: ({{ docker_install_extra_scripts | ternary('present', 'absent') }})"
ansible.builtin.copy:
src: "{{ item }}"
dest: /usr/local/bin/
mode: '0755'
with_fileglob:
- files/*
when: "docker_install_extra_scripts"
- name: Ensure extra packages are installed
apt:
state: present
name:
- jq # For nip support
when: "docker_install_extra_scripts"