Docker启动MySQL报错

今日启动MYSQL且映射到宿主机端口报错

启动mysql

sudo docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

报错信息如下:

docker: Error response from daemon: Conflict. The container name "/mysql" is already in use by containea8a9a4cb547f2849b30dff9". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

从以上信息看出貌似是之前启动了MySQL导致Docker容器出现重名的情况。
您必须删除(或重命名)该容器才能重用该名称。

于是我到Docker排查启动的容器

[root@cappuccino ~]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS                     PORTS  
f539b2e0797c   mysql:5.7   "docker-entrypoint.s…"   10 minutes ago   Exited (0) 4 minutes ago    

找到先前显示完整的CONTAINER ID rmove掉

[root@cappuccino ~]# docker rm f539b2e0797cbf42f9d5c95d4994dfd959f295911a8a9a4cb547f2849b30dff9
f539b2e0797cbf42f9d5c95d4994dfd959f295911a8a9a4cb547f2849b30dff9

重新执行端口映射命令成功解决!

[root@cappuccino ~]# sudo docker run -p 3306:3306 --name mysql \
> -v /mydata/mysql/log:/var/log/mysql \
> -v /mydata/mysql/data:/var/lib/mysql \
> -v /mydata/mysql/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
efb2c23596a0a3b30ece84de280db9befec5ff453f3814c3cc78a4d1ac53d4c5

Q.E.D.


奔浪少年永不停歇,勇敢追逐星河灿烂。