From 8885fe3fa61a457a189aa4d1dffadd448e12aa9a Mon Sep 17 00:00:00 2001 From: Jacob Seman Date: Tue, 18 Jun 2024 19:03:04 -0600 Subject: [PATCH] Replace container management scripts with singular `adamant_env.sh` script Fix to make `adamant_env.sh` executable Apply fix to README.md Rename compose.yaml->compose.yml --- docker/Dockerfile | 2 +- docker/README.md | 16 ++++---- docker/adamant_env.sh | 84 ++++++++++++++++++++++++++++++++++++++ docker/build_image.sh | 4 -- docker/compose.yml | 13 ++++++ docker/create_container.sh | 24 ----------- docker/docker_config.sh | 12 ------ 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 -- 12 files changed, 106 insertions(+), 69 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 delete mode 100755 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/Dockerfile b/docker/Dockerfile index d6aeeb53..4cd77175 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,7 +1,7 @@ # # $ docker build -t $DOCKER_IMAGE_NAME -f Dockerfile . # -# For best results use the ./build_image.sh and ./create_container.sh scripts +# For best results use the ./adamant_env.sh script with the `build` and `start` arguments # provided in this directory. # FROM ubuntu:24.04 AS base diff --git a/docker/README.md b/docker/README.md index 0c5bedaf..b22b634c 100644 --- a/docker/README.md +++ b/docker/README.md @@ -17,17 +17,17 @@ 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/pkgs/container/adamant). This make take a few minutes and ~3 GB of disk space. By default, the container created is named `adamant_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/pkgs/container/adamant). This make take a few minutes and ~3 GB of disk space. By default, the container created is named `adamant_container`. To change this, or the image that the container uses, modify `compose.yml` before running the commands below. ``` $ cd adamant/docker - $ ./create_container.sh + $ ./adamant_env.sh start ``` 4. Finally, you can log into the container by running. ``` - $ ./login_container.sh + $ ./adamant_env.sh login ``` The first time you log in, the environment will be set up automatically. This can take a few minutes. Note that the `adamant/` directory on your host is shared with the docker container at `~/adamant/`. This allows you to modify files on your host and compile those same files on the container. @@ -37,13 +37,13 @@ The first time you log in, the environment will be set up automatically. This ca Once you have created a container using the section above, you can stop it by running. ``` - $ ./stop_container.sh + $ ./adamant_env.sh stop ``` To start the container up again, run: ``` - $ ./start_container.sh + $ ./adamant_env.sh start ``` ## Testing the Environment @@ -51,7 +51,7 @@ To start the container up again, run: We can make sure the environment is set up correctly by running a component unit test. Below, we login to the container and run the unit test for the Command Router. ``` - $ ./login_container.sh + $ ./adamant_env.sh login ``` From within the container run: @@ -74,7 +74,7 @@ Next, you can create the Docker image by running: ``` $ cd adamant/docker - $ ./build_image.sh + $ ./adamant_env.sh build ``` -This may take several minutes complete. By default, the image created is named `ghcr.io/lasp/adamant:latest`. To change this, modify `docker_config.sh` before running `./build_image.sh`. +This may take several minutes complete. By default, the image created is named `ghcr.io/lasp/adamant:latest`. To change this, modify `compose.yml` before running `./adamant_env.sh build`. diff --git a/docker/adamant_env.sh b/docker/adamant_env.sh new file mode 100755 index 00000000..a85a2544 --- /dev/null +++ b/docker/adamant_env.sh @@ -0,0 +1,84 @@ +#!/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 + +DOCKER_CONTAINER_NAME="adamant_container" +DOCKER_IMAGE_NAME="ghcr.io/lasp/adamant:latest" +DOCKER_COMPOSE_COMMAND="docker compose" +export DOCKER_CONTAINER_NAME +export DOCKER_IMAGE_NAME +export DOCKER_COMPOSE_COMMAND +${DOCKER_COMPOSE_COMMAND} version &> /dev/null +if [ "$?" -ne 0 ]; then + export DOCKER_COMPOSE_COMMAND="docker-compose" +fi + +# Helper function to print out command as executed: +execute () { + echo "$ $@" + eval "$@" +} + +set -e + +usage() { + echo "Usage: $1 [start, stop, login, push, build, remove]" >&2 + echo "* start: create and start the adamant container" >&2 + echo "* stop: stop the running adamant container" >&2 + echo "* login: login to the adamant 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" >&2 + exit 1 +} + +if [ "$#" -eq 0 ]; then + usage $0 +fi + +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 exec -it -u user ${DOCKER_CONTAINER_NAME} //bin//bash" + ;; + push ) + execute "docker push ${DOCKER_IMAGE_NAME}" + ;; + build ) + execute "docker build --progress=plain -t ${DOCKER_IMAGE_NAME} -f Dockerfile ." + ;; + 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 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 + ;; +esac diff --git a/docker/build_image.sh b/docker/build_image.sh deleted file mode 100755 index bdbf2d7f..00000000 --- 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 00000000..86d85d3d --- /dev/null +++ b/docker/compose.yml @@ -0,0 +1,13 @@ +name: adamant +services: + adamant: + container_name: adamant_container + volumes: + - type: bind + source: ../../adamant + target: /home/user/adamant + extra_hosts: + - host.docker.internal:host-gateway + network_mode: host + image: ghcr.io/lasp/adamant:latest + command: sleep infinity diff --git a/docker/create_container.sh b/docker/create_container.sh deleted file mode 100755 index a8e02a3a..00000000 --- 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 \ - --mount type=bind,source=\"$(pwd)\"/../../adamant,target=/home/user/adamant \ - $ON_LINUX \ - $DOCKER_IMAGE_NAME \ - sleep infinity" - -echo "Finished creating container \"$DOCKER_CONTAINER_NAME\"." - -echo "" -echo "Run ./login_container.sh to log in." diff --git a/docker/docker_config.sh b/docker/docker_config.sh deleted file mode 100755 index 05e41f08..00000000 --- a/docker/docker_config.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh -e - -DOCKER_CONTAINER_NAME="adamant_container" -DOCKER_IMAGE_NAME="ghcr.io/lasp/adamant:latest" -export DOCKER_CONTAINER_NAME -export DOCKER_IMAGE_NAME - -# Helper function to print out command as executed: -execute () { - echo "$ $@" - eval "$@" -} diff --git a/docker/login_container.sh b/docker/login_container.sh deleted file mode 100755 index ef9a42bf..00000000 --- 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 e3d3750c..00000000 --- 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 054bb1f2..00000000 --- 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 9867d6e1..00000000 --- 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 82600669..00000000 --- a/docker/stop_container.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e - -. ./docker_config.sh -execute "docker stop $DOCKER_CONTAINER_NAME"