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

Set the ip of the docker container #81

Closed
adixia opened this issue Apr 1, 2019 · 7 comments
Closed

Set the ip of the docker container #81

adixia opened this issue Apr 1, 2019 · 7 comments
Assignees
Labels
enhancement under-development Denotes that a issue is under development but not yet released. waiting-for-feedback Waiting for someone to give feedback until this issue can be brought further.

Comments

@adixia
Copy link

adixia commented Apr 1, 2019

Hello,
I would like to create a docker container from a dockerfile and set its IP, e.g 1.1.1.1, on a custom network called mynetwork, like this:
docker run --net mynetwork --ip '1.1.1.1' -it mycontainer
is it possible with FluentDocker?

Thank you for the support
Alessandro

mariotoffia pushed a commit that referenced this issue Apr 2, 2019
It has been added to the create params and therefore the run and
create command can handle explicit set ip v4 or v6 on the container.
In addition, the container builder has UseIpV4 and V6 respectively
to allow for explicit ip address on container via the fluent API.
@mariotoffia mariotoffia added enhancement waiting-for-feedback Waiting for someone to give feedback until this issue can be brought further. under-development Denotes that a issue is under development but not yet released. labels Apr 2, 2019
@mariotoffia mariotoffia self-assigned this Apr 2, 2019
@mariotoffia mariotoffia added this to To do in FluentDocker via automation Apr 2, 2019
@mariotoffia
Copy link
Owner

Hi, I've added support for ipv4 and v6 for both run and create commands. I've also added support for the explicit ip setting on container in the fluent API. Can you checkout the code and have a go to reproduce your specific case? Can you submit that as a unit test as a PR?

I've added test but have not the time to setup a proper network as of now to test this. But along the lines of.

        var cmd = _docker.Run("postgres:9.6-alpine", new ContainerCreateParams
        {
          PortMappings = new[] {"40001:5432"},
          Environment = new[] {"POSTGRES_PASSWORD=mysecretpassword"},
          Network = "mynetwork",
          Ipv4 = "1.1.1.1"
        }, _certificates);
        
        var container = cmd.Data;
        var insp = _docker.InspectContainer(container, _certificates);
        var ip = insp.Data.NetworkSettings.IPAddress;
        Assert.AreEqual("1.1.1.1", ip);

The above example will create a new container using network mynetwork and set the static ip of 1.1.1.1. The fluent API would look something like this:

      using (var nw = Fd.UseNetwork("mynetwork").Build())
      {
        using (
          var container =
            Fd.UseContainer()
              .WithName("mycontainer")
              .UseImage("postgres:9.6-alpine")
              .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
              .ExposePort(5432)
              .UseNetwork(nw)
              .UseIpV4("1.1.1.1")              
              .WaitForPort("5432/tcp", 30000 /*30s*/)
              .Build()
              .Start())
        {
          var ip = container.GetConfiguration().NetworkSettings.IPAddress;
          Assert.AreEqual("1.1.1.1", ip);
        }
      }

Cheers,
Mario

mariotoffia pushed a commit that referenced this issue Apr 2, 2019
It has been added to the create params and therefore the run and
create command can handle explicit set ip v4 or v6 on the container.
In addition, the container builder has UseIpV4 and V6 respectively
to allow for explicit ip address on container via the fluent API.
@adixia
Copy link
Author

adixia commented Apr 2, 2019

Hi Mario,
I am sorry but I am a junior Developer and I don't know how to do the unit Tests correctly...

Anyway I created my own network and then I tried to set the ip of the newer container to one in that network via fluent API, but it raises me this exception :

user specified IP address is supported on user defined networks only Error: failed to start containers....

I think this happens because the "--ip" arguments has to be added after the name of the custom network, and I don't know if this is possible with the API... Thank you for the support.

@mariotoffia
Copy link
Owner

Thanks @adixia ! Can you checkout and try again? --ip or --ip6 are now emitted after the --network.

Cheers,
Mario

@adixia
Copy link
Author

adixia commented Apr 4, 2019

Hi Mario , still receiving
user specified IP address is supported on user defined networks only...
I think that Docker doesn't understand that the IP I passed it's for the new container when it uses the custom network.

Thanks

@adixia
Copy link
Author

adixia commented Apr 10, 2019

Hi Mario,
I've inspected deeper this issue and i found that the problem is the parameters order in the command.
The proper command should be:
'docker run --net mynetwork --ip '1.1.1.1' -it mycontainer'.
the --ip flag has to be added as the next command after the name of the custom network, otherwise it doesn't work.
Sorry for bothering you, I hope you can help me.

Have a nice day.

@mariotoffia
Copy link
Owner

neat, thanks I'll update and release tomorrow.
cheers
mario

mariotoffia pushed a commit that referenced this issue Apr 11, 2019
This solves the issue #81 where --network needs to be emitted when
static ip is assigned to the container. It also adds two unit tests
to ensure both command and fluent API is working properly.
@mariotoffia
Copy link
Owner

I'll release the new version 2.6.6 now (it will be available in a few hours - depending on nuget indexing). I've also created two unit test to ensure that it is really working:

This is the fluent test.

      using (var nw = Fd.UseNetwork("unit-test-nw")
                        .UseSubnet("10.18.0.0/16").Build())
      {
        using (
          var container =
            Fd.UseContainer()
              .WithName("mycontainer")
              .UseImage("postgres:9.6-alpine")
              .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
              .ExposePort(5432)
              .UseNetwork(nw)
              .UseIpV4("10.18.0.22")              
              .WaitForPort("5432/tcp", 30000 /*30s*/)
              .Build()
              .Start())
        {
          var ip = container.GetConfiguration().NetworkSettings.Networks["unit-test-nw"].IPAddress;
          Assert.AreEqual("10.18.0.22", ip);
        }
      }

… and this is the command version of the same test...

        var created = _docker.NetworkCreate("unit-test-nw", new NetworkCreateParams
        {
          Subnet  = new [] {"10.18.0.0/16"}
        }, _certificates);
        
        Assert.IsTrue(created.Success);
        id = created.Data[0];

        var cmd = _docker.Run("postgres:9.6-alpine", new ContainerCreateParams
        {
          PortMappings = new[] {"40001:5432"},
          Environment = new[] {"POSTGRES_PASSWORD=mysecretpassword"},
          Network = "unit-test-nw",
          Ipv4 = "10.18.0.22"
        }, _certificates);
        
        Assert.IsTrue(cmd.Success);
        container = cmd.Data;

        var insp = _docker.InspectContainer(container, _certificates);
        Assert.IsTrue(insp.Success);

        var ip = insp.Data.NetworkSettings.Networks["unit-test-nw"].IPAddress;
        Assert.AreEqual("10.18.0.22", ip);

Cheers,
Mario

FluentDocker automation moved this from To do to Done Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement under-development Denotes that a issue is under development but not yet released. waiting-for-feedback Waiting for someone to give feedback until this issue can be brought further.
Projects
FluentDocker
  
Done
Development

No branches or pull requests

2 participants