Containers are started twice

I use ubuntu 22.04 and I want to run nextcloud within containers and use the example you can find here. I use it more or less one by one.

I did my first test in a virtual machine and everything works fine.

I moved it to a real machine and everything worked fine.

But all of a sudden the system doesn’t run anymore. The reason is that all containers that should be started by docker-compose.yml are started twice. And this is not working, obviously.

If I move my files one by one to a different user on the same machine, I have the same result.

If I move my files one by one to a different machine (VM) it works fine.

Conclusion: there some muddle in the docker-settings. But WHAT??

I reinstalled docker-compose with no success.

Any hint?

If you compare the output of docker inspect ${container id or name} --format '{{json .Config.Labels}}' | jq for the duplicate containers, you should see that they differ in their labels. If you don’t see the difference, please share the outputs with us.

I am quite sure you deployed the same compose file (or a copy of it) from a different folder, therefore causing the deployments to use the folder name as project name - thus you end up with two different deployments.

This is the output of one of the tests. I’m pretty sure, that there is no other container running.

# Start with no continer running
media@g1905:~/docker/myserver$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# This is my setup
media@g1905:~/docker/myserver$ ls -l
insgesamt 12
-rw-rw-r-- 1 media media 2730 Mär 19 14:28 docker-compose.yml
-rw-rw-r-- 1 media media   79 Mär 19 14:28 nc-db.env
drwxrwxr-x 2 media media 4096 Mär 19 14:28 proxy

# I try to run docker-compose 
media@g1905:~/docker/myserver$ docker-compose up
Creating myserver_db_1 ... 
Creating myserver_redis_1 ... 
Creating myserver_proxy_1 ... 
Creating myserver_proxy_1 ... error

Creating myserver_db_1    ... done
Creating myserver_redis_1 ... done
 address already in use
Creating myserver_app_1   ... done
Creating myserver_cron_1  ... done

ERROR: for proxy  Cannot start service proxy: driver failed programming external connectivity on endpoint myserver_proxy_1 (284029aeb22c4c222b21961390b8efa8173229116791266bf6d1914c99ce8744): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use
ERROR: Encountered errors while bringing up the project.

# Stop all running containers. Only 4 are running
media@g1905:~/docker/myserver$ docker-compose stop
Stopping myserver_app_1   ... done
Stopping myserver_cron_1  ... done
Stopping myserver_redis_1 ... done
Stopping myserver_db_1    ... done

# Remove all containers. There are 5 containers to be removed.
media@g1905:~/docker/myserver$ docker-compose rm
Going to remove myserver_app_1, myserver_cron_1, myserver_proxy_1, myserver_redis_1, myserver_db_1
Are you sure? [yN] y
Removing myserver_app_1   ... done
Removing myserver_cron_1  ... done
Removing myserver_proxy_1 ... done
Removing myserver_redis_1 ... done
Removing myserver_db_1    ... done

# Move everythin to a different folder and start with new container names
media@g1905:~/docker/myserver$ mkdir ../my-server
media@g1905:~/docker/myserver$ cp . ../my-server/ -r
media@g1905:~/docker/myserver$ cd ../my-server/
media@g1905:~/docker/my-server$ docker-compose up
Building proxy
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginxproxy/nginx-proxy:alpine
 ---> fa9f6d1e8c07
Step 2/2 : COPY uploadsize.conf /etc/nginx/conf.d/uploadsize.conf
 ---> Using cache
 ---> 9f04ddb5326b
Successfully built 9f04ddb5326b
Successfully tagged my-server_proxy:latest
WARNING: Image for service proxy was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating my-server_proxy_1 ... 
Creating my-server_proxy_1 ... error
Creating my-server_db_1    ... 
WARNING: Host is already in use by another container

docker compose drives the docker api, which always runs as root, so deploying the compose file with the same project name will be detected as the same deployments.

The project name is set by using -p or --project-name. If it’s not set, the directory name is used as fallback (in your case myserver and my-server).

Though what exactly do you mean by double containers? I see a different error message regarding non unique host names.

The active user is added to the groupe ‘docker’. So I think it is not necessary to run it as root. (??)

As you can see here all containers, and proxy especially are started twice. And the second start runs in an error because resources of the first instance are already in use.

