Giter VIP home page Giter VIP logo

Comments (4)

mchlstckl avatar mchlstckl commented on May 31, 2024

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”).

from testcontainers-rs.

0xFFFFFFFFFFFFFFFFFF avatar 0xFFFFFFFFFFFFFFFFFF commented on May 31, 2024

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 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.

from testcontainers-rs.

mchlstckl avatar mchlstckl commented on May 31, 2024

from testcontainers-rs.

0xFFFFFFFFFFFFFFFFFF avatar 0xFFFFFFFFFFFFFFFFFF commented on May 31, 2024

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.

from testcontainers-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.