无法运行MySQL容器,可能的容器无法读取`.env`文件中设置的变量

时间:2018-02-07 06:16:36

标签: mysql docker docker-compose

我有问题配置一个docker容器,它将为我的drupal dev环境提供MySQL服务

我的设置

我的Docker version 18.01.0-ce, build 03596f51b1

我有以下目录结构

.
├───.env
├───docker
│   └───drupal-mysql
│         └───Dockerfile
└───docker-compose.yml

我的.env文件:

MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=drupal-db
MYSQL_USER=drupal-user
MYSQL_PASSWORD=drupal-password
COMPOSE_CONVERT_WINDOWS_PATHS=1

我对该容器的docker-compose.yml

  drupal-mysql:
    container_name: drupal-mysql
    build:
      context: ./docker/drupal-mysql
    ports:
      - "3306:3306"
    environment:
     - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
     - MYSQL_DATABASE=${MYSQL_DATABASE}
     - MYSQL_USER=${MYSQL_USER}
     - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - ./mysql-data:/var/lib/mysql
      - /usr/bin
    user: "1000"
    networks:
      - drupal-net

我的docker/drupal-mysql/Dockerfile文件:

# MySQL Container
FROM mysql:5.7

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 3306
CMD ["mysqld"]

测试

正在运行docker-compose config

networks:
  drupal-net:
    driver: bridge
services:
  drupal-mysql:
    build:
      context: C:\Users\USER\Desktop\mydrupalsite\docker\drupal-mysql
    container_name: drupal-mysql
    environment:
      MYSQL_DATABASE: drupal-db
      MYSQL_PASSWORD: drupal-password
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: drupal-user
    networks:
      drupal-net: null
    ports:
    - 3306:3306/tcp
    user: '1000'
    volumes:
    - /c/Users/USER/Desktop/mydrupalsite/mysql-data:/var/lib/mysql:rw
    - /usr/bin
version: '2.0'

表示docker正在读取.env文件

中设置的变量

问题开始

但是配置容器失败了:

USER@beepoPC0119 MINGW64 ~/Desktop/mydrupalsite (master)
$ docker-compose up -d
Starting drupal-mysql ... done

USER@beepoPC0119 MINGW64 ~/Desktop/mydrupalsite (master)
$ docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                                             NAMES

USER@beepoPC0119 MINGW64 ~/Desktop/mydrupalsite (master)
$ docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                         PORTS                                             NAMES
cbee32c1ed5d        mydrupalsite_drupal-mysql   "docker-entrypoint.s…"   About an hour ago   Exited (1) 31 seconds ago                                                        drupal-mysql

您可以看到docker-compose up成功,但发出docker ps表示已退出,这可以通过发出docker ps -a

来确认

可能性

我在想,如果我运行

,容器会因为这个问题而自动退出
$ docker run mydrupalsite_drupal-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
  1. 现在,它令我困惑,为什么容器说的选项 实际上我们没有指定MYSQL_ROOT_PASSWORD
  2. 尝试解决方案

    尝试执行建议的解决方案https://github.com/docker/compose/issues/4189#issuecomment-274915631

    但是当我发出:

    $ docker-compose config
    networks:
      drupal-net:
        driver: bridge
    services:
      drupal-mysql:
        build:
          context: C:\Users\USER\Desktop\mydrupalsite\docker\drupal-mysql
        container_name: drupal-mysql
        environment:
          MYSQL_DATABASE: $${MYSQL_DATABASE}
          MYSQL_PASSWORD: $${MYSQL_PASSWORD}
          MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
          MYSQL_USER: $${MYSQL_USER}
        networks:
          drupal-net: null
        ports:
        - 3306:3306/tcp
        user: '1000'
        volumes:
        - /c/Users/USER/Desktop/mydrupalsite/mysql-data:/var/lib/mysql:rw
        - /usr/bin
    version: '2.0'
    

    你可以看到它没有从MYSQL_ROOT_PASSWORD文件解析.env,如果我发出:

    $ docker run mydrupalsite_drupal-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
    

    它仍然无法运行

    任何人都可以帮我吗?

0 个答案:

没有答案