From 6668e3ad2df75cf5534acbf0125cd8957156c7f0 Mon Sep 17 00:00:00 2001 From: Jacob Seman Date: Mon, 3 Jun 2024 12:14:58 -0600 Subject: [PATCH] Add singular script `adamant_env.sh` to handle container creation Convert Docker run script to Docker Compose (`docker compose up -d`) Make `adamant_env.sh` executable Add all usage args to `adamant_env.sh` output when run with no args Reword `adamant_env.sh` output to be more approachable to users Change `stop` arg to stop the container rather than remove Remove scripts replaced by `adamant_env.sh` Retain and use `docker_config.sh` for user configuration Add Docker Compose network mode for changes in #24 Apply `compose.yaml` fixes for Windows Rename compose.yaml->compose.yml Update compose.yml to use docker_config.sh vars Improve `adamant_env.sh` input handling Convert remaining `docker` commands to `docker-compose` Fix script `build` arg to function with `docker-compose` --- docker/README.md | 3 +- docker/adamant_env.sh | 76 ++++++++++++++++++++++++++++++++++++++ docker/build_image.sh | 4 -- docker/compose.yml | 16 ++++++++ docker/create_container.sh | 24 ------------ docker/docker_config.sh | 0 docker/login_container.sh | 4 -- docker/push_image.sh | 4 -- docker/remove_container.sh | 4 -- docker/start_container.sh | 4 -- docker/stop_container.sh | 4 -- 11 files changed, 94 insertions(+), 49 deletions(-) create mode 100755 docker/adamant_env.sh delete mode 100755 docker/build_image.sh create mode 100644 docker/compose.yml delete mode 100755 docker/create_container.sh mode change 100755 => 100644 docker/docker_config.sh delete mode 100755 docker/login_container.sh delete mode 100755 docker/push_image.sh delete mode 100755 docker/remove_container.sh delete mode 100755 docker/start_container.sh delete mode 100755 docker/stop_container.sh diff --git a/docker/README.md b/docker/README.md index e09e091..5b62c2f 100644 --- a/docker/README.md +++ b/docker/README.md @@ -16,6 +16,7 @@ This procedure is used to create a new Docker container that hosts the Adamant b $ git clone https://github.com/lasp/adamant.git ``` + 3. Next, tell Docker to create a new container from the [pre-built image](https://github.com/lasp/adamant_example/pkgs/container/adamant_example). This make take a few minutes and ~3 GB of disk space. By default the container created is named `adamant_example_container`. To change this, or the image that the container uses, modify `docker_config.sh` before running the commands below. 3. Next, tell Docker to create a new container from the [pre-built image](https://github.com/lasp/adamant_example/pkgs/container/adamant_example). This make take a few minutes and ~3 GB of disk space. By default the container created is named `adamant_example_container`. To change this, or the image that the container uses, modify `docker_config.sh` before running the commands below. ``` @@ -76,4 +77,4 @@ Next, you can create the Docker image by running: $ ./build_image.sh ``` -This may take several minutes to complete. By default, the image created is named `ghcr.io/lasp/adamant_example:latest`. To change this, modify `docker_config.sh` before running `./build_image.sh`. +This may take several minutes to complete. By default, the image created is named `ghcr.io/lasp/adamant_example:latest`. To change this, modify `docker_config.sh` before running `./adamant_env.sh build`. diff --git a/docker/adamant_env.sh b/docker/adamant_env.sh new file mode 100755 index 0000000..bdae9f1 --- /dev/null +++ b/docker/adamant_env.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +set +e + +if ! command -v docker &> /dev/null +then + if command -v podman &> /dev/null + then + function docker() { + podman $@ + } + else + echo "Neither docker nor podman found!!!" + exit 1 + fi +fi + +set -e + +. ${this_dir}/docker_config.sh + +DOCKER_COMPOSE_COMMAND="docker compose" +DOCKER_COMPOSE_CONFIG="${this_dir}/compose.yml" +export DOCKER_COMPOSE_COMMAND +${DOCKER_COMPOSE_COMMAND} version &> /dev/null +if [ "$?" -ne 0 ]; then + export DOCKER_COMPOSE_COMMAND="docker-compose" +fi + +usage() { + echo "Usage: $1 [start, stop, login, push, build, remove]" >&2 + echo "* start: create and start the adamant_example container" >&2 + echo "* stop: stop the running adamant_example container" >&2 + echo "* login: login to the adamant_example container" >&2 + echo "* push: push the image to the Docker registry" >&2 + echo "* build: build the image from the Dockerfile" >&2 + echo "* remove: remove network and volumes for adamant_example" >&2 + exit 1 +} + +case $1 in + start ) + execute "${DOCKER_COMPOSE_COMMAND} -f compose.yml up -d" + echo "" + echo "Run \"./adamant_env.sh login\" to log in." + ;; + stop ) + execute "${DOCKER_COMPOSE_COMMAND} -f compose.yml stop" + ;; + login ) + execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} exec -it -u user ${PROJECT_NAME} //bin//bash" + ;; + push ) + execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} push ${DOCKER_IMAGE_NAME}" + ;; + build ) + execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} build" + ;; + remove ) + if [ "$2" == "force" ] + then + execute "${DOCKER_COMPOSE_COMMAND} -f compose.yml down -t 30 -v" + else + echo "Are you sure? This removes ALL docker volumes and all Adamant Example data! (1-Yes / 2-No)" + select yn in "Yes" "No"; do + case $yn in + Yes ) execute "${DOCKER_COMPOSE_COMMAND} -f compose.yml down -t 30 -v"; break;; + No ) exit;; + esac + done + fi + ;; + * ) + usage $0 + ;; +esac diff --git a/docker/build_image.sh b/docker/build_image.sh deleted file mode 100755 index bdbf2d7..0000000 --- a/docker/build_image.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker build --progress=plain -t $DOCKER_IMAGE_NAME -f Dockerfile ." diff --git a/docker/compose.yml b/docker/compose.yml new file mode 100644 index 0000000..c53e324 --- /dev/null +++ b/docker/compose.yml @@ -0,0 +1,16 @@ +name: adamant_example +services: + adamant_example: + container_name: ${DOCKER_CONTAINER_NAME} + volumes: + - type: bind + source: ../../adamant + target: /home/user/adamant + - type: bind + source: ../../adamant_example + target: /home/user/adamant_example + extra_hosts: + - host.docker.internal:host-gateway + network_mode: host + image: ${DOCKER_IMAGE_NAME} + command: sleep infinity diff --git a/docker/create_container.sh b/docker/create_container.sh deleted file mode 100755 index 4412acc..0000000 --- a/docker/create_container.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -e - -# Create the docker container with a bind mount: -echo "Creating container..." -. ./docker_config.sh - -ON_LINUX="" -case "$OSTYPE" in - linux*) - ON_LINUX="--add-host=host.docker.internal:host-gateway" - ;; -esac - -execute "docker run -d \ - --name $DOCKER_CONTAINER_NAME \ - --network=\"host\" \ - --mount type=bind,source=\"$(pwd)\"/../../adamant,target=/home/user/adamant \ - --mount type=bind,source=\"$(pwd)\"/../../adamant_example,target=/home/user/adamant_example \ - $ON_LINUX \ - $DOCKER_IMAGE_NAME \ - sleep infinity" - -echo "" -echo "Run ./login_container.sh to log in." diff --git a/docker/docker_config.sh b/docker/docker_config.sh old mode 100755 new mode 100644 diff --git a/docker/login_container.sh b/docker/login_container.sh deleted file mode 100755 index ef9a42b..0000000 --- a/docker/login_container.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker exec -it -u user $DOCKER_CONTAINER_NAME //bin//bash" diff --git a/docker/push_image.sh b/docker/push_image.sh deleted file mode 100755 index e3d3750..0000000 --- a/docker/push_image.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker push $DOCKER_IMAGE_NAME" diff --git a/docker/remove_container.sh b/docker/remove_container.sh deleted file mode 100755 index 054bb1f..0000000 --- a/docker/remove_container.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker rm $DOCKER_CONTAINER_NAME" diff --git a/docker/start_container.sh b/docker/start_container.sh deleted file mode 100755 index 9867d6e..0000000 --- a/docker/start_container.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker start $DOCKER_CONTAINER_NAME" diff --git a/docker/stop_container.sh b/docker/stop_container.sh deleted file mode 100755 index 8260066..0000000 --- a/docker/stop_container.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker stop $DOCKER_CONTAINER_NAME"