Skip to content

Commit

Permalink
Combine api and frontend into one docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
schrieveslaach committed Feb 12, 2019
1 parent fdccb5a commit 01e686f
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 1,261 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
api/target/
frontend/node_modules/
frontend/target/
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Build Frontend
FROM docker.io/library/node:10-alpine as frontend-builder
WORKDIR /usr/src/frontend/
COPY frontend/package.json frontend/package-lock.json frontend/postcss.config.js frontend/webpack.config.js /usr/src/frontend/
COPY frontend/src /usr/src/frontend/src/
RUN npm ci && npm run build


# Build Backend
FROM docker.io/ekidd/rust-musl-builder as backend-builder
USER root
RUN rustup default nightly && rustup target add x86_64-unknown-linux-musl
COPY api/Cargo.toml api/Cargo.lock /usr/src/api/
COPY api/src /usr/src/api/src
WORKDIR /usr/src/api
RUN cargo build --release --target x86_64-unknown-linux-musl


# Build whole application
FROM docker.io/library/alpine
LABEL maintainer="marc.schreiber@aixigo.de"

RUN adduser -D -u 1000 prevant
COPY --chown=prevant --from=backend-builder /usr/src/api/target/x86_64-unknown-linux-musl/release/prevant /app/prevant
COPY --chown=prevant api/res/Rocket.toml api/res/config.toml /app/
COPY --chown=prevant --from=frontend-builder /usr/src/frontend/target/* /app/frontend/
COPY --chown=prevant frontend/index.html frontend/favicon.svg /app/frontend/

WORKDIR /app
EXPOSE 80
ENV ROCKET_ENV=staging
ENV RUST_LOG=info
CMD ["./prevant"]
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ This project is currently being made available as open source. Not all features

# Usage

In order to use the project you have to build the docker images (they will be released in the docker hub in the future).
In order to use the project you have to build the docker image (they will be released in the docker hub in the future).

```bash
mvn package -f api
mvn package -f frontend
docker build -t aixigo/prevant .
```

When you have build the images, you can start the docker containers with the provided docker-compose file.
When you have build the image, you can start the docker containers with the provided docker-compose file.

```
docker-compose up -d
Expand All @@ -33,4 +32,4 @@ Now, PREvant is running at [`http://localhost`](http://localhost).

# Further Readings

- [All about API image](api/README.md)
- [All about the API](api/README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class PREvantRestApiExtension implements BeforeAllCallback, AfterAllCallback, ParameterResolver {

private final GenericContainer prevantRestApiContainer = new GenericContainer("aixigo/prevant-api")
private final GenericContainer prevantRestApiContainer = new GenericContainer("aixigo/prevant")
.withFileSystemBind("/var/run/docker.sock", "/var/run/docker.sock", BindMode.READ_WRITE)
.withLabel("traefik.frontend.rule", "ReplacePathRegex: ^/api(.*) /$1;PathPrefix:/api;")
.withLogConsumer(new Consumer<OutputFrame>() {
Expand Down
1 change: 0 additions & 1 deletion api/.dockerignore

This file was deleted.

60 changes: 30 additions & 30 deletions api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "api"
name = "prevant"
version = "0.9.0"
authors = ["Marc Schreiber <marc.schreiber@aixigo.de>"]
repository = "https://github.com/aixigo/PREvant/"
Expand Down
20 changes: 0 additions & 20 deletions api/Dockerfile

This file was deleted.

9 changes: 0 additions & 9 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,4 @@
<artifactId>api</artifactId>
<name>PREvant REST API</name>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
45 changes: 16 additions & 29 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,22 @@

#![feature(proc_macro_hygiene, decl_macro, try_from)]

extern crate crossbeam_utils;
extern crate dkregistry;
#[macro_use]
extern crate failure;
extern crate futures;
extern crate goji;
extern crate handlebars;
extern crate hyper;
#[macro_use]
extern crate log;
extern crate multimap;
extern crate regex;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate serde_yaml;
extern crate shiplift;
extern crate tokio;
extern crate toml;
extern crate url;

use crate::models::request_info::RequestInfo;
use crate::services::config_service::Config;
use rocket_contrib::json::Json;
use rocket::response::NamedFile;
use serde_yaml::{from_reader, to_string, Value};
use shiplift::{ContainerListOptions, Docker};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process;
use tokio::prelude::Future;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -95,13 +80,14 @@ fn is_container_available(container_image_pattern: &'static str) -> bool {
}
}

#[get("/", format = "application/json")]
fn index(request_info: RequestInfo) -> Json<AppsStatus> {
Json(AppsStatus {
root_url: request_info.get_base_url().clone().into_string(),
swagger_ui_available: is_container_available("swaggerapi/swagger-ui"),
portainer_available: is_container_available("portainer/portainer"),
})
#[get("/")]
fn index() -> Option<NamedFile> {
NamedFile::open(Path::new("frontend/index.html")).ok()
}

#[get("/<path..>")]
fn files(path: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("frontend/").join(path)).ok()
}

#[get("/swagger.yaml")]
Expand Down Expand Up @@ -131,11 +117,12 @@ fn main() {
rocket::ignite()
.manage(config)
.mount("/", routes![index])
.mount("/", routes![files])
.mount("/", routes![swagger])
.mount("/", routes![apps::apps])
.mount("/", routes![apps::tickets])
.mount("/", routes![apps::create_app])
.mount("/", routes![apps::delete_app])
.mount("/", routes![webhooks::webhooks])
.mount("/api", routes![apps::apps])
.mount("/api", routes![apps::tickets])
.mount("/api", routes![apps::create_app])
.mount("/api", routes![apps::delete_app])
.mount("/api", routes![webhooks::webhooks])
.launch();
}
11 changes: 2 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ version: "3"

services:
api:
image: aixigo/prevant-api
image: aixigo/prevant
network_mode: "bridge"
userns_mode: "host"
labels:
traefik.frontend.rule: 'PathPrefixStrip: /api;'
traefik.frontend.rule: 'PathPrefix:/;'
volumes:
- /var/run/docker.sock:/var/run/docker.sock

frontend:
image: aixigo/prevant-frontend
network_mode: "bridge"
labels:
traefik.frontend.rule: 'PathPrefix:/;'
traefik.frontend.priority: -100

traefik:
image: traefik
command: --api --docker
Expand Down
2 changes: 0 additions & 2 deletions frontend/.dockerignore

This file was deleted.

Loading

0 comments on commit 01e686f

Please sign in to comment.