简体   繁体   English

连接到docker中的mysql失败

[英]Connecting to mysql in docker fails

I'm setting up a Dockerfile where I can run my automated tests, and I'm having troubles with connecting to mysql database. 我正在设置一个Dockerfile,我可以运行自动化测试,而且我在连接mysql数据库时遇到了麻烦。

The Dockerfile depends on a prevoously built image and looks like this: Dockerfile依赖于一个预先构建的图像,如下所示:

# Stage 0, assign argument as multistage image alias
ARG PHP_IMAGE
FROM ${PHP_IMAGE} as image

# Stage 1, start tests
FROM php:7.2-fpm

RUN curl -sS https://getcomposer.org/installer | php \
  && chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    apt-get install -yq nodejs build-essential \
        git unzip \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        subversion \
    && curl -sL https://deb.nodesource.com/setup_8.x | bash - \
    && pecl install mcrypt-1.0.1 \
    && docker-php-ext-enable mcrypt \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install -j$(nproc) mysqli

RUN apt-get install -y mysql-server
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN npm install -g npm

COPY --from=image /var/www/html/ /var/www/html/

WORKDIR /var/www/html/
COPY scripts/develop.sh develop.sh
COPY scripts/docker-test.sh docker-test.sh

RUN ["/bin/bash", "-c", "bash develop.sh && bash docker-test.sh"]

I've added RUN mysqladmin -u root -p status to try to debug why connecting to mysql failed and I got 我已经添加了RUN mysqladmin -u root -p status以尝试调试为什么连接到mysql失败而且我得到了

Enter password: mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")' Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists! 输入密码:mysqladmin:在'localhost'连接到服务器失败错误:'无法通过套接字连接到本地MySQL服务器'/var/run/mysqld/mysqld.sock'(2“没有这样的文件或目录”)'检查mysqld正在运行并且套接字:'/ var / run / mysqld / mysqld.sock'存在!

To run this I am running 为了运行这个,我正在运行

docker build -t $TEST_DOCKER_NAME --build-arg PHP_IMAGE=$DOCKER_IMAGE_NAME_PHP -f Dockerfile.test .

The TEST_DOCKER_NAME and DOCKER_IMAGE_NAME_PHP are stored in an env file and read from there. TEST_DOCKER_NAMEDOCKER_IMAGE_NAME_PHP存储在env文件中并从那里读取。 The PHP image was built successfuly and I'm using it to copy the files from there to here so that I can run PHPUnit. PHP映像是成功构建的,我正在使用它将文件从那里复制到这里,以便我可以运行PHPUnit。

When I remove that RUN line my build fails when I'm trying to run a script that creates the database 当我尝试运行创建数据库的脚本时,当我删除该RUN行时,我的构建失败

mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to MySQL server on 'localhost' (99 "Cannot assign requested address")' Check that mysqld is running on localhost and that the port is 3306. You can check this by doing 'telnet localhost 3306' mysqladmin:在'localhost'连接到服务器失败错误:'无法连接到'localhost'上的MySQL服务器(99“无法分配请求的地址”)'检查mysqld是否在localhost上运行,并且端口是3306.你可以通过'telnet localhost 3306'来检查这个

What do I need to do in my Dockerfile to make it work? 我需要在Dockerfile中做些什么来使其工作?

Answer to your specific problem 回答您的具体问题

This is a common mistake people make when using docker. 这是人们在使用docker时常犯的错误。 When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting. 在docker中使用RUN指令时,您正在运行命令直至完成,捕获文件系统更改然后退出。

So when you have the lines 所以,当你有线

RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status

The first one is starting mysql. 第一个是启动mysql。 But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. 但随后捕获更改,退出容器,然后启动一个新的运行mysqladmin命令。 Therefore the mysql process is no longer running. 因此mysql进程不再运行。

To avoid this you could combine them into a single line like 为了避免这种情况,您可以将它们组合成一条线

RUN /etc/init.d/mysql start && mysqladmin -u root -p status

However you will need to do this every time you want to use mysql. 但是,每次要使用mysql时都需要这样做。 Such as in your develop.sh . 比如在你的develop.sh

Wider answer 更广泛的答案

It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container. 建议不要在容器中运行多个进程,也不建议在容器中使用init.d或其他系统启动框架。

You seem to be treating your container like a virtual machine and are having issues because containers are not VMs. 您似乎将容器视为虚拟机,并且由于容器不是VM而存在问题。

I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers. 我建议你探索在一个单独的容器中运行mysql,然后使用像docker-compose这样的工具来启动和停止你的容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM