Skip to content

Commit

Permalink
[18938] Run with Docker Compose and remote images (#76)
Browse files Browse the repository at this point in the history
* fix: local docker

* chore: use mock service image from ecr

* chore: add script

* chore: add make command

* chore: update script to support docker compose file from cdn

* docs: add documentation on docker compose

* chore: add newline
  • Loading branch information
peschina authored Jul 11, 2024
1 parent 33f0db3 commit 53b7ab5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
40 changes: 40 additions & 0 deletions README.OGCIO.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,46 @@ e.g. `git merge v1.17.0 --strategy-option theirs`
- commit the changes with `git commit -a` to end the merge and let git write the correct message
- push and open your PR!

## Run with Docker Compose
It is possible to run Logto, its database and our MyGovId mock service in a dockerized solution, with local or remote images.

### With local images
If you have the repository on your machine and want to use images built locally for both services you can run:
```
make build run
```

### With remote images
If you want to run Logto on your machine without cloning the repo, you need to have access to aws to pull our images as a prerequisite. If you haven't already, install [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
If not yet configured, run
```
aws sso configure
```
And follow the prompts. If you don't know what your SSO start URL is, you can find it on your AWS access portal. Click on your AWS account and then on the `Access keys` option.

A script is available to login with AWS and Docker, create the custom network and run the containers. This is useful when launching it for the first time, or more in general when the image needs to be pulled.
The script expects an environment variable for the aws profile that you need to be logged in:
```
AWS_PROFILE=awsProfile-accountId
```

To execute the script, run:
```
[ ! -f docker-compose-ogcio-logto.yml ] && curl -fsSL https://raw.githubusercontent.com/ogcio/logto/HEAD/docker-compose-ogcio-logto.yml > /tmp/docker-compose-ogcio-logto.yml && curl -fsSL https://raw.githubusercontent.com/ogcio/logto/18938/automate-with-script/run-logto-remote.sh | bash -s /tmp/docker-compose-ogcio-logto.yml
```
The command downloads the Docker Compose file from Github to a temporary location if it doesn't exist already, then fetches and executes the script from GitHub, passing the temporary Docker Compose file.

If you are already authenticated and just want to run the docker compose:

```
curl -fsSL https://raw.githubusercontent.com/ogcio/logto/HEAD/docker-compose-ogcio-logto.yml | docker compose -f - up -d
```

If you already have the repo cloned locally there is a Make command available:
```
make build run-remote
```

## Setup and run Logto natively

You can also run Logto natively on your machine outside the docker container.
Expand Down
7 changes: 7 additions & 0 deletions docker-compose-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ services:
- ADMIN_ENDPOINT
- PORT=3301
- ADMIN_PORT=3302
- MOCK_TOKEN_ENDPOINT=http://mygovid-mock-service:4005/logto/mock/token
- MOCK_KEYS_ENDPOINT=http://mygovid-mock-service:4005/logto/mock/keys

postgres:
extends:
file: docker-compose-db.yaml
Expand All @@ -31,3 +34,7 @@ services:
volumes:
db:
driver: local

networks:
logto_network:
external: true
2 changes: 1 addition & 1 deletion docker-compose-ogcio-logto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:
- 5433:5433

mygovid-mock-service:
image: mygovid-mock-service:latest
image: 730335224023.dkr.ecr.eu-west-1.amazonaws.com/life-events-logto@sha256:46fbb0c0bbaa43285554aac09962f9a5bb77c1b98dff1c3374c2c158b978c7f4
build:
dockerfile: ./mygovid-mock-service/Dockerfile
ports:
Expand Down
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ run-native:
@echo "${GREEN}Connectors ready!${NC}"
@echo "${GREEN}Starting Logto...${NC}"
pnpm dev
run-remote:
./run-logto-remote.sh
80 changes: 80 additions & 0 deletions run-logto-remote.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

if [ -z "$AWS_PROFILE" ]; then
echo "AWS_PROFILE environment variable is not set. Exiting..."
exit 1
fi

print_in_green() {
GREEN='\033[0;32m'
NC='\033[0m'
echo -e "${GREEN}$1${NC}"
}

print_in_blue() {
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}$1${NC}"
}

print_in_red() {
RED='\033[0;31m'
NC='\033[0m'
echo -e "${RED}$1${NC}"
}

DOCKER_COMPOSE_FILE=${1:-docker-compose-ogcio-logto.yml}

if [ ! -f "$DOCKER_COMPOSE_FILE" ]; then
echo "Docker Compose file not found!"
exit 1
fi

aws_ecr_login() {
print_in_blue "Logging in to AWS ECR..."
aws sso login --profile $AWS_PROFILE

if [ $? -ne 0 ]; then
print_in_red "AWS SSO login failed. Exiting..."
exit 1
fi

aws ecr get-login-password --region eu-west-1 --profile $AWS_PROFILE | docker login --username AWS --password-stdin 730335224023.dkr.ecr.eu-west-1.amazonaws.com

if [ $? -ne 0 ]; then
print_in_red "Docker login to ECR failed. Exiting..."
exit 1
fi
}

create_docker_network() {
NETWORK_NAME="logto_network"
if ! docker network ls | grep -q "$NETWORK_NAME"; then
print_in_blue "Creating Docker network '$NETWORK_NAME'..."
docker network create $NETWORK_NAME
if [ $? -ne 0 ]; then
print_in_red "Failed to create Docker network '$NETWORK_NAME'. Exiting..."
exit 1
fi
else
print_in_blue "Docker network '$NETWORK_NAME' already exists."
fi
}

docker-compose -f "$DOCKER_COMPOSE_FILE" down

create_docker_network

docker-compose -f "$DOCKER_COMPOSE_FILE" up --detach
if [ $? -ne 0 ]; then
print_in_blue "docker-compose up failed. Trying to log in to AWS ECR..."
aws_ecr_login
print_in_blue "Retrying docker-compose up..."
docker-compose -f "$DOCKER_COMPOSE_FILE" up --detach
if [ $? -ne 0 ]; then
print_in_red "docker-compose up failed again. Exiting..."
exit 1
fi
fi

print_in_green "docker-compose up succeeded."

0 comments on commit 53b7ab5

Please sign in to comment.