Skip to content

Commit

Permalink
Merge pull request #50 from Jbsco/docker-compose
Browse files Browse the repository at this point in the history
Docker Compose Conversion
  • Loading branch information
dinkelk authored Jun 24, 2024
2 parents 23f01af + eab1d33 commit 4a6c280
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 69 deletions.
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 ubuntu:24.04 AS base
Expand Down
16 changes: 8 additions & 8 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `docker-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.
Expand All @@ -37,21 +37,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
```

## Testing the Environment

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:
Expand All @@ -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 `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.

15 changes: 15 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: adamant
services:
adamant:
image: ghcr.io/lasp/adamant:latest
build:
context: .
container_name: adamant_container
volumes:
- type: bind
source: ../../adamant
target: /home/user/adamant
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.

0 comments on commit 4a6c280

Please sign in to comment.