# I try to run docker-compose 
media@g1905:~/docker/myserver$ docker-compose up
Creating myserver_db_1 ... 
Creating myserver_redis_1 ... 
Creating myserver_proxy_1 ... 
Creating myserver_proxy_1 ... error

The docker engine runs as root. Adding an unprivliged user to the docker group, and therefore allowing it to access the docker.sock to interact with the docker engine, doesn’t change that fact…

I am not sure what I am seeing, unless I see the output of docker ps -a. It is not possible that two containers with identical container name are created. Looks more like it tries to create the container, but then encounters an error.

Please share the content of your compose file.

This is my yml: (domain name replaced by dummy, but this doesn’t matter here…)

media@g1905:~/docker/myserver$ cat docker-compose.yml 
version: '3'

services:
  db:
    image: mariadb:10.5
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root-mysql-pw
      - MARIADB_AUTO_UPGRADE=1
      - MARIADB_DISABLE_UPGRADE_BACKUP=1
    env_file:
      - nc-db.env

  redis:
    image: redis:alpine
    restart: always

  app:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html
    environment:
      - VIRTUAL_HOST=nextcloud.this-is-a-valid-domain.de
      - LETSENCRYPT_HOST=nextcloud.this-is-a-valid-domain.de
      - LETSENCRYPT_EMAIL=info@this-is-a-valid-domain.de
      - MYSQL_HOST=db
      - REDIS_HOST=redis
    env_file:
      - nc-db.env
    depends_on:
      - db
      - redis
    networks:
      - proxy-tier
      - default

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

  proxy:
    build: ./proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
    volumes:
      - certs:/etc/nginx/certs:ro
      - vhost.d:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - proxy-tier

  letsencrypt-companion:
    image: nginxproxy/acme-companion
    restart: always
    volumes:
      - certs:/etc/nginx/certs
      - acme:/etc/acme.sh
      - vhost.d:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - proxy-tier
    depends_on:
      - proxy


volumes:
  mysql:
  nextcloud:
  images:
  certs:
  acme:
  vhost.d:
  html:

networks:
  proxy-tier:

And I get this feetback:

media@g1905:~/docker/myserver$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
media@g1905:~/docker/myserver$ docker-compose up
Creating myserver_redis_1 ... 
Creating myserver_proxy_1 ... 
Creating myserver_proxy_1 ... error
WARNING: Host is already in use by another container

Creating myserver_redis_1 ... done
_1 (8ead0ca671d745831f4e580701ae0c46eabeec7016bfa5470b77c666ab61da8a): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind:Creating myserver_db_1    ... done
Creating myserver_cron_1  ... done
Creating myserver_app_1   ... done

ERROR: for proxy  Cannot start service proxy: driver failed programming external connectivity on endpoint myserver_proxy_1 (8ead0ca671d745831f4e580701ae0c46eabeec7016bfa5470b77c666ab61da8a): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use
ERROR: Encountered errors while bringing up the project.
media@g1905:~/docker/myserver$ docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS      NAMES
2b429d4d7914   nextcloud:apache   "/entrypoint.sh apac…"   18 seconds ago   Up 17 seconds   80/tcp     myserver_app_1
bb2bfef1ec9b   nextcloud:apache   "/cron.sh"               18 seconds ago   Up 17 seconds   80/tcp     myserver_cron_1
6392f9f84a53   mariadb:10.5       "docker-entrypoint.s…"   19 seconds ago   Up 18 seconds   3306/tcp   myserver_db_1
bf78352bbb5e   myserver_proxy     "/app/docker-entrypo…"   19 seconds ago   Created                    myserver_proxy_1
ecc93ed66ab2   redis:alpine       "docker-entrypoint.s…"   19 seconds ago   Up 18 seconds   6379/tcp   myserver_redis_1
media@g1905:~/docker/myserver$ 

Again: Exactly this file worked in the past on this machine and is still working on another machine (VM).
All I did was to reboot the server. But this was not the first time…

It is like I suspected, the container exists exactly once. The 2nd line was just to report back the error.
Another process must be already binding port 80, which prevents your proxy container from binding port 80.
This could be a native process OR a container running on a different docker engine.
To make sure you don’t have a double installation, I would like to the see the output of following commands from a root terminal:

dpkg -l | grep docker
snap list docker

Ah I understand your point…

