简体   繁体   English

Jenkins docker上的未知MySQL服务器主机

[英]Unknown MySQL server host on Jenkins docker

In a Jenkinsfile stage I'd like to build the project, but it throws error when trying to get mysql working. 在Jenkinsfile阶段,我想构建项目,但是在尝试使mysql工作时会抛出错误。 The error thrown when running jenkins is: Unknown MySQL server host 'web_db' (this is related to the line below: 运行jenkins时抛出的错误是: Unknown MySQL server host 'web_db' (这与以下行相关:

sh '''mysql -u root -h web_db --password=root database -e \'SELECT 1\' > /dev/null'''

that is contained here in this Jenkinsfile (just the stage) 这个Jenkins文件中包含的内容(只是舞台)

stage('Configure web...') {
    agent {
        docker {
            label 'web'
            image ECR_URL_WEB
            args "-v${env.HOST_WORKSPACE_DIRECTORY}_${env.BRANCH_NAME}:/projectname"
            reuseNode true
        }
    }
    steps {
        sh '''
            if [ ! -f ./web/.htaccess ]; then
            cp /config/domainname.htaccess ./web/.htaccess
            fi
        '''
        sh '''
            if [ ! -f ./app/config/parameters.yml ]; then
            cp /config/parameters.yml ./app/config/
            fi
        '''
        sh '''
            if [ ! -d ./vendor ]; then
            php -d memory_limit=-1 /usr/local/bin/composer install --no-progress
            fi
        '''
        script {
            try {
                sh '''mysql -u root -h web_db --password=root database -e \'SELECT 1\' > /dev/null'''
            } catch (err) {
                sh '''
                    bin/console doctrine:database:create
                    bin/console doctrine:schema:create
                '''
        }
    }
}

docker-compose.yml 泊坞窗,compose.yml

version: '3.7'

volumes:
  jenkins-home:

services:
  jenkins:
    image: localhost:5000/jenkins-docker
    build: .
    restart: unless-stopped
    ports:
      - target: 8080
        published: 8080
        protocol: tcp
        mode: host
    volumes:
      - jenkins-home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
    container_name: jenkins-docker
    networks:
      - net

  db_host:
    container_name: web_db
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - net

networks:
  net:

The containers needs to be referred by their service names and not by actual container names . 容器需要通过其service names引用,而不是通过实际的container names引用。 In your case, the the service name for web_db container is db_host , so replace web_db with db_host . 在您的情况下, web_db容器的服务名称是db_host ,因此将web_db替换为db_host

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

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