0

Let's assume scenario I'm using a set of CLI docker run commands for creating a whole environment of containers, networks (bridge type in my case) and connect containers to particular networks.

Everything works well till the moment I want to have only one such environment at a single machine.

But what if I want to have at the same machine a similar environment to the one I've just created but for a different purpose (testing) I'm having an issue of name collisions since I can't crate and start containers and networks with the same name.

So far I tried to start second environment the same way I did with the first but with prefixing all containers and networks names.
That worked but had a flaw: in the application that run all requests to URIs were broken since they had a structure

<scheme>://<container-name>:<port-number>

and the application was not able to reach <prefix-container-name>.

What I want to achieve is to have an exact copy of the first environment running on the same machine as the second environment that I could use to perform the application tests etc.

  1. Is there any concept of namespaces or something similar to it in Docker?
    A command that I could use before all docker run etc commands I use to create environment and have just two bash scripts that differ only by the namespace command at their beginning?
  2. Can using virtual machine, ie Oracle Virtualbox be the solution to my problem? Create a VM for the second environment? isn't that an overkill, will it add an additional set of troubles?
  3. Perhaps there is a kind of --hostname for docker run command that will allow to access the container from other container by using this name? Unlucky --hostname only gives ability to access the container by this name form the container itself but not from any other. Perhaps there is an option or command that can make an alias, virtual host or whatever magic common name I could put into apps URIs <scheme>://<magic-name>:<port-number> so creating second environment with different containers and networks names will cause no problem as long as that magic-name is available in the environment network
  4. My need for having exact copy of the environment is because of tests I want to run and check if they fail also on dependency level, I think this is quite simple scenario from the continues integration process. Are there any dedicated open source solutions to what I want to achieve? I don't use docker composer but bash script with all docker cli commands to get the whole env up and running.

Thank you for your help.

Jimmix
  • 5,644
  • 6
  • 44
  • 71

1 Answers1

0

Is there any concept of namespaces or something similar to it in Docker?

Not really, no (but keep reading).

Can using virtual machine [...] be the solution to my problem? ... Isn't that an overkill, will it add an additional set of troubles?

That's a pretty reasonable solution. That's especially true if you want to further automate the deployment: you should be able to simulate starting up a clean VM and then running your provisioning script on it, then transplant that into your real production environment. Vagrant is a pretty typical tool for trying this out. The biggest issue will be network connectivity to reach the individual VMs, and that's not that big a deal.

Perhaps there is a kind of --hostname for docker run command that will allow to access the container from other container by using this name?

docker run --network-alias is very briefly mentioned in the docker run documentation and has this effect. docker network connect --alias is slightly more documented and affects a container that's already been created.

Are there any dedicated open source solutions to what I want to achieve?

Docker Compose mostly manages this for you, if you want to move off of your existing shell-script solution: it puts a name prefix on all of the networks and volumes it creates, and creates network aliases for each container matching its name in the YAML file. If your host volume mounts are relative to the current directory then that content is fairly isolated too. The one thing you can't easily do is launch each copy of the stack on a separate host port(s), so you have to resolve those conflicts.

Kubernetes has a concept of a namespace which is in fact exactly what you're asking for, but adopting it is a substantial investment and would involve rewriting your deployment sequence even more than Docker Compose would.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • `--network-alias` worked as expected but I cannot run the second environment due to error when creating a network: `Error response from daemon: Pool overlaps with other one on this address space` In the first env I create network this way `docker network create --subnet=172.18.0.0/16 foo` and in second env `docker network create --subnet=172.18.0.0/16 boo` which is the same address space but I thought the address space was one per a network and it looks to me it is one per docker demon - is that right? if so how to run second env with exactly the same network settings as the first at same time – Jimmix Jan 06 '19 at 21:01