wwwgmo/docker.sh

199 lines
7.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
############################################################
# Decription: this script manage docker image
# file with an AWX inventory
#
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
# Creation Date: 28.06.2025
# Version: 1.0
#
# Usage: ./docker.sh --help
# Changelog:
# V1.0 - 28.06.2025 - GMo
# Added
# - Creation of script from scratch
#
############################################################
version="1.0"
# with set -e, as soon as a command returns an exit code other than 0,
# the script stops immediately.
set -e
_UID=$(id -u)
_GID=$(id -g)
############################################################
# functions
############################################################
# function usage
usage() {
cat << EOF
Usage: ./$(basename "$0") options
Manage docker image
Options:
build build docker image
build-no-cache build an image from scratch, without using the cache from previous steps.
build-multi build a multi-platform image (amd64, arm64)
build-multi-no-cache build a multi-platform image from scratch , without using the cache from previous steps.
start start the stack
stop stop the stack and delete the container
restart stop and start stack
bash open the ${COMPOSE_PROJECT_NAME}-php-fpm-1 container bash
bash-root open the ${COMPOSE_PROJECT_NAME}-php-fpm-1 container bash with root user
logs tail logs from ${COMPOSE_PROJECT_NAME}-php-fpm-1 -f
push push image php-fpm on docker registry
down stop and delete the container
help|-h|--help display this help
version|-v|--version display script version
EOF
}
# read the varoables
export $(grep -v '^#' .env | xargs)
# check param exist
if [ -z "$1" ]; then
usage
exit
fi
# check if certs folder exite in src
if [ ! -d "./src/certs" ]; then
mkdir ./src/certs
fi
while test $# -gt 0
do
case "$1" in
start)
sudo chown -R $_UID:$_GID ./certs/
# The env before the docker command is there to assign rights in case different
# users are called upon to work on this project. We comment it for documentation purposes.
# env UID=${_UID} GID=${_GID} docker compose up -d
docker compose up -d
;;
restart)
sudo chown -R $_UID:$_GID ./certs/
docker compose down --remove-orphans
docker compose up -d;
;;
stop)
docker compose stop
;;
bash)
docker exec -it ${COMPOSE_PROJECT_NAME}-php-fpm-1 /bin/sh
;;
bash-root)
docker exec --user root -it ${COMPOSE_PROJECT_NAME}-php-fpm-1 /bin/sh
;;
logs)
docker logs ${COMPOSE_PROJECT_NAME}-php-fpm-1 -f
;;
build)
# build image in local
sudo chown -R $_UID:$_GID ./src/
docker compose build --pull
docker compose up -d;
docker exec --user root ${COMPOSE_PROJECT_NAME}-php-fpm-1 /bin/sh -c "cd /var/www/html; composer require elasticsearch/elasticsearch"
# not needed. We comment it for documentation purposes.
#sleep 5 # wait mysql container up
#docker exec -it wwwgmo-mysql /bin/bash -c "mysql -u root --password=password -e \
# \"CREATE USER IF NOT EXISTS 'xmaroot'@'%' IDENTIFIED BY 'sqlAdmin'; \
# GRANT ALL PRIVILEGES ON *.* TO 'xmaroot'@'%'; \
# flush privileges;\""
docker compose stop
;;
build-no-cache)
# build image in local
sudo chown -R $_UID:$_GID ./src/
docker compose build --pull --no-cache
docker compose up -d;
docker exec --user root ${COMPOSE_PROJECT_NAME}-php-fpm-1 /bin/sh -c "cd /var/www/html; composer require elasticsearch/elasticsearch"
docker compose stop
;;
build-multi-no-cache)
# build image multi platform linux/amd64 and linux/arm64
#
# This operation may take some time (env. 15 min). Please be patient.
#
#echo "${DOCKER_USER}/wwwgmo-php-fpm:${DOCKER_IMAGE_VERSION}"
#cp ./certs/ca.pem ./src/certs/.
sudo chown -R $_UID:$_GID ./src/
# load images needed to buil docker iomage for multi platform
if [ "$(docker images -q moby/buildkit)" == "" ]; then
docker buildx create --use --name gmobuilder
docker buildx inspect gmobuilder --bootstrap
fi
# login to docker hub
docker login -u="${DOCKER_USER}" -p="${DOCKER_PASS}"
# builds image for linux/amd64 and linux/arm64
docker buildx build --no-cache --platform linux/amd64,linux/arm64 -t "${DOCKER_USER}/wwwgmo-php-fpm:${DOCKER_IMAGE_VERSION}" --push -f ./docker/php-fpm/Dockerfile .
# start stack
docker compose up -d
# install elasticsearch client for PHP
docker exec --user root ${COMPOSE_PROJECT_NAME}-php-fpm-1 /bin/sh -c "cd /var/www/html; composer require elasticsearch/elasticsearch"
# stop stack
docker compose stop
# logout from docker hub
docker logout
# cleanup container, image and instance to create multi platform image
docker stop buildx_buildkit_gmobuilder0
docker container rm buildx_buildkit_gmobuilder0
docker rmi moby/buildkit:buildx-stable-1 --force
docker buildx rm --all-inactive --force
;;
build-multi)
# build image multi platform linux/amd64 and linux/arm64
#
# This operation may take some time (env. 15 min). Please be patient.
#
#echo "${DOCKER_USER}/wwwgmo-php-fpm:${DOCKER_IMAGE_VERSION}"
#cp ./certs/ca.pem ./src/certs/.
sudo chown -R $_UID:$_GID ./src/
# load images needed to buil docker iomage for multi platform
if [ "$(docker images -q moby/buildkit)" == "" ]; then
docker buildx create --use --name gmobuilder
docker buildx inspect gmobuilder --bootstrap
fi
# login to docker hub
docker login -u="${DOCKER_USER}" -p="${DOCKER_PASS}"
# builds image for linux/amd64 and linux/arm64
docker buildx build --platform linux/amd64,linux/arm64 -t "${DOCKER_USER}/wwwgmo-php-fpm:${DOCKER_IMAGE_VERSION}" --push -f ./docker/php-fpm/Dockerfile .
# start stack
docker compose up -d
# install elasticsearch client for PHP
docker exec --user root ${COMPOSE_PROJECT_NAME}-php-fpm-1 /bin/sh -c "cd /var/www/html; composer require elasticsearch/elasticsearch"
# stop stack
docker compose stop
# logout from docker hub
docker logout
# cleanup container, image and instance to create multi platform image
docker stop buildx_buildkit_gmobuilder0
docker container rm buildx_buildkit_gmobuilder0
docker rmi moby/buildkit:buildx-stable-1 --force
docker buildx rm --all-inactive --force
;;
push)
docker login -u="${DOCKER_USER}" -p="${DOCKER_PASS}"
docker image push ${DOCKER_IMAGE}:${DOCKER_IMAGE_VERSION}
docker logout
;;
down)
docker compose down --remove-orphans
;;
version|-v|--version)
cat << EOF
$(basename "$0") v$version (c) 1990 - $(date +%Y) by Gilles Mouchet
Non-Commercial Use License See LICENSE for details
EOF
exit
;;
*|help|-h|--help)
usage
exit
;;
esac
shift
done
exit 0