Skip to content

Support locally built images with Docker::Image.get fallback in #start #78

Closed

Description

@dieter-medium

Hi there 👋,

Thanks for the great work on testcontainers-ruby!

Currently, the #start method internally uses:

`Docker::Image.create(...)´

This approach always tries to pull the image from a remote registry, which can be problematic when I’ve built an image locally using:

image_name = "my-name:latest"
Docker::Image.build_from_dir('.', { "t" => image_name, "dockerfile" => "Dockerfile" })

n such cases, it would be helpful if #start could attempt to use Docker::Image.get(image_name) first to find a local image, and only fall back to Docker::Image.create(...) if it's not found.

Proposed logic:

begin
  image = Docker::Image.get(image_name)
rescue Docker::Error::NotFoundError
  image = Docker::Image.create('fromImage' => image_name)
end

Alternatively — or even better — maybe the project could support building an image natively from a local Dockerfile as part of the container lifecycle setup? This would make local development more seamless and reduce external dependencies for custom containers.

Would you be open to adding this fallback behavior?

Thanks again

Activity

  1. dieter-medium commented on Apr 6, 2025

    @dieter-medium
    ContributorAuthor

    Just to add some context — I tried working around this by monkey patching the #start method like so:

    module TestcontainersExtensions
      refine Testcontainers::DockerContainer do
        def start_local_image
          @_container ||= Docker::Container.create(_container_create_options)
          @_container.start
    
          @_id = @_container.id
          json = @_container.json
          @name = json["Name"]
          @_created_at = json["Created"]
    
          @wait_for&.call(self)
    
          self
        rescue Docker::Error::NotFoundError => e
          raise NotFoundError, e.message
        rescue Excon::Error::Socket => e
          raise ConnectionError, e.message
        end
      end
    end
    
    using TestcontainersExtensions
  2. guilleiguaran commented on May 7, 2025

    @guilleiguaran
    Member

    maybe the project could support building an image natively from a local Dockerfile as part of the container lifecycle setup? This would make local development more seamless and reduce external dependencies for custom containers.

    @dieter-medium That's part of our roadmap, can you create a PR for it? If a different API is needed for this we can see to other testcontainers projects for inspiration about how the API would be.

  3. guilleiguaran commented on Oct 19, 2025

    @guilleiguaran
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions