Skip to content

Commit

Permalink
dev: more docker work
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 25, 2023
1 parent b211008 commit 26dc027
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
/project-words.txt
/README.md
/rustfmt.toml
/storage/
/lib/torrust/
/target/
6 changes: 3 additions & 3 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DATABASE_URL=sqlite://storage/database/data.db?mode=rwc
TORRUST_IDX_BACK_CONFIG=
TORRUST_IDX_BACK_USER_UID=1000
DATABASE_URL=sqlite:///var/lib/torrust/database/data.db?mode=rwc
TORRUST_INDEX_CONFIG=
USER_UID=1000
TORRUST_TRACKER_CONFIG=
TORRUST_TRACKER_USER_UID=1000
TORRUST_TRACKER_API_TOKEN=MyAccessToken
76 changes: 59 additions & 17 deletions .github/workflows/container.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,92 @@ on:
env:
CARGO_TERM_COLOR: always


jobs:
test:
name: Test
name: Test Release
runs-on: ubuntu-latest

steps:
- id: setup
name: Setup Toolchain
uses: docker/setup-buildx-action@v2

- id: build_index
name: Build
uses: docker/build-push-action@v4
with:
push: false
load: true
target: release
tags: torrust-index-backend:release
cache-from: type=gha
cache-to: type=gha,mode=max

integration:
name: Integration
runs-on: ubuntu-latest

steps:
- id: checkout
name: Checkout Repository
- id: checkout_index
name: Checkout Repository (Index)
uses: actions/checkout@v3
with:
path: index

- id: checkout_tracker
name: Checkout Repository (Tracker)
uses: actions/checkout@v3
with:
repository: torrust/torrust-tracker
path: tracker

- id: setup
name: Setup Toolchain
uses: docker/setup-buildx-action@v2

- id: build
- id: build_index
name: Build
uses: docker/build-push-action@v4
with:
context: index
push: false
load: true
tags: torrust-index-backend:local
target: debug
tags: torrust-index-backend:debug
cache-from: type=gha
cache-to: type=gha,mode=max

- id: inspect
name: Inspect
run: docker image inspect torrust-index-backend:local
- id: build_tracker
name: Build
uses: docker/build-push-action@v4
with:
context: index
push: false
load: true
target: debug
tags: torrust-tracker:debug
cache-from: type=gha
cache-to: type=gha,mode=max

- id: inspect_index
name: Inspect Index
run: docker image inspect torrust-index-backend:debug

- id: inspect_tracker
name: Inspect Tracker
run: docker image inspect torrust-tracker:debug

- id: compose
name: Compose Applications
run: |
export TORRUST_IDX_BACK_CONFIG=$(cat config-index.mysql.local.toml)
export TORRUST_TRACKER_CONFIG=$(cat config-tracker.local.toml)
docker compose build \
--build-arg TORRUST_IDX_BACK_CONFIG="$TORRUST_IDX_BACK_CONFIG" \
--build-arg TORRUST_TRACKER_CONFIG="$TORRUST_TRACKER_CONFIG"
run: docker compose build

- id: run
name: Run Applications

run: |
export TORRUST_IDX_BACK_CONFIG=$(cat config-index.mysql.local.toml)
export TORRUST_TRACKER_CONFIG=$(cat config-tracker.local.toml)
export TORRUST_INDEX_CONFIG=$(cat index/config-index.mysql.local.toml)
export TORRUST_TRACKER_CONFIG=$(cat index/config-tracker.local.toml)
docker compose up --detach
Expand Down
35 changes: 12 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,47 +95,36 @@ RUN chmod -R u=rw,go=r,a+X /app
RUN chmod -R a+x /app/bin


## Torrust-Index-Backend (debug)
FROM gcr.io/distroless/cc:debug as index_backend_debug

## Runtime
FROM gcr.io/distroless/cc:debug as Runtime
RUN ["/busybox/cp", "-sp", "/busybox/sh", "/bin/sh"]
ENV ENV=/etc/profile

ARG USER_ID=1000
ARG USER_NAME=appuser
ARG API_PORT=3001

ENV USER_ID=${USER_ID}
ENV USER_NAME=${USER_NAME}
ENV API_PORT=${API_PORT}
ENV TZ=Etc/UTC

EXPOSE ${API_PORT}/tcp

COPY --from=test_debug /app/ /usr/
WORKDIR /home/torrust
RUN adduser --disabled-password --uid "${USER_ID}" "torrust"
RUN mkdir -p /var/lib/torrust; chown -R "${USER_ID}":"${USER_ID}" /var/lib/torrust; chmod -R 2775 /var/lib/torrust

ENV ENV=/etc/profile
COPY ./docker/motd.debug /etc/motd

RUN echo '[ ! -z "$TERM" -a -r /etc/motd ] && cat /etc/motd' >> /etc/profile
USER "torrust":"torrust"

WORKDIR /home/${USER_NAME}
RUN adduser --disabled-password --uid "${USER_ID}" "${USER_NAME}"
USER "${USER_NAME}":"${USER_NAME}"

## Torrust-Index-Backend (debug)
FROM runtime as debug
COPY --from=test_debug /app/ /usr/
RUN env

## Torrust-Index-Backend (release) (default)
FROM gcr.io/distroless/cc:nonroot as index_backend
COPY --from=gcr.io/distroless/cc:debug /busybox/wget /usr/bin/wget
FROM runtime as release
COPY --from=test /app/ /usr/

ARG API_PORT=3001

ENV API_PORT=${API_PORT}
ENV TZ=Etc/UTC

EXPOSE ${API_PORT}/tcp

HEALTHCHECK CMD ["/usr/bin/wget", "--no-verbose", "--tries=1", "--spider", "localhost:${API_PORT}"]

HEALTHCHECK CMD ["/busybox/wget", "--no-verbose", "--tries=1", "--spider", "localhost:${API_PORT}"]
CMD ["/usr/bin/torrust-index-backend"]
2 changes: 1 addition & 1 deletion bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if ! [ -f "./config.toml" ]; then
cp ./config-index.local.toml ./config.toml
fi

# Generate storage directory if it does not exist
# Generate lib/torrust directory if it does not exist
mkdir -p "./storage/database"

# Generate the sqlite database for the index backend if it does not exist
Expand Down
18 changes: 10 additions & 8 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
name: torrust
services:

index-back:
image: torrust-index-backend:local
index:
image: torrust-index-backend:debug
entrypoint: torrust-index-backend
tty: true
environment:
- TORRUST_IDX_BACK_CONFIG=${TORRUST_IDX_BACK_CONFIG}
- TORRUST_IDX_BACK_CORS_PERMISSIVE=true
- TORRUST_INDEX_CONFIG=${TORRUST_INDEX_CONFIG}
- TORRUST_INDEX_CORS_PERMISSIVE=true
- CARGO_HOME=/home/appuser/.cargo
networks:
- server_side
ports:
- 3001:3001
volumes:
- ./storage:/app/storage
- ./lib/torrust:/var/lib/torrust
depends_on:
- tracker
- mailcatcher
- mysql

tracker:
image: torrust/tracker:develop
image: torrust-tracker:debug
entrypoint: torrust-tracker
tty: true
environment:
- TORRUST_TRACKER_CONFIG=${TORRUST_TRACKER_CONFIG}
Expand All @@ -31,7 +33,7 @@ services:
- 6969:6969/udp
- 1212:1212/tcp
volumes:
- ./storage:/app/storage
- ./lib/torrust:/var/lib/torrust
depends_on:
- mysql

Expand All @@ -58,7 +60,7 @@ services:
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=root_secret_password
- MYSQL_DATABASE=${TORRUST_IDX_BACK_MYSQL_DATABASE:-torrust_index_backend_e2e_testing}
- MYSQL_DATABASE=${TORRUST_INDEX_MYSQL_DATABASE:-torrust_index_backend_e2e_testing}
- MYSQL_USER=db_user
- MYSQL_PASSWORD=db_user_secret_password
networks:
Expand Down
2 changes: 1 addition & 1 deletion config-index.sqlite.local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ max_password_length = 64
secret_key = "MaxVerstappenWC2021"

[database]
connect_url = "sqlite://storage/database/torrust_index_backend_e2e_testing.db?mode=rwc"
connect_url = "sqlite:///var/lib/torrust/database/torrust_index_backend_e2e_testing.db?mode=rwc"

[mail]
email_verification_enabled = false
Expand Down
2 changes: 1 addition & 1 deletion config-tracker.local.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
log_level = "info"
mode = "public"
db_driver = "Sqlite3"
db_path = "./storage/database/torrust_tracker_e2e_testing.db"
db_path = "/var/lib/torrust/database/torrust_tracker_e2e_testing.db"
announce_interval = 120
min_announce_interval = 120
max_peer_timeout = 900
Expand Down
24 changes: 12 additions & 12 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## Requirements

- Docker version 20.10.21
- You need to create the `storage` directory with this structure and files:
- You need to create the `lib/torrust` directory with this structure and files:

```s
$ tree storage/
storage/
$ tree lib/torrust/
lib/torrust/
└── database
  ├── data.db
   └── tracker.db
Expand All @@ -21,20 +21,20 @@ Build and run locally:

```s
docker context use default
export TORRUST_IDX_BACK_USER_UID=$(id -u)
./docker/bin/build.sh $TORRUST_IDX_BACK_USER_UID
export USER_UID=$(id -u)
./docker/bin/build.sh $USER_UID
./bin/install.sh
./docker/bin/run.sh $TORRUST_IDX_BACK_USER_UID
./docker/bin/run.sh $USER_UID
```

Run using the pre-built public docker image:

```s
export TORRUST_IDX_BACK_USER_UID=$(id -u)
export USER_UID=$(id -u)
docker run -it \
--user="$TORRUST_IDX_BACK_USER_UID" \
--user="$USER_UID" \
--publish 3001:3001/tcp \
--volume "$(pwd)/storage":"/app/storage" \
--volume "$(pwd)/lib/torrust":"/var/lib/torrust" \
torrust/index-backend
```

Expand All @@ -49,7 +49,7 @@ docker run -it \
The docker-compose configuration includes the MySQL service configuration. If you want to use MySQL instead of SQLite you have to change your `config.toml` or `config-idx-back.local.toml` configuration from:

```toml
connect_url = "sqlite://storage/database/data.db?mode=rwc"
connect_url = "sqlite:///var/lib/torrust/database/data.db?mode=rwc"
```

to:
Expand All @@ -63,8 +63,8 @@ If you want to inject an environment variable into docker-compose you can use th
Build and run it locally:

```s
TORRUST_IDX_BACK_USER_UID=${TORRUST_IDX_BACK_USER_UID:-1000} \
TORRUST_IDX_BACK_CONFIG=$(cat config-index.mysql.local.toml) \
USER_UID=${USER_UID:-1000} \
TORRUST_INDEX_CONFIG=$(cat config-index.mysql.local.toml) \
TORRUST_TRACKER_CONFIG=$(cat config-tracker.local.toml) \
TORRUST_TRACKER_API_TOKEN=${TORRUST_TRACKER_API_TOKEN:-MyAccessToken} \
docker compose up -d --build
Expand Down
8 changes: 4 additions & 4 deletions docker/bin/build.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

TORRUST_IDX_BACK_USER_UID=${TORRUST_IDX_BACK_USER_UID:-1000}
TORRUST_INDEX_USER_UID=${TORRUST_INDEX_USER_UID:-1000}
TORRUST_IDX_BACK_RUN_AS_USER=${TORRUST_IDX_BACK_RUN_AS_USER:-appuser}

echo "Building docker image ..."
echo "TORRUST_IDX_BACK_USER_UID: $TORRUST_IDX_BACK_USER_UID"
echo "TORRUST_INDEX_USER_UID: $TORRUST_INDEX_USER_UID"
echo "TORRUST_IDX_BACK_RUN_AS_USER: $TORRUST_IDX_BACK_RUN_AS_USER"

docker build \
--build-arg UID="$TORRUST_IDX_BACK_USER_UID" \
--build-arg UID="$TORRUST_INDEX_USER_UID" \
--build-arg RUN_AS_USER="$TORRUST_IDX_BACK_RUN_AS_USER" \
--tag torrust-index-backend:local .
--target debug --tag torrust-index-backend:debug .
2 changes: 1 addition & 1 deletion docker/bin/e2e/mysql/e2e-env-reset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "DROP DATABASE IF EXIST
# Delete tracker database
rm -f ./storage/database/torrust_tracker_e2e_testing.db

# Generate storage directory if it does not exist
# Generate lib/torrust directory if it does not exist
mkdir -p "./storage/database"

# Generate the sqlite database for the tracker if it does not exist
Expand Down
8 changes: 4 additions & 4 deletions docker/bin/e2e/mysql/e2e-env-up.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash

TORRUST_IDX_BACK_USER_UID=${TORRUST_IDX_BACK_USER_UID:-1000} \
USER_UID=${USER_UID:-1000} \
docker compose build

TORRUST_IDX_BACK_USER_UID=${TORRUST_IDX_BACK_USER_UID:-1000} \
TORRUST_IDX_BACK_CONFIG=$(cat config-index.mysql.local.toml) \
TORRUST_IDX_BACK_MYSQL_DATABASE="torrust_index_backend_e2e_testing" \
USER_UID=${USER_UID:-1000} \
TORRUST_INDEX_CONFIG=$(cat config-index.mysql.local.toml) \
TORRUST_INDEX_MYSQL_DATABASE="torrust_index_backend_e2e_testing" \
TORRUST_TRACKER_CONFIG=$(cat config-tracker.local.toml) \
TORRUST_TRACKER_API_TOKEN=${TORRUST_TRACKER_API_TOKEN:-MyAccessToken} \
docker compose up -d
Expand Down
7 changes: 4 additions & 3 deletions docker/bin/e2e/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ CURRENT_USER_ID=$(id -u)
echo "User name: $CURRENT_USER_NAME"
echo "User id: $CURRENT_USER_ID"

TORRUST_IDX_BACK_USER_UID=$CURRENT_USER_ID
TORRUST_INDEX_USER_UID=$CURRENT_USER_ID
TORRUST_TRACKER_USER_UID=$CURRENT_USER_ID
export TORRUST_IDX_BACK_USER_UID
export TORRUST_INDEX_USER_UID
export TORRUST_TRACKER_USER_UID

wait_for_container_to_be_healthy() {
Expand Down Expand Up @@ -37,7 +37,8 @@ wait_for_container_to_be_healthy() {

# Install tool to create torrent files.
# It's needed by some tests to generate and parse test torrent files.
cargo install imdl || exit 1
cargo install cargo-binstall || exit 1
cargo binstall imdl || exit 1

# Install app (no docker) that will run the test suite against the E2E testing
# environment (in docker).
Expand Down
Loading

0 comments on commit 26dc027

Please sign in to comment.