title: Bitbucket pipeline with docker and mysql
body: <p>I want to implement some CI/CD for a Django web using the bitbucket pipeline. The goal is: Test the Docker builds correctly and next run Django test. </p>
  
  <p>But I get this error:</p>
  
  <pre><code>django.db.utils.OperationalError: (2003, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
  </code></pre>
  
  <p>Here is the <code>bitbucket-pipelines.yml</code>:</p>
  
  <pre><code>options:
    docker: true
  
  definitions:
    services:
      mysql:
        image: mysql:5.7
        variables:
            MYSQL_DATABASE: 'foo'
            MYSQL_USER: 'default'
            MYSQL_PASSWORD: 'default'
            MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
  
    steps:
      - step: &amp;test-docker
          name: "docker builds"
          services:
            - docker
            - mysql
          caches:
            - docker
          script:
            - export IMAGE_NAME=foo:$BITBUCKET_COMMIT
            - export CONTAINER_NAME=test-foo
  
            - docker build -t $IMAGE_NAME .
            - docker run -p 8080:8080 --name $CONTAINER_NAME --rm -d $IMAGE_NAME
  
            - docker exec $CONTAINER_NAME python /app/manage.py test tests --noinput
            - docker stop $CONTAINER_NAME
  
  pipelines:
    default:
        - step: *test-docker
  </code></pre>
  
  <p>I already tried some solutions and workarounds like. Linking the ports from the services and the docker; using volumes; and test the Django outside docker. This one have more problems because it needs 2 DB (original and test) and a user with full access, and using the entrypoint didn't work.</p>