为什么错误无法为服务创建容器:冲突?

Why error Cannot create container for service : Conflict?

我克隆了原始仓库 docker bookstack

我复制了原文Docker-compose。甚至我都不想更新到 3,希望这些东西能用。

version: "2"
services:
  bookstack:
    image: lscr.io/linuxserver/bookstack
    container_name: bookstack
    environment:
      - PUID=1000
      - PGID=1000
      - APP_URL=
      - DB_HOST=bookstack_db
      - DB_USER=bookstack
      - DB_PASS=*******
      - DB_DATABASE=mystackapp
    volumes:
      - /path/to/data:/config
    ports:
      - 6875:80
    restart: unless-stopped
    depends_on:
      - bookstack_db
  bookstack_db:
    image: lscr.io/linuxserver/mariadb
    container_name: bookstack_db
    environment:
      - PUID=1000
      - PGID=1000
      - MYSQL_ROOT_PASSWORD=******
      - TZ=Europe/Budapest
      - MYSQL_DATABASE=mystackapp
      - MYSQL_USER=bookstack
      - MYSQL_PASSWORD=*******
    volumes:
      - /path/to/data:/config
    restart: unless-stopped

但我还是报错。

Creating network "docker-bookstack_default" with the default driver
WARNING: Found orphan containers (docker-bookstack_mysql_1) 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 bookstack_db ... done
Creating bookstack    ... error

ERROR: for bookstack  Cannot create container for service bookstack: Conflict. The container name "/bookstack" is already in use by container "ceede4ebfd4c16842e3a9486fd58b67713be6d527bc3ff2740a251172b2c5967". You have to remove (or rename) that container to be able to reuse that name.

我尝试添加 -p 标志并指定我的项目。再次出错!!

docker-compose -p firstproject up
Creating network "firstproject_default" with the default driver
Creating bookstack_db ... done
Creating bookstack    ... error

ERROR: for bookstack  Cannot create container for service bookstack: Conflict. The container name "/bookstack" is already in use by container "ceede4ebfd4c16842e3a9486fd58b67713be6d527bc3ff2740a251172b2c5967". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for bookstack  Cannot create container for service bookstack: Conflict. The container name "/bookstack" is already in use by container "ceede4ebfd4c16842e3a9486fd58b67713be6d527bc3ff2740a251172b2c5967". You have to remove (or rename) that container to be able to reuse that name.
ERROR: Encountered errors while bringing up the project.

为什么? 这个 docker-compose 文件是正确的吗? 在这种情况下我有循环依赖吗?

错误消息指出问题及其解决方法:

ERROR: for bookstack  Cannot create container for service bookstack:
Conflict. The container name "/bookstack" is already in use by container "ceede4ebfd4c16842e3a9486fd58b67713be6d527bc3ff2740a251172b2c5967". 
You have to remove (or rename) that container to be able to reuse that name.

问题在于您硬编码了容器名称,因此使用其他项目名称不会改变任何内容,容器名称保持不变。

services:
  bookstack:
    container_name: bookstack

您需要删除容器,即使用 docker compose downdocker rm bookstack

我建议不要对容器名称进行硬编码以避免此问题和其他问题。这样做没有什么好处。

只需将其从您的 yaml 文件中删除即可。

services:
  bookstack:
    image: lscr.io/linuxserver/bookstack
    # other stuff without container name

然后您还需要确保使用此名称进行通信的任何其他服务使用正确的名称。由于您的撰写服务名称与旧的静态容器名称相同,因此无需进一步更改即可使用。

我还会从 yaml 文件中删除任何其他 container_name 密钥。即 bookstack_db.