Closed ndrean closed 3 months ago
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 100.00%. Comparing base (
462ef27
) to head (0008846
). Report is 16 commits behind head on main.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
I fought a little bit, and my process ends with an error 137 (out of memory) ?? (I have only 8G RAM, Docker+MacOS)
Hopefully it works, so if you have a better way, I take it.
We can whether use build: .
and pass args in the "docker-compose.yml" file or use an image.
1) If Image:
docker run -d -p 5001:5000 --name registry registry:2.7
docker build --build-args="MIX_ENV=prod" -t localhost:5001/img:latest .
where the Dockerfile uses a build-arg to switch Mix modes:
...
FROM ${BUILDER_IMAGE} as builder
ARG MIX_ENV
ENV MIX_ENV=$MIX_ENV
...
FROM ${RUNNER_IMAGE}
ARG MIX_ENV
ENV MIX_ENV=$MIX_ENV
...
docker push localhost:5001/img-[dev/prod]:latest
2) Instantiate the database.
App.Release.migrate/0
in the Dockerfile CMD
The 1st option uses CMD
to execute Ecto.Migrator
. You firstly run the migration and then start the app:
CMD ["sh", "-c", "/app/bin/migrate && /app/bin/server"]
Check he Fly.io blog on running migrations Also this thread on Elixirforum helps a lot.
The 2d option works only for a newly created database. Clean the generated file below and save it.
mix ecto.create && mix ecto.migrate --log-migrations-sql > ./init.sql
psql
variables.# init-db.sh
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d "$POSTGRES_DB" -v db="$POSTGRES_DB" -v user="$POSTGRES_USER" -e <<-EOSQL
GRANT ALL PRIVILEGES ON DATABASE :db TO :user;
CREATE TABLE "images" (
"id" bigserial,
"url" varchar(255),
"description" varchar(255),
"width" integer,
"height" integer,
"inserted_at" timestamp(0) NOT NULL,
"updated_at" timestamp(0) NOT NULL,
PRIMARY KEY ("id")
);
ALTER TABLE "images"
ADD COLUMN "idx" bigint,
ADD COLUMN "sha1" varchar(255);
CREATE UNIQUE INDEX "images_sha1_index" ON "images" ("sha1");
CREATE UNIQUE INDEX "images_idx_index" ON "images" ("idx");
CREATE TABLE IF NOT EXISTS "hnswlib_index" (
"id" bigserial,
"lock_version" integer DEFAULT 1,
"file" bytea,
PRIMARY KEY ("id")
);
EOSQL
3) Create an ".env-docker" file for docker compose
:
# .env-prod-docker
MIX_ENV=prod
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres
POSTGRES_DB=app_prod
PGDATA=var/lib/postgresql/data/pg-data
SECRET_KEY_BASE=qhSXOpnkmvYzf3/maxi4/pg+WkVnYkhp
DATABASE_URL=ecto://postgres:postgres@db:5432/app_prod
PHX_SERVER=true
4) Run the "docker-compose.yml". The "pg-data" volume can be pruned to re-instantiate the DB.
docker compose up
# docker-compose.yml
version: "3.9"
volumes:
pg-data:
driver: local
services:
db:
image: postgres:15.3-bullseye
env_file:
- .env-docker
restart: always
volumes:
- pg-data:/var/lib/postgresql/data/pg-data
- ./init.sql:/docker-entrypoint-initdb.d/init.sh:ro
ports:
- 5432:5432
app:
build:
context: .
args:
- MIX_ENV
# OR
image: localhost:5001/img-[dev/prod]:latest
depends_on:
- db
env_file:
- .env-docker
ports:
- 4000:4000
Thanks @ndrean !
Match code with Dockerfile. The Dockerfile builds correctly. I did not use it nor tested the deployment of course.
Why?
You seemed to have had problems with
npm
in the dev mode and used successfullypnpm
to overcome this.For the Dockerfile, you don't use
pnpm
and I assume it works.If you want to match the code with the Dockerfile, you may want to use
pnpm
too.How?
For this, you need to install a recent
Node.js
version viacURL
(Debian comes with v12 installed whilstpnpm
requires >v16). Note thatnode
comes withnpm
.You can use the Dockerfile below.