From eae16009983fca18753e4a44d8bb4c69c9d57437 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Sun, 20 Aug 2023 18:44:16 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[WIP]=20docker=E5=91=A8=E3=82=8A=E3=81=AE?= =?UTF-8?q?=E3=82=A2=E3=83=83=E3=83=97=E3=83=87=E3=83=BC=E3=83=88=20sqlx?= =?UTF-8?q?=20db=20create=E3=81=A7error=20communicating=20with=20database:?= =?UTF-8?q?=20Cannot=20assign=20requested=20address=20(os=20error=2099)?= =?UTF-8?q?=E3=81=A8=E6=80=92=E3=82=89=E3=82=8C=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 35 +++++++++++++++++++++++++++++++++++ docker-compose.yml | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fb7eac2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM rust:1.71.1 + +ARG DATABASE_URL + +ENV CARGO_TARGET_DIR=/tmp/target \ + DEBIAN_FRONTEND=noninteractive \ + LC_CTYPE=ja_JP.utf8 \ + LANG=ja_JP.utf8 \ + DATABASE_URL=${DATABASE_URL} + +RUN apt-get update \ + && apt-get install -y -q \ + ca-certificates \ + locales \ + libpq-dev \ + gnupg \ + apt-transport-https\ + libssl-dev \ + pkg-config \ + curl \ + build-essential \ + git \ + wget \ + && echo "ja_JP UTF-8" > /etc/locale.gen \ + && locale-gen \ + && echo "install rust tools" \ + && cargo install sqlx-cli --no-default-features --features postgres + +WORKDIR /app +COPY . . +RUN sqlx db create \ + && sqlx migrate run \ + && cargo build --release + +CMD ["cargo", "run", "--release", "--", "--bind", "0.0.0.0:3000"] diff --git a/docker-compose.yml b/docker-compose.yml index cdda5e6..051c125 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,23 +1,42 @@ version: "3" + services: api-server: - image: rust:latest container_name: api-server - working_dir: /workspace - command: cargo run -- --bind 127.0.0.1:3000 - tty: true + build: + context: . + args: + DATABASE_URL: ${DATABASE_URL} volumes: - - ./:/workspace + - .:/app ports: - 3000:3000 + environment: + DATABASE_URL: ${DATABASE_URL} + depends_on: + - db + networks: + - net + tty: + true db: image: postgres:15 - environment: - - DATABASE_URL: ${DATABASE_URL:?} + container_name: postgres + working_dir: /app ports: - - ${POSTGRES_PORT:?}:5432 + - 5432:5432 + restart: always + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} volumes: - - postgres-data-dev:/var/lib/postgresql/data + - "postgres_data_dev:/var/lib/postgresql" + networks: + - net volumes: - postgres-data-dev: + postgres_data_dev: +networks: + net: + driver: bridge From 93b7a60cc84d71c693357833d400a9544fb4401b Mon Sep 17 00:00:00 2001 From: Takaharu Nakamura <64473501+appare45@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:40:21 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20Dockerfile=E3=82=92=E3=83=93?= =?UTF-8?q?=E3=83=AB=E3=83=89=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dockerを経由してデプロイできるようになった --- Dockerfile | 9 +++++---- docker-compose.yml | 2 -- init.sh | 4 ++++ 3 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 init.sh diff --git a/Dockerfile b/Dockerfile index fb7eac2..ae74554 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,8 @@ ENV CARGO_TARGET_DIR=/tmp/target \ LANG=ja_JP.utf8 \ DATABASE_URL=${DATABASE_URL} +RUN sed -i.org -e 's|ports.ubuntu.com|jp.archive.ubuntu.com|g' /etc/apt/sources.list + RUN apt-get update \ && apt-get install -y -q \ ca-certificates \ @@ -28,8 +30,7 @@ RUN apt-get update \ WORKDIR /app COPY . . -RUN sqlx db create \ - && sqlx migrate run \ - && cargo build --release +ARG SQLX_OFFLINE=true +RUN cargo build --release -CMD ["cargo", "run", "--release", "--", "--bind", "0.0.0.0:3000"] +CMD ["bash", "./init.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 051c125..86766f8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,6 @@ services: container_name: api-server build: context: . - args: - DATABASE_URL: ${DATABASE_URL} volumes: - .:/app ports: diff --git a/init.sh b/init.sh new file mode 100644 index 0000000..ba993cb --- /dev/null +++ b/init.sh @@ -0,0 +1,4 @@ +#!/bin/bash +sqlx db create +sqlx migrate run +cargo run --release -- --bind 0.0.0.0:3000 From a77e885cb50e809cda42da344aaa6c09951d1ab5 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Tue, 5 Sep 2023 12:37:00 +0900 Subject: [PATCH 3/3] update README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 29d9fb9..3f21277 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,15 @@ - Docker - DockerCompose +以下の4つのURLを適切に設定します。 + +- `POSTGRES_DB` +- `POSTGRES_USER` +- `POSTGRES_PASSWORD` +- `DATABASE_URL` + +そのうえで以下のコマンドを実行します。 + ```sh docker-compose up -d ```