Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gateway instead of 0.0.0.0 to allow docker in docker #8

Merged
merged 4 commits into from
May 14, 2019

Conversation

GuillaumeSmaha
Copy link
Contributor

@GuillaumeSmaha GuillaumeSmaha commented Aug 26, 2018

I want to use tox-docker inside docker (docker in docker). But using 0.0.0.0 or localhost doestn't work in this case.
That why I made this PR to add an option docker_use_gateway which use docker gateway ip to test connection with the container. I also add a new environment var XXX_HOST to save the gateway ip.

Finally, I replace name.upper() by a call to escape_env_var() because when you use a private registry, the name with the URL is not correctly escaped. Example: my.registry.com/myimage became MY.REGISTRY.COM/MYIMAGE instead of MY_REGISTRY_COM_MYIMAGE.

To test the failure case:
docker run --rm -ti -v $(pwd):/src -w /src -v /var/run/docker.sock:/var/run/docker.sock -v /etc/passwd:/etc/passwd -v /etc/group:/etc/group docker sh -c 'apk add python py-pip ; pip install . ; tox'
In tox.ini, if you set docker_use_gateway to True, the previous command will success.

Copy link
Member

@dcrosta dcrosta left a comment

Choose a reason for hiding this comment

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

Hey, sorry it took so long to get back to this -- I lost track of the github email in my inbox :(

@@ -6,6 +6,16 @@
from docker.errors import ImageNotFound
import docker as docker_module


def escape_env_var(varname):
Copy link
Member

Choose a reason for hiding this comment

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

Can you either add a test with realistic examples (preferred) or a docstring explaining why this is necessary?

Copy link
Contributor Author

@GuillaumeSmaha GuillaumeSmaha Sep 25, 2018

Choose a reason for hiding this comment

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

I will do both.

tox_docker.py Outdated
@@ -54,6 +64,9 @@ def tox_runtest_pre(venv):
conf._docker_containers.append(container)

container.reload()
gateway_ip = "0.0.0.0"
if conf.docker_use_gateway and container.attrs["NetworkSettings"]["Gateway"]:
Copy link
Member

Choose a reason for hiding this comment

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

Is the gateway sometimes not available? I'd love for this to be the default way to do things, rather than an option, in order to keep the interface smaller, but if it's sometimes not possible, that's a bummer.

Copy link
Contributor Author

@GuillaumeSmaha GuillaumeSmaha Sep 25, 2018

Choose a reason for hiding this comment

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

I will check if it is possible that the gateway is unavailable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to always be set. So I will remove the parameter docker_use_gateway.

@GuillaumeSmaha GuillaumeSmaha force-pushed the use_gateway branch 2 times, most recently from c476f66 to d9b7022 Compare September 25, 2018 18:34
@GuillaumeSmaha
Copy link
Contributor Author

@dcrosta It is ok, Thank for your answer :)
Now, what do you think about the PR ?

Copy link
Member

@dcrosta dcrosta left a comment

Choose a reason for hiding this comment

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

Sorry this slipped off my radar again! I'll try to be more responsive.


class ToxDockerRegistryTest(unittest.TestCase):
def test_it_sets_specific_env_vars(self):
self.assertEqual("env-var-value", os.environ["ENV_VAR"])
Copy link
Member

Choose a reason for hiding this comment

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

We don't need this test case replicated in the other test env.

def test_it_sets_automatic_env_vars(self):
# the nginx image we use exposes port 80
self.assertIn("DOCKER_IO_LIBRARY_NGINX_HOST", os.environ)
self.assertIn("DOCKER_IO_LIBRARY_NGINX_80_TCP", os.environ)
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me that this should be what you have here vs. eg NGINX_80_TCP. What's the use case for having multiple containers with the same name running (if they come from different regstries)?

@tox-dev/maintainers any opinions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay, I miss the notification for your replies.
The aim is to replace all invalid characters in the environment variable name.
Without the fix, an image from docker.io/library/nginx:1.13-alpine (or another private registry) will create an env var with the name DOCKER.IO_LIBRARY_NGINX_80_TCP which is not valid for a environment var.

name.upper(),
containerport.replace("/", "_").upper(),
)
envvar = escape_env_var("{}_HOST".format(
Copy link
Member

Choose a reason for hiding this comment

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

Could you add documentation on this to README.md?

@GuillaumeSmaha
Copy link
Contributor Author

GuillaumeSmaha commented Nov 28, 2018

Since the commit for UDP check (63b80bf), my test fails and currently, I don't know why it works with the 0.0.0.0 and not with the gateway

@dcrosta dcrosta mentioned this pull request May 4, 2019
@dcrosta dcrosta merged commit 5810ea9 into tox-dev:master May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants