I have a server app that is dockerised, but I cannot seem to pass a variable from any docker-compse.yaml
's services to the Dockerfile
as desired.
Thus I couldn't find any another solution than what follows...
I have two nearly identical Dockerfile
s, one per each server environment:
# ./docker/production-server/Dockerfile
FROM rust:latest
WORKDIR /usr/src/rust-scrape-yt
ENV BUILD_FLAGS="--release"
ENV TEST_DOCKER="true"
ENV PRODUCTION="true"
COPY dummy.rs src/main.rs
COPY Cargo.toml .
COPY Cargo.lock .
RUN echo "Dummy build for deps, with flags: $BUILD_FLAGS"
RUN cargo build $BUILD_FLAGS
COPY . .
RUN cargo build $BUILD_FLAGS
And for the development container:
# ./docker/development-server/Dockerfile
FROM rust:latest
WORKDIR /usr/src/rust-scrape-yt
ENV BUILD_FLAGS=""
ENV TEST_DOCKER="true"
ENV PRODUCTION="false"
COPY dummy.rs src/main.rs
COPY Cargo.toml .
COPY Cargo.lock .
RUN echo "Dummy build for deps, with flags: $BUILD_FLAGS"
RUN cargo build $BUILD_FLAGS
COPY . .
RUN cargo build $BUILD_FLAGS
The only difference is that the production Dockerfile has:
ENV BUILD_FLAGS="--release"
While the development server image has:
ENV BUILD_FLAGS=""
COPY dummy.rs src/main.rs
COPY Cargo.toml .
COPY Cargo.lock .
RUN echo "Dummy build for deps, with flags: $BUILD_FLAGS"
RUN cargo build $BUILD_FLAGS
COPY . .
RUN cargo build $BUILD_FLAGS
My docker-compose.yaml
is as follows:
version: "3.9"
services:
production-db:
image: postgres
restart: always
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: thisisjustSommmePasWorttLaliloo
POSTGRES_DB: rust-yt-scraper
ports:
- 15878:5432
volumes:
- production-rust-yt-scraper:/var/lib/postgresql/data
development-db:
image: postgres
restart: always
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: thisisjustSommmePasWorttLalilooForDev
POSTGRES_DB: rust-yt-scraper
ports:
- 15879:5432
volumes:
- development-rust-yt-scraper:/var/lib/postgresql/data
production-server:
depends_on:
- production-db
build:
context: .
dockerfile: docker/production-server/Dockerfile
image: djfm/rust-yt-scraper-prod:latest
restart: always
environment:
PRODUCTION: "true"
ports:
- "19850:8080"
command: cargo run --release --bin server
development-server:
depends_on:
- development-db
build:
context: .
dockerfile: docker/development-server/Dockerfile
image: djfm/rust-yt-scraper-dev:latest
restart: always
ports:
- "19851:8080"
command: cargo run --bin server
environment:
PRODUCTION: "false"
server-tests:
image: djfm/rust-yt-scraper:latest
restart: never
depends_on:
- development-server
environment:
- PRODUCTION:false
- BUILD_FLAGS:""
command: cargo test
adminer:
image: adminer
restart: always
ports:
- "9280:8080"
volumes:
production-rust-yt-scraper:
driver: local
development-rust-yt-scraper:
driver: local
I've tried messing with:
service:
blah:
build:
context: .
dockerfiles: ./conf/server.Dockerfile
args:
PRODUCTION:true
Is this hopeless? do I need to maintain separate Dockerfiles?