跳转至

Docker 设置容器环境变量


2016-05-20 by dongnan

问题描述

使用 mysql:5.7镜像创建容器提示: You need to specify one of MYSQL_ROOT_PASSWORD:

# 执行命令
docker run -ti mysql

error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

错误原因

mysql 容器不允许使用密码为空的 root账户运行 mysql服务器,所以需要指定容器的环境变量,使用 -e 参数即可。

解决方法

按照提示设置容器环境变量

# 执行命令
docker run --name mysql-server -t \
       -e MYSQL_DATABASE="zabbix" \
       -e MYSQL_USER="zabbix" \
       -e MYSQL_PASSWORD="password" \
       -e MYSQL_ROOT_PASSWORD="password" \
       -e TZ=Asia/Chongqing \
       -v /etc/localtime:/etc/localtime:ro \
       -v /tmp/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
       -v /data/mysql:/var/lib/mysql \
       -d mysql:5.7 \
         --character-set-server=utf8 \
         --collation-server=utf8_general_ci \
         --sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

验证

列出当前容器

# 执行命令
docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7c06cbf77a43       mysql               "docker-entrypoint.sh"   3 seconds ago       Up 2 seconds        3306/tcp           backstabbing_roentgen

查看容器IP地址

# 执行命令
docker exec 7c06cbf77a43 ip add show eth0

9: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever

登录服务器

# 执行命令
mysql -h 172.17.0.2 -uroot -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12 MySQL Community Server (GPL)
#...省略

参数

# 执行命令
docker run --help | grep '\-e'

 -e, --env=[]                    Set environment variables
  --entrypoint=                   Overwrite the default ENTRYPOINT of the image
  --env-file=[]                   Read in a file of environment variables
  --expose=[]                     Expose a port or a range of ports

欢迎关注微信公众号: 运维录

Back to top