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

    详细错误提示:
    /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..
    下面举例说明。
    列出本地镜像:

    1. # docker images
    2. REPOSITORY TAG IMAGE ID CREATED SIZE
    3. docker.io/tomcat 8.5.35 78b258e36eed 2 weeks ago 463 MB
    4. docker.io/tomcat latest 6759d91a032b 3 weeks ago 463 MB

    创建新的容器:

    1. # docker run --name tomcat8080 -d -p 8080:8080 tomcat
    2. /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..
    3. See '/usr/bin/docker-current run --help'.

    上面创建新容器出现了错误,提示:容器名被占用,须移除或重命名后才能使用这个容器名。
    【解决办法】
    1. 先查看所有的容器

    1. # docker ps -a
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. e3274a72e8d6 tomcat "catalina.sh run" 2 weeks ago Exited (130) 19 minutes ago tomcat8080

    看到了这个名为 “tomcat8080” 的容器,并且这个容器是非运行(Exited)状态。
    注:“docker ps” 是查看当前运行的容器,“docker ps -a” 是查看所有容器(包括停止的)。
    2. 移除这个“tomcat8080”容器

    1. # docker rm e3274a72e8d6
    2. e3274a72e8d6
    3. # 再看,容器已经移除:
    4. # docker ps -a
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    1. 然后再创建新容器
      1. # docker run --name tomcat8080 -d -p 8080:8080 tomcat
      2. af52e9ac72c0393b5468cccf235ad70a7bf6a6b4ed30122b345b3758875d8911
      新容器创建成功,并且是运行状态:
      1. # docker ps -a
      2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      3. af52e9ac72c0 tomcat "catalina.sh run" 7 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp tomcat8080