Skip to content
This repository was archived by the owner on May 9, 2022. It is now read-only.

Fix additional container options#19

Merged
dpmex4527 merged 8 commits into
masterfrom
fix-container-options
Aug 8, 2019
Merged

Fix additional container options#19
dpmex4527 merged 8 commits into
masterfrom
fix-container-options

Conversation

@dpmex4527
Copy link
Copy Markdown
Member

This PR fixes a bug where any additional container options passed in to Hoosegow that use the :HostConfig key gets ignored due to changes introduced in #17. #17 started using the :HostConfig key for the :Binds configuration option. If a user passes in another :HostConfig configuration, such as options = {:HostConfig => { :ExtraHosts => "test:123"}}, these options will be ignored in favor of the default :Binds config when we .merge against the default container options being used.

This adds a new spec test that will mount a local directory and have the docker
container launched copy the `/etc/hosts` file to that directory. This is to
ensure that volume mounts still work as expected (see #17) and that any custom
@container_options passed in to Hoosegow that use the `:HostConfig` key get
properly configured. In this case, we're testing that the `:ExtraHost` option
properly adds the list of hostname/IP mappings to the containers `/etc/hosts`
file.
This lets us take advantage of the ActiveSupport's `deep_merge` hash method
to recursively merge hashes and not overwrite existing keys.
Activesupport's `deep_merge` hash method will recursively merge hashes so
that duplicate keys will not overwrite each other. #17 introduced a change
where we now configure volume binds at create time by using the `:HostConfig`
option to do this. This makes it so that any other custom docker option that
uses `:HostConfig` (such as `:ExtraHosts`) will get overwritten in the .merge
in favor of the default volume `:Bind` configuration.
@dpmex4527 dpmex4527 requested a review from spraints August 7, 2019 17:33
Copy link
Copy Markdown
Contributor

@spraints spraints left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this bug be fixed without bringing in activesupport? Adding it as a dependency will complicate the dependencies for pages and porter.

Comment thread spec/hoosegow_docker_spec.rb Outdated

it "configures ExtraHosts option" do
config = CONFIG.merge(
:Entrypoint => ['/bin/cp', '/etc/hosts', '/volume-test/hosts'],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I'm curious if the intent of this test is getting obscured by /etc/hosts and related config. Is /etc/hosts used as an example file so that the test can verify that something comes out on the volume? If so, would it make sense to use something that's easier to directly control? e.g. ['/bin/sh', '-c', 'echo THISISAMAGICTOKEN > /volume-test/out.txt'] or ['/bin/cp', '/hoosegow/inmate/inmate.rb', '/volume-test/inmate.rb'].

Copy link
Copy Markdown
Member Author

@dpmex4527 dpmex4527 Aug 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is /etc/hosts used as an example file so that the test can verify that something comes out on the volume?

The test serves two purposes: that the volume bind configuration introduced in #17 is not broken (we can copy any file into the mounted volume and read it outside of the container) and that we have successfully configured the :ExtraHosts option in the #create-a-container docker API. Below is the description for this option for the docker API under the :HostConfig key:

ExtraHosts - A list of hostnames/IP mappings to add to the container’s /etc/hosts file. Specified in the form ["hostname:IP"].

Another way to verify that the :ExtraHosts option is no longer being ignored and being properly set is to introduce a container_info method to directly check the running containers config:

    # Public: Get more information about the running container
    #
    # Returns response body or nil if no container is running.
    def container_info
      return unless @container
      @container.json
    end

This would give us docker inspect functionality and we can easily check to see that any configuration we've set is present:

image

Can this bug be fixed without bringing in activesupport?

Definitely! Selectively merging into an existing :HostConfig option would be the way to do this. Let me try a few things and get something up shortly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test serves two purposes: that the volume bind configuration introduced in #17 is not broken (we can copy any file into the mounted volume and read it outside of the container) and that we have successfully configured the :ExtraHosts option in the #create-a-container docker API.

Ah, word. I didn't realize there were two purposes. From a test purity perspective, it's better to check one thing per test. But for stuff like this sometimes it's easier to check just one thing. 👍 on using /etc/hosts if you'd like to keep just one test here.

- Pull out default container options into seperate method
- Persist additional `:HostConfig` options by selectively merging into default
  `:HostConfig` options
- Remove volume bind check as we already test this in unit tests above
- Instead of reading off of /etc/hosts, use getent to verify
  that we have added the entries indirectly. Docker can decide to change how
  they plug into the /etc/hosts file with a different format down the road.
@dpmex4527 dpmex4527 merged commit f8ba0d2 into master Aug 8, 2019
@dpmex4527 dpmex4527 deleted the fix-container-options branch August 8, 2019 14:59
@dpmex4527 dpmex4527 mentioned this pull request Aug 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants