创建新Docker容器时出现“The container name “/xxx“ is already in use by container xxxxxxxxxxx...”问题的解决办法

2021-12-06 20:02:27 浏览数 (1)

创建新Docker容器时出现“The container name “/xxx” is already in use by container xxxxxxxxxxx…”问题的解决办法

代码语言:javascript复制
详细错误提示:
 /usr/bin/docker-current: Error response from daemon: Conflict. The container name “/xxx” is already in use by container e3274a72e8d62a0f3022d3201405ce586147b3031c1232452d001ee41fb9c938. You have to remove (or rename) that container to be able to reuse that name…

下面举例说明。

列出本地镜像:

docker images

代码语言:javascript复制
REPOSITORY TAG IMAGE ID CREATED SIZE
 docker.io/tomcat 8.5.35 78b258e36eed 2 weeks ago 463 MB
 docker.io/tomcat latest 6759d91a032b 3 weeks ago 463 MB

创建新的容器:

docker run --name tomcat8080 -d -p 8080:8080 tomcat

代码语言:javascript复制
/usr/bin/docker-current: Error response from daemon: Conflict. The container name “/tomcat8080” is already in use by container e3274a72e8d62a0f3022d3201405ce586147b3031c1232452d001ee41fb9c938. You have to remove (or rename) that container to be able to reuse that name…
 See ‘/usr/bin/docker-current run --help’.
 上面创建新容器出现了错误,提示:容器名被占用,须移除或重命名后才能使用这个容器名。

【解决办法】

  1. 先查看所有的容器

docker ps -a

代码语言:javascript复制
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 e3274a72e8d6 tomcat “catalina.sh run” 2 weeks ago Exited (130) 19 minutes ago tomcat8080
 看到了这个名为 “tomcat8080” 的容器,并且这个容器是非运行(Exited)状态。

注:“docker ps” 是查看当前运行的容器,“docker ps -a” 是查看所有容器(包括停止的)。

  1. 移除这个“tomcat8080”容器

docker rm e3274a72e8d6

e3274a72e8d6 再看,容器已经移除:

docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

  1. 然后再创建新容器

docker run --name tomcat8080 -d -p 8080:8080 tomcat

af52e9ac72c0393b5468cccf235ad70a7bf6a6b4ed30122b345b3758875d8911 新容器创建成功,并且是运行状态:

docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES af52e9ac72c0 tomcat “catalina.sh run” 7 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp tomcat8080

【解决办法2】

如果想重新启动之前的,可以 docker ps -a 找到对应的容器然后 docker start containerId

0 人点赞