Size: 3157 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash

#########################################
### DOCKER-SPECIFIC UTILITY FUNCTIONS ###
#########################################

# Finds the name of an active container with the given service name.
function get_service_container_name() {
    # Get the name of the directory (not full path) of where this is run
    # We need to lowercase this directory (as docker-compose does) to be
    # able to properly search for the target container.
    local current_dir=$(echo "${PWD##*/}" | awk '{print tolower($0)}')

    # This is an argument from the user (e.g. api, postgres, etc.)
    local service_name=$1

    # This is how docker-compose constructs the name of compose containers.
    # docker-compose prepends the current directory (without underscores) and service name.
    local docker_compose_prefix="${current_dir//_/}_${service_name}"

    output=$(docker ps --format "{{ .Names }}" | grep ${docker_compose_prefix} | sed -n 1p)

    if [ -z "$output" ]; then
        # If there is no output, then we are probably dealing the the name of a fixed container.
        # Try returning just the results for using the service_name
        echo $(docker ps --format "{{ .Names }}" | grep ${service_name} | sed -n 1p)
    else
        echo ${output}
    fi
}


# Checks if a given volume exists
# Usage: does_volume_exist VOLUME_NAME
# Argument VOLUME_NAME: The name of the volume to check if exists
# Return: Boolean (int) of whether or not the volume of the given name exists or not
function does_volume_exist() {
    local VOLUME_NAME=$1
    if [[ -z $(docker volume ls | grep $VOLUME_NAME) ]]; then
        # There is no volume with the given name
        echo "0";
    else
        echo "1";
    fi
}


# Creates volumes if they do not exist
# Usage: create_volumes_if_not_exist VOLUME_NAME_1 VOLUME_NAME_2 ...
# Argument(s) VOLUME_NAME_1 VOLUME_NAME_2 ...: The names of volumes to create if they do not exist
# Return: Nothing
function create_volumes_if_not_exist() {
    echo "Creating volumes if they do not exist:"

    for VOLUME_NAME in ${@:1}
    do
        volume_exists="$(does_volume_exist $VOLUME_NAME)"
        if [ "$volume_exists" -eq "0" ]; then

            echo "--> Creating non-existent volume $VOLUME_NAME."
            docker volume create $VOLUME_NAME

        else

            echo "--> Volume $VOLUME_NAME already exists. Skipping creation."

        fi
    done

    echo "Done."
    echo ""
}

# Removes volumes if they do exist
# Usage: remove_volumes VOLUME_NAME_1 VOLUME_NAME_2 ...
# Argument(s) VOLUME_NAME_1 VOLUME_NAME_2 ...: The names of volumes to remove if they exist
# Return: Nothing
function remove_volumes() {
    echo "Removing volumes if they exist:"

    for VOLUME_NAME in ${@:1}
    do
        volume_exists="$(does_volume_exist $VOLUME_NAME)"
        if [ "$volume_exists" -eq "0" ]; then

            echo "--> Volume $VOLUME_NAME doesn't exist. Skipping removal."

        else

            echo "--> Volume $VOLUME_NAME exists. Removing..."
            docker volume rm $VOLUME_NAME
            echo "    --> Removed $VOLUME_NAME."

        fi
    done

    echo "Done."
    echo ""
}
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 5ms.