Jump to content

Automate docker network connect after start a container

Recommended Posts

First of all, I'm new to Unraid and I don't know if this functionality exists or if there's a better way to do it.


I have a maybe a bit complex setup.

On Unraid server I have Nextcloud AIO installed from the official docker image and I would like to make it accessible over the internet.

The company that provides my internet connection does not give me any access to the router (equipment that converts a fiber optic signal to an Ethernet cable), and because of this I cannot allow access to external ports or do port forwarding, etc. . Also, it gives me a dynamic IP address, which tends to change frequently.

One of the ways around this is by using VPN.

I then bought a domain name, rented a low latency VPS server for my region, and created a VPN network using Netmaker installed via docker.

On my VPS server, I have Caddy as a reverse proxy that forwards requests to the Netmarker client host which effectively gives me access to the exposed ports of the Unraid server.

All of this has been working fine, that is, I have no problems accessing my Nextcloud server via my domain.


Although the setup worked fine, there is a problem: When I am indoors accessing the same network as the Unraid server, when the Nextcloud mobile app uploads something, the request is sent via the internet to my VPS server and then via the network VPN goes back to Unraid server. Since I pay data traffic on the VPS server and my internet connection, this is not a good thing.

The solution for this was:

1 - Create a new container with Caddy with a fixed IP that reverse proxy directly to the Nextcloud containers without going through the VPN 
     > I have a cron task that from time to time enters my VPS server and copies the Caddy certificates from the VPS to the Caddy running in Unraid and keeps the Unraid Caddy in sync.

2 - Install Pi-hole and include a dns record to resolve my domain address directly to the Caddy IP running in Unraid.

This is the configuration I have now for Caddy running on Unraid:


Nextcloud AIO creates its own docker network named nextcloud-aio.

To make Caddy in Unraid have access to Nextcloud's internal network (aka nextcloud-aio docker network), it is necessary manually execute


docker network connect nextcloud-aio caddy-local-proxy

Connecting the network manually works fine, however the configuration does not resist the way Unraid recreates the containers, that is, if you click on edit the caddy-local-proxy container for example and apply it, Unraid will delete the previous container and create it again, however the previously connected extra networks will not be connected again.

The Feature Request:

One way I thought to solve this, is to include an additional step to be performed automatically by Unraid when Unraid starts and/or recreates the containers:

Create a new UI way for the user to add via the dropdown selector (something as shown in the Network Type dropdown) which additional networks they want to connect to.

From that list, run docker network connect

Currently, I have a cron job to check this from time to time and ensure that a given container has access to a given network, but I think if that functionality or better functionality doesn't exist, it might be a nice addition to Unraid.



# Define the list of container-network pairs
    "caddy-local-proxy nextcloud-aio"
    # Add more pairs as needed

# Wait until Docker daemon is available
while ! docker info >/dev/null 2>&1; do
    echo "Waiting for Docker daemon to start..."
    sleep 1

# Function to check and connect container-network pairs
check_and_connect_network() {
    local container="${1}"
    local network="${2}"

    # Wait until the container is running
    while ! docker container inspect -f '{{.State.Running}}' "${container}" >/dev/null 2>&1; do
        echo "Waiting for container '${container}' to start..."
        sleep 1

    # Check if the container is connected to the network
    if docker container inspect "${container}" | jq -e --arg network "${network}" '.[0].NetworkSettings.Networks[$network]' >/dev/null 2>&1; then
        echo "Container '${container}' is already connected to the '${network}' network."
        echo "Container '${container}' is not connected to the '${network}' network. Connecting..."
        if docker network connect "${network}" "${container}" >/dev/null 2>&1; then
            echo "Successfully connected '${container}' to the '${network}' network."
            echo "Failed to connect '${container}' to the '${network}' network."
            exit 1

# Iterate over the container-network pairs and check/connect each pair
for pair in "${container_network_pairs[@]}"; do
    container="$(echo "${pair}" | awk '{print $1}')"
    network="$(echo "${pair}" | awk '{print $2}')"
    check_and_connect_network "${container}" "${network}"

exit 0


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...