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

Add a default value for the docker.network configuration #3471

Merged
merged 10 commits into from
Jun 13, 2018
16 changes: 15 additions & 1 deletion docs/configuration/backends/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ usebindportip = true
#
swarmMode = false

# Define a default docker network to use for connections to all containers.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should add the fact that it's possible to override this with the label traefik.docker.network

Copy link
Member Author

Choose a reason for hiding this comment

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

done

# Can be overridden by the traefik.docker.network label.
#
# Optional
#
network = "web"

# Enable docker TLS connection.
#
# Optional
Expand Down Expand Up @@ -125,6 +132,13 @@ watch = true
#
swarmMode = true

# Define a default docker network to use for connections to all containers.
# Can be overridden by the traefik.docker.network label.
#
# Optional
#
network = "web"

# Override default configuration template.
# For advanced users :)
#
Expand Down Expand Up @@ -195,7 +209,7 @@ Labels can be used on containers to override default behavior.

| Label | Description |
|------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `traefik.docker.network` | Set the docker network to use for connections to this container. [1] |
| `traefik.docker.network` | Override the default docker network to use for connections to this container. [1] |
| `traefik.domain` | Default domain used for frontend rules. |
| `traefik.enable=false` | Disable this container in Træfik |
| `traefik.port=80` | Register this port. Useful when the container exposes multiples ports. |
Expand Down
2 changes: 1 addition & 1 deletion provider/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (p *Provider) getFrontendRule(container dockerData, segmentLabels map[strin
}

func (p Provider) getIPAddress(container dockerData) string {
if value := label.GetStringValue(container.Labels, labelDockerNetwork, ""); value != "" {
if value := label.GetStringValue(container.Labels, labelDockerNetwork, p.Network); value != "" {
networkSettings := container.NetworkSettings
if networkSettings.Networks != nil {
network := networkSettings.Networks[value]
Expand Down
100 changes: 99 additions & 1 deletion provider/docker/config_container_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,83 @@ func TestDockerBuildConfiguration(t *testing.T) {
CircuitBreaker: nil,
},
},
}, {
desc: "when basic container configuration with multiple network",
containers: []docker.ContainerJSON{
containerJSON(
name("test"),
ports(nat.PortMap{
"80/tcp": {},
}),
withNetwork("bridge", ipv4("127.0.0.1")),
withNetwork("webnet", ipv4("127.0.0.2")),
),
},
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test-docker-localhost-0": {
Backend: "backend-test",
PassHostHeader: true,
EntryPoints: []string{},
BasicAuth: []string{},
Routes: map[string]types.Route{
"route-frontend-Host-test-docker-localhost-0": {
Rule: "Host:test.docker.localhost",
},
},
},
},
expectedBackends: map[string]*types.Backend{
"backend-test": {
Servers: map[string]types.Server{
"server-test": {
URL: "http://127.0.0.2:80",
Weight: label.DefaultWeight,
},
},
CircuitBreaker: nil,
},
},
},
{
desc: "when basic container configuration with specific network",
containers: []docker.ContainerJSON{
containerJSON(
name("test"),
labels(map[string]string{
"traefik.docker.network": "mywebnet",
}),
ports(nat.PortMap{
"80/tcp": {},
}),
withNetwork("bridge", ipv4("127.0.0.1")),
withNetwork("webnet", ipv4("127.0.0.2")),
withNetwork("mywebnet", ipv4("127.0.0.3")),
),
},
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test-docker-localhost-0": {
Backend: "backend-test",
PassHostHeader: true,
EntryPoints: []string{},
BasicAuth: []string{},
Routes: map[string]types.Route{
"route-frontend-Host-test-docker-localhost-0": {
Rule: "Host:test.docker.localhost",
},
},
},
},
expectedBackends: map[string]*types.Backend{
"backend-test": {
Servers: map[string]types.Server{
"server-test": {
URL: "http://127.0.0.3:80",
Weight: label.DefaultWeight,
},
},
CircuitBreaker: nil,
},
},
},
{
desc: "when container has label 'enable' to false",
Expand Down Expand Up @@ -420,6 +497,7 @@ func TestDockerBuildConfiguration(t *testing.T) {
provider := &Provider{
Domain: "docker.localhost",
ExposedByDefault: true,
Network: "webnet",
}
actualConfig := provider.buildConfigurationV2(dockerDataList)
require.NotNil(t, actualConfig, "actualConfig")
Expand Down Expand Up @@ -938,6 +1016,24 @@ func TestDockerGetIPAddress(t *testing.T) {
),
expected: "127.0.0.1",
},
{
container: containerJSON(
networkMode("host"),
withNetwork("testnet", ipv4("10.11.12.13")),
withNetwork("webnet", ipv4("10.11.12.14")),
),
expected: "10.11.12.14",
},
{
container: containerJSON(
labels(map[string]string{
labelDockerNetwork: "testnet",
}),
withNetwork("testnet", ipv4("10.11.12.13")),
withNetwork("webnet", ipv4("10.11.12.14")),
),
expected: "10.11.12.13",
},
{
container: containerJSON(
networkMode("host"),
Expand All @@ -962,7 +1058,9 @@ func TestDockerGetIPAddress(t *testing.T) {
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
dData.SegmentLabels = segmentProperties[""]

provider := &Provider{}
provider := &Provider{
Network: "webnet",
}

actual := provider.getIPAddress(dData)
assert.Equal(t, test.expected, actual)
Expand Down
1 change: 1 addition & 0 deletions provider/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Provider struct {
ExposedByDefault bool `description:"Expose containers by default" export:"true"`
UseBindPortIP bool `description:"Use the ip address from the bound port, rather than from the inner network" export:"true"`
SwarmMode bool `description:"Use Docker on Swarm Mode" export:"true"`
Network string `description:"Default Docker network used" export:"true"`
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we call it DefaultNetwork instead?

Copy link
Member

Choose a reason for hiding this comment

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

To be homogeneous with domain, we need to keep Network.

}

// dockerData holds the need data to the Provider p
Expand Down