[root@iZwz97but8tjijoed local]# docker run -p 3306:3306 --name mysql \
> -v /usr/local/docker/mysql/logs:/var/log/mysql \
> -v /usr/local/docker/mysql/data:/var/lib/mysql \
> -v /usr/local/docker/mysql/conf/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
> -e MYSQL_ROOT_PASSWORD=123456 \
> -d mysql:5.7
参数说明:
-p 3306:3306:将容器的3306端口映射到主机的3306端口
-v /usr/local/docker/mysql/logs:/var/log/mysql:将日志文件夹挂载到主机
-v /usr/local/docker/mysql/data:/var/lib/mysql:将配置文件夹挂载到主机
-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码
执行运行命令后,报错了,它说有同名的实例在运行
docker: Error response from daemon: Conflict. The container name "/mysql" is already in use by container "f154e3d09563e7cc23307bd513ffc56dd3f78bc825908df95a4c59c043030bc1". You have to remove (or rename) that container to be able to reuse that name.
那就看看正在运行的实例有哪些:
[root@iZwz97bu0gr8jijoed local]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f154e3d09563 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
aaa672c11140 redis "docker-entrypoint.s…" 2 hours ago Exited (0) 2 hours ago redis
96c3bb2fb776 mysql "docker-entrypoint.s…" 4 days ago Exited (0) 2 hours ago mysql8.0
f209c67409c2 hello-world "/hello" 5 days ago Exited (0) 5 days ago inspiring_jennings
[root@iu0gr8vwt8th6su3Z local]# docker rm f154e3d09563
移除正在运行的那个实例:又报错了
[root@iZwz97bu0grjijoed local]# docker rm f154e3d09563
Error response from daemon: You cannot remove a running container f154e3d09563e7cc23307bd513ffc56dd3f78bc825908df95a4c59c043030bc1. Stop the container before attempting removal or force remove
原因:不能删除一个正在运行中的容器,要先停止下来才能删除
[root@iZwz97bu0gr8tjijoed local]# docker stop f154e3d09563e7cc23307bd513ffc56dd3f78bc825908df95a4c59c043030bc1
f154e3d09563e7cc23307bd513ffc56dd3f78bc825908df95a4c59c043030bc1
现在再移除:
[root@iZwz97fdag8vwt8tjijoed local]# docker rm f154e3d09563
f154e3d09563
成功啦,现在执行运行命令就没问题了。