terraform-provider-docker
terraform-provider-docker copied to clipboard
Duplicate docker_network with same NAME : cause error during attachment
This issue was originally opened by @kdefives as https://github.com/hashicorp/terraform-provider-docker/issues/161. It was migrated here as a result of the community provider takeover from @kreuzwerker. The original body of the issue is below.
Hi there,
Terraform Version
Terraform v0.11.13
+ provider.docker v1.2.0
+ provider.null v2.1.2
Affected Resource(s)
- resource "docker_network"
Terraform Configuration Files
# filename: networks.tf
resource "docker_network" "public_bridge_network" {
name = "public_ghost_network"
driver = "bridge"
}
#failename: main.tf
resource "docker_container" "blog_container" {
name = "ghost_blog"
image = "${docker_image.ghost_image.name}"
depends_on = ["null_resource.sleep", "docker_container.mysql_container"]
env = [
"database__client=mysql",
"database__connection__host=${var.mysql_network_alias}",
"database__connection__user=${var.ghost_db_username}",
"database__connection__password=${var.mysql_root_password}",
"database__connection__database=${var.ghost_db_name}"
]
ports {
internal = "2368"
external = "${var.ext_port}"
}
networks_advanced {
name = "${docker_network.public_bridge_network.name}"
aliases = ["${var.ghost_network_alias}"]
}
}
Expected Behavior
What should have happened? In my opinion, before creating the network, the Docker provider should check if the network name that we are trying to create already exists or not. In order to not have multiple networks with the same name. Moreover, by using docker command line, docker does not allow that as you can see:
[cloud_user@kdefives1c test_networks2]$ docker network create public_ghost_network
Error response from daemon: network with name public_ghost_network already exists
Because due to that, if there are 2 networks with same name, it is impossible to attach the network to a resource "docker_container" with the attribute "networks_advanced". If we try, Terraform apply will crash with the error message below:
Error: Error applying plan:
1 error(s) occurred:
* docker_container.mysql_container: 1 error(s) occurred:
* docker_container.mysql_container: Unable to start container: Error response from daemon: Could not attach to network ghost_mysql_internal: rpc error: code = NotFound desc = network ghost_mysql_internal not found
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
Steps to Reproduce
Please list the steps required to reproduce the issue, for example:
- Create a local repository and copy the networks.tf file
- Execute a plan apply to create the network
- As below, the network public_ghost_network will be correctly created:
[cloud_user@kdefives1c test_networks2]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
e9035fa2cfca bridge bridge local
85f208d95f44 docker_gwbridge bridge local
14c1057397d3 host host local
icng26xy77dl ingress overlay swarm
dc96a683a4dd none null local
4124fe91f9c7 public_ghost_network bridge local
- Now create another local repository and redo the step 1 and 2 on this new repository. You will see that Terraform create another network ID with the same NAME:
[cloud_user@kdefives1c test_networks2]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
e9035fa2cfca bridge bridge local
85f208d95f44 docker_gwbridge bridge local
14c1057397d3 host host local
icng26xy77dl ingress overlay swarm
dc96a683a4dd none null local
4124fe91f9c7 public_ghost_network bridge local
6d2a11b21a01 public_ghost_network bridge local
Important Factoids
I am using a simple cluster docker swarm :
[cloud_user@kdefives1c test_networks2]$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
05594caql183tvde45wctlosv * kdefives1c.mylabserver.com Ready Active Leader 18.09.6
uh74dfnz5qkcjhtfxxla0mm3l kdefives2c.mylabserver.com Ready Active 18.09.6
Regards,
This issue is stale because it has been open 60 days with no activity.
Remove stale label or comment or this will be closed in 7 days.
If you don't want this issue to be closed, please set the label pinned.
This issue is stale because it has been open 60 days with no activity.
Remove stale label or comment or this will be closed in 7 days.
If you don't want this issue to be closed, please set the label pinned.
Any though on fixing this ? When a deployment fail for any reason, if the network was created during first attempt, it is not removed.
then, running apply again throw an error (network name is ambiguous) because the network will be created again with the same name.
Checking if the network already exists before it is created would be very helpfull
There is a check_duplicate, but setting it to true will stop the whole process. I am not sure what's the point of this to be set to false and create duplicate networks.
I think the check_duplicate set to true should avoid creating a duplicate network and continues instead of throwing an exception.