Docker:尝试设置2个DB容器时使用MySQL [2002]

时间:2019-04-09 14:44:54

标签: mysql docker laravel-5 docker-compose digital-ocean

嗨,我正在尝试学习Docker。

作为起点,我遵循了DigitalOcean为Laravel here设计的教程。

我已经设法整理好所有容器,甚至完成了我的Laravel应用程序的构建。现在,我想设置第二个数据库进行测试。

我复制了db的{​​{1}}部分的设置,并更改了容器名称和端口。

所有容器的设置都很好,但是每当我尝试在两个数据库容器中的任何一个上运行docker-compose时,我都会收到2002错误,当我只运行一个DB容器时,我并没有遇到过:

docker exec

Dockerfile

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

docker-compose.yml

FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    mysql-client \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

version: '3' services: #PHP Service app: build: context: . dockerfile: Dockerfile image: digitalocean.com/php container_name: todo_app restart: unless-stopped tty: true environment: SERVICE_NAME: app SERVICE_TAGS: dev working_dir: /var/www volumes: - ./:/var/www - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini networks: - app-network #Nginx Service web: image: nginx:alpine container_name: todo_web restart: unless-stopped tty: true ports: - "80:80" - "443:443" volumes: - ./:/var/www - ./nginx/conf.d/:/etc/nginx/conf.d/ networks: - app-network #MySQL Service db: image: mysql:5.7.22 container_name: todo_db restart: unless-stopped tty: true ports: - "3306:3306" environment: MYSQL_DATABASE: todo MYSQL_ROOT_PASSWORD: ent3r1n SERVICE_TAGS: dev SERVICE_NAME: mysql volumes: - dbdata:/var/lib/mysql/ - ./mysql/my.cnf:/etc/mysql/my.cnf networks: - app-network db_test: image: mysql:5.7.22 container_name: todo_db_test restart: unless-stopped tty: true ports: - "3307:3306" environment: MYSQL_DATABASE: todo MYSQL_ROOT_PASSWORD: ent3r1n SERVICE_TAGS: dev SERVICE_NAME: mysql volumes: - dbdata:/var/lib/mysql/ - ./mysql/my.cnf:/etc/mysql/my.cnf networks: - app-network #Docker Networks networks: app-network: driver: bridge #Volumes volumes: dbdata: driver: local 的输出:

docker ps

1 个答案:

答案 0 :(得分:1)

由于您使用的图片,我曾经遇到过这样的问题:image: mysql:5.7.22 您应该使用:image: mysql/mysql-server:5.7。 希望它能对您有所帮助。