Docker-android containers with network separate from host

Hi,

I have an Android app that needs some micro-services to be running to be able to work correctly. So each micro-service (when started correctly) opens a specific port on localhost and listen on it. All the services are dockerized.

So currently, I have created a docker-compose files with all the necessary micro-services. A typical micro-service looks like this:

  my_service:
    image: my_docker_registry_url/my_service:latest
    ports:
      - 4002:4002
    environment:
      ...
    restart: unless-stopped
    logging:
      driver: json-file
      options:
        max-size: "200k"
        max-file: "10"
    depends_on:
      ...

In the snippet above you can see the port mapping I use in my docker-compose file.

So anyway, what I do then is to run ‘docker login …’ and ‘docker-compose up -d’ to start the services.

The problem I’m having is that the ports that are opened by the services are opened on the host and not inside the container. So inside the container ‘localhost:4002’ is not accessible.

On the host machine, however, ‘localhost:4002’ is accessible, but I don’t have any means to connect to it from within the container. Even if I did have a way, it’s not exactly a good solution. Because I’ll have port conflicts if I try to run concurrent builds on bitrise.

So the question I’m asking here is:

Is there a possibility of making each container(build) have it’s own network separate from the host and that when I start the services in each container the ports are mapped to that container’s network and not the host machine?

Please let me know if you have any questions or anything is unclear.

Latest update:
so if I start the docker-android container like

docker run --privileged --env CI=false --volume "$(pwd):/bitrise/src" --volume "/var/run/docker.sock:/var/run/docker.sock" -it --net=host bitriseio/docker-android:latest bash

(notice the --net=host)

…it works. the host ports are accessible to the container.

however, I don’t think I have control over how bitrise.io starts the docker-android container?
Also as i mentioned earlier, this solution, isn’t really desirable in case I need to run concurrent builds.

Just heard on the Slack channel that each build uses its own host, so I guess we should be good with this solution (or something similar). I need to have access to the ports I open from within the container.

I believe it should be possible to create a docker network, and then connect it to the current container, related docs: https://docs.docker.com/engine/reference/commandline/network_connect/#parent-command

To attach the current container to the network you have to find out the name of the container you’re in - there are some tips in this thread on doing so: https://stackoverflow.com/questions/20995351/docker-how-to-get-container-information-from-within-the-container

Just ran a test docker network ls on the current Linux/Android stack:

+ docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
2e7b0d05e921        bridge              bridge              local
475bfa0f4b33        host                host                local
f1fa53e277af        none                null                local

Hey @paymand,

Did you manage to solve the issue using docker networks?

Hey @ghaabor!

No, I gave up on that. The workaround I ended up using is to use the default gateway address:
DOCKER_HOST=$(ip route | grep default | awk '{print $3}')
And then I can check if the service has started properly:
bash ./scripts/wait-for-service.sh $DOCKER_HOST:4002

1 Like

So that works, you can communicate through that IP right? Seems like a neat workaround for now @paymand :wink:

We’ll also check if there’s any reason not to specify the --net=host flag (whether it might break something), we’ll experiment with that next week.

Yeah, seems to be working. And yes, it would be great to look into using --net=host option.

Have a nice weekend! :grinning:

1 Like

Thanks for the infos, we’ll definitely experiment with this next week :wink:

Have a nice weekend :slight_smile:

Hey @paymand,

We’ve updated our docker command so we’ll be running the containers with --network=host flag: Weekly Virtual Machine / Stack updates - 2017.11.04 - Xcode 9.1 final, Xcode 9.2 beta

It’s rolling out tomorrow so you can try it on Sunday or next week – please ping us when you had the chance to test it. :slight_smile:

Happy building! :building_construction:

1 Like

Awesome! it seems to work fine. Updating my workflow, thanks.

1 Like

This topic was automatically closed after 14 days. New replies are no longer allowed.