Skip to content

Commit

Permalink
docker improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
istudyatuni committed Jun 12, 2024
1 parent 454f064 commit 9d0d80c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env:
IMAGE_NAME: ghcr.io/istudyatuni/kotync
IMAGE_TAG: ${{ inputs.tag }}
ORIGINAL_TAG: ${{ inputs.tag }}-original
MYSQL_TAG: ${{ inputs.tag }}-mysql

jobs:
docker:
Expand All @@ -31,6 +32,7 @@ jobs:
run: |
docker build . --tag $IMAGE_NAME:$IMAGE_TAG
docker build . --tag $IMAGE_NAME:$ORIGINAL_TAG --build-arg kind=original
docker build . --tag $IMAGE_NAME:$MYSQL_TAG --build-arg kind=mysql
- name: Login to ghcr
uses: docker/login-action@v3
with:
Expand All @@ -40,4 +42,5 @@ jobs:
- name: Push docker
run: |
docker push $IMAGE_NAME:$ORIGINAL_TAG
docker push $IMAGE_NAME:$MYSQL_TAG
docker push $IMAGE_NAME:$IMAGE_TAG
14 changes: 10 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# new, original
# new, original, mysql
ARG kind=new

# ----- build ----- #
Expand All @@ -12,6 +12,9 @@ ENV FEATURES=new
FROM builder-base AS builder-original
ENV FEATURES=original

FROM builder-base AS builder-mysql
ENV FEATURES=mysql

FROM builder-${kind} AS builder

# install even unnecessary deps for better caching
Expand Down Expand Up @@ -44,11 +47,14 @@ ENV DEPS=sqlite-libs
FROM run-base AS run-original
ENV DEPS=mariadb-connector-c

FROM run-${kind}
FROM run-base AS run-mysql
ENV DEPS=mariadb-connector-c

FROM run-${kind} AS run
RUN apk add --no-cache libgcc ${DEPS}

FROM run
EXPOSE 8080:8080
WORKDIR /app
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/kotync .

EXPOSE 8080:8080
ENTRYPOINT ["/app/kotync"]
24 changes: 22 additions & 2 deletions docs/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ services:
MARIADB_DATABASE: kotatsu_db
MARIADB_ROOT_PASSWORD: ROOT_PASSWORD
volumes:
- /var/lib/mysql:/var/lib/mariadb
- /var/lib/mysql:/var/lib/mysql
# socket
- /run/mysqld:/run/mysqld
expose:
- 3306
networks:
internal:
ipv4_address: 172.20.0.2

# server with mysql
server-original:
server-mysql:
# if you do not need to work with original database, change "-original" to "-mysql"
image: ghcr.io/istudyatuni/kotync:dev-original
container_name: kotync-original
depends_on:
Expand All @@ -30,6 +36,11 @@ services:
# RUST_LOG: info
ports:
- 8081:8080
restart: always
networks:
internal:
# to be able to tell mariadb IP of client
ipv4_address: 172.20.0.3

# server with sqlite
server:
Expand All @@ -45,3 +56,12 @@ services:
volumes:
# change ${PWD} to directory, where you want to save data.db
- ${PWD}:/app/data
restart: always

# https://stackoverflow.com/a/39498534
networks:
internal:
ipam:
config:
- subnet: 172.20.0.0/16
gateway: 172.20.0.1
17 changes: 14 additions & 3 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ After that fill environment variables inside, and run:
docker compose up -d server

# Work on MySQL database from original server
docker compose up -d server-original
docker compose up -d server-mysql
```

### With docker
Expand Down Expand Up @@ -44,9 +44,11 @@ docker run -d -p 8081:8080 \
--name kotync ghcr.io/istudyatuni/kotync:dev-original
```

#### Note on MySQL
### Note on MySQL

When running with docker (not docker compose), you should allow your mysql user to connect from `172.17.0.2`\*. To do this, you can run (via `mysql` cli under root user):
*_When using `docker`_*

You should allow your mysql user to connect from `172.17.0.2`\*. To do this, connect to mysql under root user (via `sudo mysql`), and execute:

```sql
CREATE USER 'some_user'@'172.17.0.%' IDENTIFIED BY 'some_password';
Expand All @@ -57,6 +59,15 @@ GRANT ALL PRIVILEGES ON kotatsu_db.* TO 'some_user'@'172.17.0.%';

After that, set `DATABASE_HOST=172.17.0.1`.

*_When using `docker compose`_*

MySQL user is created on startup with all permissions, if you want to set more granular permissions, you should allow user to connect from within docker network. To do this, first run `docker compose up -d db`, then connect to mariadb under root user (via `sudo mysql` or `sudo mariadb`), and execute:

```sql
CREATE USER 'some_user'@'172.20.0.3' IDENTIFIED BY 'some_password';
GRANT ALL PRIVILEGES ON kotatsu_db.* TO 'some_user'@'172.20.0.3';
```

See more details [here](https://stackoverflow.com/a/44544841).

## Building
Expand Down

0 comments on commit 9d0d80c

Please sign in to comment.