Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Compose Conversion #25

Merged
merged 9 commits into from
Jun 24, 2024
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 ghcr.io/lasp/adamant:latest as adamant_base
Expand Down
16 changes: 8 additions & 8 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,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_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-compose.yml` before running the commands below.

```
$ cd adamant_example/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_example/` and `adamant/` directories on your host will be shared with the new Docker container at `~/adamant_example/` and `~/adamant/`. This allows you to modify files on your host and compile those same files on the container.
Expand All @@ -36,21 +36,21 @@ 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
```

## Running the Example Project

To build and run the example project (for Linux) we need to first log in to the container.

```
$ ./login_container.sh
$ ./adamant_env.sh login
```

From within the container run:
Expand All @@ -73,7 +73,7 @@ Next, you can create the Docker image by running:

```
$ cd adamant_example/docker
$ ./build_image.sh
$ ./adamant_env.sh build
```

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-compose.yml` before running `./adamant_env.sh build`.
90 changes: 90 additions & 0 deletions docker/adamant_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

this_dir=`readlink -f "${BASH_SOURCE[0]}" | xargs dirname`

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

PROJECT_NAME=${this_dir%/*}
PROJECT_NAME=${PROJECT_NAME##*/}
DOCKER_COMPOSE_COMMAND="docker compose"
DOCKER_COMPOSE_CONFIG="${this_dir}/docker-compose.yml"
export PROJECT_NAME
export DOCKER_COMPOSE_COMMAND
export DOCKER_COMPOSE_CONFIG
${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 "$@"
}

usage() {
echo "Usage: $1 [start, stop, login, push, build, remove]" >&2
echo "* start: create and start the ${PROJECT_NAME} container" >&2
echo "* stop: stop the running ${PROJECT_NAME} container" >&2
echo "* login: login to the ${PROJECT_NAME} 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 ${PROJECT_NAME}" >&2
exit 1
}

case $1 in
start )
execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} up --pull \"missing\" -d"
execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} exec ${PROJECT_NAME} bash -c \"\
if [ ! -f /home/user/.initialized ]; \
then source /home/user/${PROJECT_NAME}/env/activate && touch /home/user/.initialized; \
fi;\""
echo ""
echo "Run \"./adamant_env.sh login\" to log in."
;;
stop )
execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} 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"
;;
build )
execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} build"
;;
remove )
if [ "$2" == "force" ]
then
execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} down -t 30 -v"
else
echo "Are you sure? This removes ALL docker volumes and all ${PROJECT_NAME} data! (1-Yes / 2-No)"
select yn in "Yes" "No"; do
case $yn in
Yes ) execute "${DOCKER_COMPOSE_COMMAND} -f ${DOCKER_COMPOSE_CONFIG} down -t 30 -v"; break;;
No ) exit;;
esac
done
fi
;;
* )
usage $0
;;
esac
4 changes: 0 additions & 4 deletions docker/build_image.sh

This file was deleted.

24 changes: 0 additions & 24 deletions docker/create_container.sh

This file was deleted.

18 changes: 18 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: adamant_example
services:
adamant_example:
image: ghcr.io/lasp/adamant_example:latest
build:
context: .
container_name: adamant_example_container
volumes:
- type: bind
source: ../../adamant
target: /home/user/adamant
- type: bind
source: ../../adamant_example
target: /home/user/adamant_example
network_mode: host
extra_hosts:
- host.docker.internal:host-gateway
command: sleep infinity
12 changes: 0 additions & 12 deletions docker/docker_config.sh

This file was deleted.

4 changes: 0 additions & 4 deletions docker/login_container.sh

This file was deleted.

4 changes: 0 additions & 4 deletions docker/push_image.sh

This file was deleted.

4 changes: 0 additions & 4 deletions docker/remove_container.sh

This file was deleted.

4 changes: 0 additions & 4 deletions docker/start_container.sh

This file was deleted.

4 changes: 0 additions & 4 deletions docker/stop_container.sh

This file was deleted.