diff --git a/containerised/Dockerfile b/containerised/Dockerfile index 525ea7d..9d0fe10 100644 --- a/containerised/Dockerfile +++ b/containerised/Dockerfile @@ -51,10 +51,12 @@ RUN cp /usr/src/oxker/target/$(cat /.platform)/release/oxker / FROM alpine:latest AS runtime +# Set an ENV that we're running in a container, so that the application can sleep for 250ms at start +ENV OXKER_RUNTIME=container + # Copy application binary from builder image COPY --from=builder /oxker /usr/local/bin -COPY ./containerised/start_oxker.sh ./ # Run the application # this is used in the application itself, to stop itself show when running from a docker container, so DO NOT EDIT -ENTRYPOINT [ "./start_oxker.sh"] +ENTRYPOINT [ "./app/oxker"] diff --git a/containerised/Dockerfile_dev b/containerised/Dockerfile_dev index 4a91c38..77a5522 100644 --- a/containerised/Dockerfile_dev +++ b/containerised/Dockerfile_dev @@ -1,16 +1,23 @@ ############# ## Runtime ## ############# +FROM scratch -FROM alpine:latest AS runtime +# Set env that we're running in a container, so that the application can sleep for 250ms at start +ENV OXKER_RUNTIME=container # Copy application binary from builder image -COPY ./target/x86_64-unknown-linux-musl/release/oxker /usr/local/bin -COPY ./containerised/start_oxker.sh ./ +COPY ./target/x86_64-unknown-linux-musl/release/oxker /app/ # Run the application # this is used in the application itself, to stop itself show when running from a docker container, so DO NOT EDIT -ENTRYPOINT [ "./start_oxker.sh"] +ENTRYPOINT [ "./app/oxker"] + +# Dev build for testing +# docker build -t oxker_dev -f Dockerfile . && docker run --rm -it --volume /var/run/docker.sock:/var/run/docker.sock:ro oxker_dev + +# Dev build one liner, x86 host +# docker image prune -a; cargo build --release --target x86_64-unknown-linux-musl && docker build -t oxker_dev -f containerised/Dockerfile_dev . && docker run --rm -it --volume /var/run/docker.sock:/var/run/docker.sock:ro oxker_dev ## One liner to build musl program, build docker image, then execute the image # cargo build --release --target x86_64-unknown-linux-musl && docker build -t oxker_dev -f containerised/Dockerfile . && docker run --rm -it --volume /var/run/docker.sock:/var/run/docker.sock:ro oxker_dev diff --git a/containerised/start_oxker.sh b/containerised/start_oxker.sh deleted file mode 100755 index 7ac5fa3..0000000 --- a/containerised/start_oxker.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -set -e - -# Without this sleep, the docker image will instantly close -# No idea why this is solving my issue, or even where the issue is originally coming from -sleep .1 - -exec /usr/local/bin/oxker "$@" \ No newline at end of file diff --git a/src/docker_data/mod.rs b/src/docker_data/mod.rs index 298de6c..ec90e0e 100644 --- a/src/docker_data/mod.rs +++ b/src/docker_data/mod.rs @@ -54,6 +54,7 @@ pub struct DockerData { app_data: Arc>, args: CliArgs, binate: Binate, + containerised: bool, docker: Arc, gui_state: Arc>, is_running: Arc, @@ -175,7 +176,7 @@ impl DockerData { /// Get all current containers, handle into ContainerItem in the app_data struct rather than here /// Just make sure that items sent are guaranteed to have an id - /// Will ignore any container that uses `./start_oxker.sh` as an entry point, unless the `-s` flag is set + /// If in a containerised runtime, will ignore any container that uses the q`./app/oxker` as an entry point, unless the `-s` flag is set pub async fn update_all_containers(&mut self) -> Vec<(bool, ContainerId)> { let containers = self .docker @@ -190,15 +191,15 @@ impl DockerData { .into_iter() .filter_map(|f| match f.id { Some(_) => { - if f.command - .as_ref() - .map_or(false, |c| c.starts_with(ENTRY_POINT)) - && self.args.show_self - { - None - } else { - Some(f) - } + if self.containerised && f.command + .as_ref() + .map_or(false, |c| c.starts_with(ENTRY_POINT)) + && self.args.show_self + { + None + } else { + Some(f) + } } None => None, }) @@ -414,6 +415,7 @@ impl DockerData { /// Initialise self, and start the message receiving loop pub async fn init( app_data: Arc>, + containerised: bool, docker: Docker, docker_rx: Receiver, gui_state: Arc>, @@ -423,6 +425,7 @@ impl DockerData { if app_data.lock().get_error().is_none() { let mut inner = Self { app_data, + containerised, args, binate: Binate::One, docker: Arc::new(docker), diff --git a/src/main.rs b/src/main.rs index 9e3fdfe..a72e347 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,16 +33,33 @@ use ui::{create_ui, GuiState, Status}; use crate::docker_data::DockerMessage; -const ENTRY_POINT: &str = "./start_oxker.sh"; +// this is the entry point when running as a Docker Container, and is used to check if we are running as a Docker Containerq +const ENTRY_POINT: &str = "./app/oxker"; +/// Enable tracing, only really used in debug mode, for now /// write to file if `-g` is set? fn setup_tracing() { tracing_subscriber::fmt().with_max_level(Level::INFO).init(); } +/// An ENV is set in the ./containerised/Dockerfile, if this is ENV found, then sleep for 250ms, else the container, for as yet unknown reasons, will close immediately +/// returns a bool, so that the `update_all_containers()` won't bother to check the entry point unless running via a container +fn check_if_containerised() -> bool { + if std::env::vars() + .into_iter() + .any(|x| x == ("OXKER_RUNTIME".to_owned(), "container".to_owned())) + { + std::thread::sleep(std::time::Duration::from_millis(250)); + true + } else { + false + } +} + /// Create docker daemon handler, and only spawn up the docker data handler if a ping returns non-error async fn docker_init( app_data: &Arc>, + containerised: bool, docker_rx: Receiver, gui_state: &Arc>, is_running: &Arc, @@ -53,7 +70,12 @@ async fn docker_init( let gui_state = Arc::clone(gui_state); let is_running = Arc::clone(is_running); tokio::spawn(DockerData::init( - app_data, docker, docker_rx, gui_state, is_running, + app_data, + containerised, + docker, + docker_rx, + gui_state, + is_running, )); } else { app_data.lock().set_error(AppError::DockerConnect); @@ -87,7 +109,10 @@ fn handler_init( #[tokio::main] async fn main() { + let containerised = check_if_containerised(); + setup_tracing(); + let args = CliArgs::new(); let app_data = Arc::new(Mutex::new(AppData::default(args))); let gui_state = Arc::new(Mutex::new(GuiState::default())); @@ -95,7 +120,7 @@ async fn main() { let (docker_sx, docker_rx) = tokio::sync::mpsc::channel(16); let (input_sx, input_rx) = tokio::sync::mpsc::channel(16); - docker_init(&app_data, docker_rx, &gui_state, &is_running).await; + docker_init(&app_data, containerised, docker_rx, &gui_state, &is_running).await; handler_init(&app_data, &docker_sx, &gui_state, input_rx, &is_running);