testcontainers-rs
testcontainers-rs copied to clipboard
Make it work with a remote docker host
The docker daemon can be hosted on a remote machine, hence in addition to the port
of a container, we should also expose the host
on which the docker container was started.
I ran into this issue on Gitlab CI where you need to set the DOCKER_HOST=tcp://docker:2375
.
I ended up writing an extension trait that extends Container
with a get_host()
method. The method url parses the DOCKER_HOST
value and if scheme is ”tcp” returns the url host else returns ”localhost” (like when the scheme is ”unix”).
I ran into this issue on Gitlab CI where you need to set the
DOCKER_HOST=tcp://docker:2375
.I ended up writing an extension trait that extends
Container
with aget_host()
method. The method url parses theDOCKER_HOST
value and if scheme is ”tcp” returns the url host else returns ”localhost” (like when the scheme is ”unix”).
@mchlstckl Could you please provide us some snippets of the relevant lines including the .gitlab-ci.yml? My integration tests are running fine on my local machines but not in the GitLab runner. I was struggling the past days to get this working, but I haven't figured out yet.
Sorry. That was a while back and I don’t have that code anymore.
On Tue, 14 Mar 2023 at 23:29, kuba @.***> wrote:
I ran into this issue on Gitlab CI where you need to set the DOCKER_HOST=tcp://docker:2375.
I ended up writing an extension trait that extends Container with a get_host() method. The method url parses the DOCKER_HOST value and if scheme is ”tcp” returns the url host else returns ”localhost” (like when the scheme is ”unix”).
@mchlstckl https://github.com/mchlstckl Could you please provide us some snippets of the relevant lines including the .gitlab-ci.yml? My integration tests are running fine on my local machines but not in the GitLab runner. I was struggling the past days to get this working, but I haven't figured out yet.
— Reply to this email directly, view it on GitHub https://github.com/testcontainers/testcontainers-rs/issues/89#issuecomment-1468927468, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAARXEXEQF32CQUPND6QMWDW4DWOHANCNFSM4HLWYGLA . You are receiving this because you were mentioned.Message ID: @.***>
No problem, thanks for checking though. I managed to get it working and want to share it in case someone else stumbles on on this. I am passing my docker socket to the runner as described here.
.gitlab-ci.yml:
test:
image: rust:1
stage: test
before_script:
- wget https://download.docker.com/linux/static/stable/x86_64/docker-23.0.1.tgz
- tar xvfz docker-23.0.1.tgz
- mv docker/docker /usr/bin/
- rm -Rf docker/ docker-23.0.1.tgz
script:
- cargo test
rust trait:
use std::{process::Command, io::Read, str::FromStr};
pub trait ContainerHost {
fn get_host_addr(&self) -> String;
}
impl<'d, I> ContainerHost for testcontainers::Container<'d, I>
where
I: testcontainers::Image,
{
fn get_host_addr(&self) -> String {
let mut output = String::new();
Command::new("docker")
.arg("inspect")
.arg("-f")
.arg("'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'")
.arg(&self.id())
.output()
.expect("inspecting container")
.stdout
.as_slice()
.read_to_string(&mut output)
.expect("parsing IP address");
let output = output.replace('\'', "");
let output = String::from(output.trim());
assert!(std::net::IpAddr::from_str(output.as_str()).is_ok());
output
}
}
Then you can just connect to the container using its IP and internal port in your integration test.