Last active
April 4, 2018 13:04
-
-
Save LM1LC3N7/dcd1dce90b4dcd374ac76bd685cc3f60 to your computer and use it in GitHub Desktop.
Script to easily create docker networks and volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Execute: | |
# chmod +x create_docker_volumes_and_networks.sh | |
# ./create_docker_volumes_and_networks.sh | |
# | |
# Verify docker networks and docker network cards: | |
# docker network ls | |
# ip a | |
# | |
# Verify docker volumes: | |
# docker volume ls | |
# | |
# Delete ALL UNUSED volumes & networks: | |
# docker nework prune -f | |
# docker volume prune -f | |
# | |
# | |
# Each new networks or volumes will be created. | |
# | |
# Note: Names should not contain spaces. | |
# They will be replaced by an underscore. | |
# | |
# Reminder: Docker volume's default path is in | |
# /var/lib/docker but can be changed | |
# as explained https://bit.ly/2GApYo3 | |
# | |
NETWORKS=("reverse_proxy" "web") | |
VOLUMES=("reverse_proxy") | |
# Check if all networks exists | |
for NET in "${NETWORKS[@]}" ; do | |
# Replace spaces by underscores | |
NET="$(echo ${NET// /_})" | |
N=$(docker network ls | grep "$NET") | |
if [ -z "$N" ] ; then | |
echo "Creating docker network \"$NET\" :" | |
docker network create --opt 'com.docker.network.bridge.name'=$NET $NET | |
if [ "$?" -ne 0 ] ; then echo "Cannot create docker network $NET" ; fi | |
echo "" | |
fi | |
done | |
# Check if all volumes exists | |
for VOL in "${VOLUMES[@]}" ; do | |
# Replace spaces by underscores | |
VOL="$(echo ${VOL// /_})" | |
V=$(docker volume ls | grep "$VOL") | |
if [ -z "$V" ] ; then | |
echo "Creating docker volume \"$VOL\" :" | |
docker volume create $VOL | |
if [ "$?" -ne 0 ] ; then echo "Cannot create docker volume $VOL" ; fi | |
echo "" | |
fi | |
done | |
echo "All volumes and networks have been created." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment