feat: add ability to run an exposed container
This commit is contained in:
parent
17fcd053ac
commit
56df0f204b
12
NETWORKING.md
Normal file
12
NETWORKING.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# Networking
|
||||||
|
|
||||||
|
We've got multiple hosts potentially in play, and multiple containers that wants
|
||||||
|
to expose themselves on the primary host, but only 1 process can take a tcp port
|
||||||
|
on the same host at a time.
|
||||||
|
|
||||||
|
1. The primary instance, will bind to common tcp ports as required (80, 443) can
|
||||||
|
be changed.
|
||||||
|
1. TODO: TLS termination
|
||||||
|
1. Will send traffic to peers hosting their versions of a url.
|
||||||
|
1. On the peers, each container will bind to a port (TBD), maybe we will do some
|
||||||
|
long running connections instead. SOCK protocol
|
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
container_runtime::ContainerRuntimeState, grpc_client::GrpcClientState, state::ClientState,
|
container_runtime::ContainerRuntimeState, grpc_client::GrpcClientState, models::port::Port,
|
||||||
|
state::ClientState,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
@ -22,6 +23,10 @@ impl SubscribeCommand {
|
|||||||
.ensure_running(
|
.ensure_running(
|
||||||
&project.name,
|
&project.name,
|
||||||
&format!("{}:{}", project.image, project.version),
|
&format!("{}:{}", project.image, project.version),
|
||||||
|
vec![Port {
|
||||||
|
host_port: 38080,
|
||||||
|
container_port: 80,
|
||||||
|
}],
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
@ -5,13 +5,13 @@ use bollard::{
|
|||||||
image::CreateImageOptions,
|
image::CreateImageOptions,
|
||||||
query_parameters::{
|
query_parameters::{
|
||||||
CreateContainerOptionsBuilder, CreateImageOptionsBuilder, ListContainersOptionsBuilder,
|
CreateContainerOptionsBuilder, CreateImageOptionsBuilder, ListContainersOptionsBuilder,
|
||||||
StartContainerOptions,
|
StartContainerOptions, StartContainerOptionsBuilder,
|
||||||
},
|
},
|
||||||
secret::ContainerCreateBody,
|
secret::{ContainerCreateBody, HostConfig, PortBinding},
|
||||||
};
|
};
|
||||||
use futures_util::TryStreamExt;
|
use futures_util::TryStreamExt;
|
||||||
|
|
||||||
use crate::state::ClientState;
|
use crate::{models::port::Port, state::ClientState};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ContainerRuntime {
|
pub struct ContainerRuntime {
|
||||||
@ -20,7 +20,12 @@ pub struct ContainerRuntime {
|
|||||||
|
|
||||||
impl ContainerRuntime {
|
impl ContainerRuntime {
|
||||||
#[tracing::instrument(skip(self), level = "trace")]
|
#[tracing::instrument(skip(self), level = "trace")]
|
||||||
pub async fn ensure_running(&self, name: &str, image: &str) -> anyhow::Result<()> {
|
pub async fn ensure_running(
|
||||||
|
&self,
|
||||||
|
name: &str,
|
||||||
|
image: &str,
|
||||||
|
ports: Vec<Port>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
tracing::debug!("ensuring that image is running");
|
tracing::debug!("ensuring that image is running");
|
||||||
|
|
||||||
let containers = self
|
let containers = self
|
||||||
@ -50,18 +55,34 @@ impl ContainerRuntime {
|
|||||||
.try_collect::<Vec<_>>()
|
.try_collect::<Vec<_>>()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
let ports: HashMap<_, _> = ports
|
||||||
|
.iter()
|
||||||
|
.map(|p| {
|
||||||
|
(
|
||||||
|
format!("{}/tcp", p.container_port),
|
||||||
|
Some(vec![PortBinding {
|
||||||
|
host_ip: Some("0.0.0.0".into()),
|
||||||
|
host_port: Some(p.host_port.to_string()),
|
||||||
|
}]),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
self.client
|
self.client
|
||||||
.create_container(
|
.create_container(
|
||||||
Some(CreateContainerOptionsBuilder::new().name(name).build()),
|
Some(CreateContainerOptionsBuilder::new().name(name).build()),
|
||||||
ContainerCreateBody {
|
ContainerCreateBody {
|
||||||
image: Some(image.into()),
|
image: Some(image.into()),
|
||||||
|
host_config: Some(HostConfig {
|
||||||
|
port_bindings: Some(ports),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.client
|
self.client
|
||||||
.start_container(name, None::<StartContainerOptions>)
|
.start_container(name, Some(StartContainerOptionsBuilder::default().build()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1,2 +1,10 @@
|
|||||||
pub mod project_tag;
|
pub mod project_tag;
|
||||||
pub use project_tag::*;
|
pub use project_tag::*;
|
||||||
|
|
||||||
|
pub mod port {
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct Port {
|
||||||
|
pub host_port: usize,
|
||||||
|
pub container_port: usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user