But see that the other containers are started as well two times. Here there is no conflict, so they are running in parallel.

media@g1905:~/docker/myserver$ dpkg -l | grep docker
ii  docker-compose                             1.29.2-1                                all          define and run multi-container Docker applications with YAML
ii  docker.io                                  20.10.21-0ubuntu1~22.04.2               amd64        Linux container runtime
ii  python3-docker                             5.0.3-1                                 all          Python 3 wrapper to access docker.io's control socket
ii  python3-dockerpty                          0.4.1-2                                 all          Pseudo-tty handler for docker Python client (Python 3.x)
media@g1905:~/docker/myserver$ snap list docker
Fehler: keine passenden Snaps installiert
media@g1905:~/docker/myserver$ 

Btw: This is what I get on the other machine. Especially each container is created only once.

media@myVMserver1:~/docker/myserver$ docker-compose up
Creating myserver_db_1                    ... done
Creating myserver_redis_1 ... done
Creating myserver_proxy_1 ... done
Creating myserver_letsencrypt-companion_1 ... done
Creating myserver_cron_1                  ... done
Creating myserver_app_1                   ... done
Attaching to myserver_redis_1, myserver_proxy_1, myserver_db_1, myserver_letsencrypt-companion_1, myserver_cron_1, myserver_app_1
cron_1                   | crond: crond (busybox 1.30.1) started, log level 0
cron_1                   | crond: user:www-data entry:(null)
app_1                    | Configuring Redis as session handler
db_1                     | 2023-03-19 16:39:30+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.5.19+maria~ubu2004 started.
cron_1                   | 100001000010000100001000010000100001000010000100001000010000
cron_1                   | 111111111111111111111111
cron_1                   | 11111111111111111111111111111111
cron_1                   | 111111111111
cron_1                   | 1111111
db_1                     | 2023-03-19 16:39:30+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
letsencrypt-companion_1  | Info: running acme-companion version v2.2.6
proxy_1                  | Info: running nginx-proxy version 1.2.1-14-gabcef6b
db_1                     | 2023-03-19 16:39:30+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.5.19+maria~ubu2004 started.
proxy_1                  | Setting up DH Parameters..
proxy_1                  | Warning: TRUST_DOWNSTREAM_PROXY is not set; defaulting to "true". For security, you should explicitly set TRUST_DOWNSTREAM_PROXY to "false" if there is not a trusted reverse proxy in front of this proxy.
proxy_1                  | Warning: The default value of TRUST_DOWNSTREAM_PROXY might change to "false" in a future version of nginx-proxy. If you require TRUST_DOWNSTREAM_PROXY to be enabled, explicitly set it to "true".
db_1                     | 2023-03-19 16:39:31+00:00 [Note] [Entrypoint]: MariaDB upgrade not required
proxy_1                  | forego      | starting dockergen.1 on port 5000
proxy_1                  | forego      | starti

... and a lot more...

Btw. you are not using docker-ce from docker’s repos.

You are using the docker.io of the Linux distribution, which is supported by whoever creates and maintains that package, which is not docker itself.

Back to your problem: please share the output (again from a root shell) of this command:
netstat -tpln | awk '/:80 / { split($7,x,"/"); system("ps -f --pid " x[1])}'

I hope it works on your system.

Here it is…

media@g1905:~/docker/myserver$ sudo netstat -tpln | awk '/:80 / { split($7,x,"/"); system("ps -f --pid " x[1])}'
UID          PID    PPID  C STIME TTY          TIME CMD
root         982       1  0 16:25 ?        00:00:00 /usr/sbin/apache2 -k start

I think the root cause is that ‘proxy’ is started a second time. The first time port 80 is free and can be used. The second time port 80 is already used by the first container, so it fails.

So the main question seems to be: Why are ALL containers created twice?

The output does not indicate that a container binds port 80, but instead a local apache httpd server does.

Your container uses the restart policy always: of course it’s going to restart the container until it succeeds, which it never does because the port is already occupied.

The output of docker ps -a clearly shows that no container exists twice.

Hi Metin, now I know what you mean, and you are right. The problem is created by apache.

No idea why it is started, because it is not used in this system. Perhaps I installed it in the past, but I don’t remember…

Anyhow. I stop apache and all containers are running as expected.

Thank’s a lot for your help and patience !

Problem solved. :ok_hand: