From 4d8a6a7b12436ae0e80d2673925f78742a6806cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Thu, 4 Jun 2020 20:47:10 +0200 Subject: [PATCH] Add docker --- .builds/nginx.conf | 29 ++++++++++++++++++++ .dockerignore | 4 --- .env | 1 - docker-compose.yml | 54 ++++++++++++++++++++++++++++++++++--- jirs-client/.dockerignore | 5 ++++ jirs-client/Dockerfile | 33 +++++++++++++++++++++++ jirs-client/jirs.nginx | 4 +-- jirs-client/scripts/prod.sh | 2 +- jirs-server/Dockerfile | 20 ++++++++------ jirs-server/src/db/mod.rs | 8 +++--- 10 files changed, 137 insertions(+), 23 deletions(-) create mode 100644 .builds/nginx.conf create mode 100644 jirs-client/.dockerignore create mode 100644 jirs-client/Dockerfile diff --git a/.builds/nginx.conf b/.builds/nginx.conf new file mode 100644 index 00000000..52ebce69 --- /dev/null +++ b/.builds/nginx.conf @@ -0,0 +1,29 @@ +server { + listen 80; + server_name jirs.lvh.me; + + charset utf-8; + root /assets; + + location ~ .wasm { + default_type application/wasm; + } + + location *.js { + default_type application/javascript; + } + + location / { + index index.html index.htm; + } + + error_page 404 =200 /index.html; + + location /ws/ { + proxy_pass http://server:5000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_set_header Host $host; + } +} diff --git a/.dockerignore b/.dockerignore index 4fd8b59e..fff88fed 100644 --- a/.dockerignore +++ b/.dockerignore @@ -77,10 +77,6 @@ fabric.properties # will have compiled files and executables /target/ -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - # These are backup files generated by rustfmt **/*.rs.bk diff --git a/.env b/.env index 69a1c147..2a22425c 100644 --- a/.env +++ b/.env @@ -1,5 +1,4 @@ DEBUG=true -NODE_ENV=development RUST_LOG=debug JIRS_CLIENT_PORT=7000 JIRS_CLIENT_BIND=0.0.0.0 diff --git a/docker-compose.yml b/docker-compose.yml index b933059a..6c19b01a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,59 @@ -version: '3.0' +version: '3.2' services: db: image: postgres:latest - ports: - - 5432:5432 + environment: + - POSTGRES_USER=postgres + - POSTGRES_HOST_AUTH_METHOD=trust + server: build: dockerfile: ./jirs-server/Dockerfile context: . + depends_on: + - db + environment: + - DATABASE_URL=postgres://postgres@db/jirs + - JIRS_SERVER_PORT=5000 + - JIRS_SERVER_BIND=0.0.0.0 + - RUST_LOG=debug + - DEBUG=true + - JIRS_CLIENT_PORT=7000 + - JIRS_CLIENT_BIND=0.0.0.0 + + nginx: + image: nginx:latest + depends_on: + - client + - server + ports: + - 80:80 + volumes: + - ./.builds/nginx.conf:/etc/nginx/conf.d/default.conf + - type: volume + source: assets + target: /assets + volume: + nocopy: true + + client: + build: + dockerfile: ./jirs-client/Dockerfile + context: . + env_file: + - .env + environment: + - JIRS_SERVER_PORT=80 + - JIRS_SERVER_BIND=jirs.lvh.me + - JIRS_CLIENT_PORT=80 + - JIRS_CLIENT_BIND=jirs.lvh.me + volumes: + - type: volume + source: assets + target: /assets + volume: + nocopy: true + +volumes: + assets: diff --git a/jirs-client/.dockerignore b/jirs-client/.dockerignore new file mode 100644 index 00000000..d5d00d6c --- /dev/null +++ b/jirs-client/.dockerignore @@ -0,0 +1,5 @@ +node_modules +dev +pkg +tmp +target diff --git a/jirs-client/Dockerfile b/jirs-client/Dockerfile new file mode 100644 index 00000000..0b65e4bf --- /dev/null +++ b/jirs-client/Dockerfile @@ -0,0 +1,33 @@ +FROM archlinux:latest + +RUN pacman -Sy rustup gcc which --noconfirm + +WORKDIR /app/ + +RUN rustup toolchain install nightly && \ + rustup default nightly && \ + curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + +ADD ./jirs-data /app/jirs-data +ADD ./jirs-css /app/jirs-css + +RUN cd /app/jirs-css && cargo build --bin jirs-css && cp ./target/debug/jirs-css /bin + +ADD ./jirs-client /app/jirs-client + +RUN cd ./jirs-client && \ + rm -Rf build && \ + mkdir build && \ + wasm-pack build --mode normal --release --out-name jirs --out-dir ./build --target web && \ + jirs-css -i ./js/styles.css -O ./build/styles.css && \ + cp -r ./static/* ./build && \ + cat ./static/index.js \ + | sed -e "s/process.env.JIRS_SERVER_BIND/'$JIRS_SERVER_BIND'/g" \ + | sed -e "s/process.env.JIRS_SERVER_PORT/'$JIRS_SERVER_PORT'/g" &> ./build/index.js && \ + cp ./js/template.html ./build/index.html && \ + mkdir -p /assets && \ + cp -r ./build/* /assets + +CMD cat /app/jirs-client/static/index.js \ + | sed -e "s/process.env.JIRS_SERVER_BIND/'$JIRS_SERVER_BIND'/g" \ + | sed -e "s/process.env.JIRS_SERVER_PORT/'$JIRS_SERVER_PORT'/g" &> /assets/index.js diff --git a/jirs-client/jirs.nginx b/jirs-client/jirs.nginx index a525972a..72860d27 100644 --- a/jirs-client/jirs.nginx +++ b/jirs-client/jirs.nginx @@ -6,11 +6,11 @@ server { root /home/eraden/code/eraden/jirs/jirs-client/tmp; location ~ .wasm { - default_type application/wasm; + default_type application/wasm; } location *.js { - default_type application/javascript; + default_type application/javascript; } location / { diff --git a/jirs-client/scripts/prod.sh b/jirs-client/scripts/prod.sh index 565acd2a..97106a19 100755 --- a/jirs-client/scripts/prod.sh +++ b/jirs-client/scripts/prod.sh @@ -6,7 +6,7 @@ rm -Rf build mkdir build wasm-pack build --mode normal --release --out-name jirs --out-dir ./build --target web -../target/debug/jirs-css -i ./js/styles.css -O ./tmp/styles.css +../target/debug/jirs-css -i ./js/styles.css -O ./build/styles.css cp -r ./static/* ./build cat ./static/index.js \ diff --git a/jirs-server/Dockerfile b/jirs-server/Dockerfile index de2d56de..c08e6ece 100644 --- a/jirs-server/Dockerfile +++ b/jirs-server/Dockerfile @@ -3,13 +3,17 @@ FROM archlinux:latest WORKDIR /app/ RUN pacman -Sy rustup gcc postgresql --noconfirm - -ADD jirs-server . -ADD jirs-data . - RUN rustup toolchain install nightly && \ - rustup default nightly && \ - cargo install diesel_cli --no-default-features --features postgres && \ - cd jirs-server && diesel setup + rustup default nightly -CMD cd jirs-server && cargo run --bin jirs_server +RUN cargo install diesel_cli --no-default-features --features postgres + +ADD jirs-server /app/jirs-server +ADD jirs-data /app/jirs-data + +RUN pacman -Sy openssl openssh pkgconf --noconfirm +RUN pkg-config --libs openssl + +CMD cd /app/jirs-server && \ + $HOME/.cargo/bin/diesel setup --database-url=$DATABASE_URL && \ + cargo run --bin jirs_server diff --git a/jirs-server/src/db/mod.rs b/jirs-server/src/db/mod.rs index 9a1706c2..ebbc1a7a 100644 --- a/jirs-server/src/db/mod.rs +++ b/jirs-server/src/db/mod.rs @@ -63,11 +63,11 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { let database_url = if cfg!(test) { - "postgres://postgres@localhost:5432/jirs_test" + "postgres://postgres@localhost:5432/jirs_test".to_string() } else { - "postgres://postgres@localhost:5432/jirs" - } - .to_string(); + std::env::var("DATABASE_URL") + .unwrap_or_else(|_| "postgres://postgres@localhost:5432/jirs".to_string()) + }; Self { concurrency: 2, database_url,