feat: switch to scratch docker container

This commit is contained in:
Jack Wills
2023-02-03 21:40:14 +00:00
parent a76bfbcbd9
commit 17b71b6b41
5 changed files with 56 additions and 27 deletions
+4 -2
View File
@@ -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"]
+11 -4
View File
@@ -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
-8
View File
@@ -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 "$@"
+5 -2
View File
@@ -54,6 +54,7 @@ pub struct DockerData {
app_data: Arc<Mutex<AppData>>,
args: CliArgs,
binate: Binate,
containerised: bool,
docker: Arc<Docker>,
gui_state: Arc<Mutex<GuiState>>,
is_running: Arc<AtomicBool>,
@@ -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,7 +191,7 @@ impl DockerData {
.into_iter()
.filter_map(|f| match f.id {
Some(_) => {
if f.command
if self.containerised && f.command
.as_ref()
.map_or(false, |c| c.starts_with(ENTRY_POINT))
&& self.args.show_self
@@ -414,6 +415,7 @@ impl DockerData {
/// Initialise self, and start the message receiving loop
pub async fn init(
app_data: Arc<Mutex<AppData>>,
containerised: bool,
docker: Docker,
docker_rx: Receiver<DockerMessage>,
gui_state: Arc<Mutex<GuiState>>,
@@ -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),
+28 -3
View File
@@ -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<Mutex<AppData>>,
containerised: bool,
docker_rx: Receiver<DockerMessage>,
gui_state: &Arc<Mutex<GuiState>>,
is_running: &Arc<AtomicBool>,
@@ -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);