Docker Compose Fails because network has active endpoints

Many years ago an old colleague created a series of local servers with Docker to develop and test our app.

It happens that a few weeks ago one of the servers refuses to work, always displaying the message "ERROR: error while removing network: network docker_myapp id df1e3b1 has active endpoints" .

Unfortunately I have no technical knowledge of how Docker works. What can I do to correct the problem?

Our environment is separated into 3 environments:

  • DB (PostgreSQL)
  • BackEnd (PHP and Apache)
  • FrontEnd (NGINX)

First we run this command to start the database container (every day):

#!/bin/bash
 
source ./docker/bin/variables
 
REBUILD=true
if [[ $* == *--no-rebuild* ]]; then
  if docker history -q $IMAGE >/dev/null 2>&1; then
    echo "[run_build] will not rebuild image $IMAGE"
    REBUILD=false
  fi
fi
 
if $REBUILD; then
 
  echo "[run_build] start docker image build"
 
  docker build -t $IMAGE -f ./docker/Dockerfile .
 
  echo "[run_build] docker image build finished"
 
fi

These are the Variables

    #!/bin/bash
     
    export REPO=$(basename $(git rev-parse --show-toplevel))
    export BRANCH=$(git symbolic-ref --short HEAD)
    export TAG=${BRANCH//[^a-zA-Z0-9\-]/_}
     
    export IMAGE=myapp/${REPO}:${TAG}
     
    NAMING_SUFFIX=${REPO}_${TAG}

And this is the Dockerfile for the database

    FROM postgres:12
    MAINTAINER Bob
     
    COPY . /opt/database

After having created the database, when executing the backend for the first time, we run the file run_build.sh from the backend, and to start the container every day we use run_dev.sh.

./docker/bin/run_build #only on the first run when there is no container
./docket/bin/run_dev #every day to start the container and work

#run_build content
#!/bin/bash
 
source ./docker/bin/variables
 
REBUILD=true
if [[ $* == *--no-rebuild* ]]; then
  if docker history -q $IMAGE >/dev/null 2>&1; then
    echo "[run_build] will not rebuild image $IMAGE"
    REBUILD=false
  fi
fi
 
if $REBUILD; then
 
  echo "[run_build] start docker image build"
 
  docker build -t $IMAGE -f ./docker/Dockerfile .
 
  echo "[run_build] docker image build finished"
 
fi

#run_dev content
#!/bin/bash
 
source ./docker/bin/variables
 
echo "[run_dev] calling /run_build"
./docker/bin/run_build --no-rebuild
 
echo "[run_dev] starting docker-compose for docker/compose.yml"
docker-compose --file=./docker/compose.yml up -d --force-recreate
 
echo "[run_dev] attaching to bash on $PROJECT_NAME"
(docker exec -it $PROJECT_NAME bash)
 
echo "[run_dev] tearing down docker/compose.dev.yml"
docker-compose --file=./docker/compose.yml down
 
echo "[run_dev] done"

#Dockerfile content

FROM php:7.0-apache
MAINTAINER Bob
 
RUN apt-get -qq update
RUN apt-get -qqy install libpq-dev libmcrypt-dev libreadline-dev git zip
RUN apt-get install libldap2-dev -y
RUN rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu
RUN docker-php-ext-install ldap
RUN docker-php-ext-install -j$(nproc) pgsql
RUN docker-php-ext-install -j$(nproc) mcrypt
 
RUN a2enmod headers
RUN adduser --uid 1000 --gecos 'local' --disabled-password local && chown -R "$APACHE_RUN_USER:$APACHE_RUN_GROUP" /var/lock/apache2 /var/run/apache2 /var/log/apache2
 
COPY . /var/www/html/

After starting the database and the backend (these are working correctly so far), we create the front-end container with run_build (if it doesn’t exist), and then run run_dev every day to start the container (this has given the problem active network).

./docker/bin/run_build #only on the first run when there is no container, same content from backend
./docket/bin/run_dev #every day to start the container and work, same content from backend

#run_build content
#!/bin/bash
 
source ./docker/bin/variables
 
REBUILD=true
if [[ $* == *--no-rebuild* ]]; then
  if docker history -q $IMAGE >/dev/null 2>&1; then
    echo "[run_build] will not rebuild image $IMAGE"
    REBUILD=false
  fi
fi
 
if $REBUILD; then
 
  echo "[run_build] start docker image build"
 
  docker build -t $IMAGE -f ./docker/Dockerfile .
 
  echo "[run_build] docker image build finished"
 
fi

#run_dev content

#Dockerfile content

FROM node:4
MAINTAINER Bob
 
WORKDIR /opt/app
COPY . /opt/app
 
RUN npm install
RUN npm install -g bower
RUN npm install -g grunt
RUN bower install --allow-root
 
CMD ["grunt", "serve"]

I thought the problem could be something during the grunt command of the server, but I removed the execution of the RUN npm commands, but the problem persisted.

I’ve tried it on Ubuntu and macOS, and the error always occurs.

What may be happening and how can I fix it?

I tried to run the command docker network rm docker_myapp, but this message is displayed Error response from daemon: error while removing network: network docker_myapp id df1e3b1 has active endpoints.

The only containers that are active using this network are the database container and the container with the backend, which have always been run before the frontend.
They cannot be removed as the frontend depends on it to load the data.

Looking at the list of commands I realized that there is an error when executing the command (docker exec -it $PROJECT_NAME bash), the error is Error response from daemon: Container bb65b5cf4c9 is not running, But by the ID I don’t know which project was supposed to be running.

$ docker-compose --file=./docker/compose.yml up -d --force-recreate
WARNING: Found orphan containers (myapp-back-end_master, postgres.myapp-back-end_master) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating myapp-front-end_master ... done
Creating nginx.myapp-front-end_master ... done

$ (docker exec -it $PROJECT_NAME bash)
Error response from daemon: Container ca696dec1b8e4 is not running