Answers: <p>Your golang api should be included in the compose file and added to the same network.</p>
  
  <p>It also looks like when you run the golang api that you are adding it to a network other than <code>backend</code>, which is the network that is created in your compose file.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-25T05:26:54.960Z
Answers: 2020-01-25T05:26:54.960Z
Answers: Wesley Rolnick
Answers: <p>In addition to connecting app container with same <code>backend</code> network, you will also need to fix MySQL host parameter in connection string.</p>
  
  <p>Looking at <code>main</code> function you have MySQL host set to <code>localhost</code></p>
  
  <p><code>serverName := "localhost:3306"</code></p>
  
  <p>However, MySQL is not listening at <code>lcoalhost</code> of appc container you will need to change it to MySQL service name in compose file, i.e. <code>db</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-02-07T13:12:27.447Z
Answers: 2020-02-07T13:12:27.447Z
Answers: haroon
Body: <p>I have a mysql database running with the following <code>docker-compose.yml</code>:</p>
  
  <pre><code>version: '3.3'
      services:
          db:
              image: mysql:5.7
              restart: always
              environment:
                  MYSQL_DATABASE: 'demo'
                  # So you don't have to use root, but you can if you like
                  MYSQL_USER: 'user'
                  # You can use whatever password you like
                  MYSQL_PASSWORD: 'password'
                  # Password for root access
                  MYSQL_ROOT_PASSWORD: 'password'
              ports:
              # &lt;Port exposed&gt; : &lt; MySQL Port running inside container&gt;
                  - '3306:3306'
              expose:
                  # Opens port 3306 on the container
                  - '3306'
              # Where our data will be persisted
              volumes:
                  - my-db:/var/lib/mysql
              networks:
                  - backend
  
  networks:
      backend:
          driver: bridge
  # Names our volume
  volumes:
      my-db:
  </code></pre>
  
  <p><code>$ docker-compose build</code></p>
  
  <p><code>$ docker-compose up</code></p>
  
  <hr>
  
  <p>I have a basic golang rest api with the following <code>Dockerfile</code>:</p>
  
  <pre><code># Start from golang:1.12-alpine base image
  FROM golang:1.12-alpine
  
  # Adding git, bash and openssh to the image
  RUN apk update &amp;&amp; apk upgrade &amp;&amp; \
      apk add --no-cache bash git openssh
  
  
  # Set the Current Working Directory inside the container
  WORKDIR /app
  
  
  # Copy the source from the current directory to the Working Directory inside the container
  COPY . .
  RUN go get -d github.com/gorilla/mux
  RUN go get -d github.com/go-sql-driver/mysql
  RUN go get -d github.com/golang-migrate/migrate
  RUN go get -d github.com/golang-migrate/migrate/database/mysql
  RUN go get -d github.com/golang-migrate/migrate/source/file
  
  
  
  # Build the Go app
  RUN go build -o main .
  
  # Expose port 8080 to the outside world
  EXPOSE 8080
  
  # Run the executable
  CMD ["./main"]
  </code></pre>
  
  <p>and the following function is called:</p>
  
  <pre><code>func CreateDatabase() (*sql.DB, error) {
      serverName := "localhost:3306"
      user := "user"
      password := "password"
      dbName := "demo"
  
      connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&amp;collation=utf8mb4_unicode_ci&amp;parseTime=true&amp;multiStatements=true", user, password, serverName, dbName)
      db, err := sql.Open("mysql", connectionString)
      if err != nil {
          return nil, err
      }
  
      if err := migrateDatabase(db); err != nil {
          return db, err
      }
  
      return db, nil
  }
  </code></pre>
  
  <p><code>$ docker run -p 80:8080 --network=&lt;appname&gt;-mysql_backend &lt;imageid&gt;</code></p>
  
  <p><code>$ Database connection failed: %sdial tcp 127.0.0.1:3306: connect: connection refused</code></p>
  
  <hr>
  
  <p>I cannot get the api to establish a database connection with the database container?</p>
  
Tags: <docker><docker-compose><containers><dockerfile><mysql>
Title: docker | database connection refused between two containers
UserDisplayName: ma77c
ThreadText: # docker | database connection refused between two containers
  
  (Asked by: username_0 on 2020-01-25)
  
  I have a mysql database running with the following `docker-compose.yml`:
  
  ```
  version: '3.3'
      services:
          db:
              image: mysql:5.7
              restart: always
              environment:
                  MYSQL_DATABASE: 'demo'
                  # So you don't have to use root, but you can if you like
                  MYSQL_USER: 'user'
                  # You can use whatever password you like
                  MYSQL_PASSWORD: 'password'
                  # Password for root access
                  MYSQL_ROOT_PASSWORD: 'password'
              ports:
              # <Port exposed> : < MySQL Port running inside container>
                  - '3306:3306'
              expose:
                  # Opens port 3306 on the container
                  - '3306'
              # Where our data will be persisted
              volumes:
                  - my-db:/var/lib/mysql
              networks:
                  - backend
  
  networks:
      backend:
          driver: bridge
  # Names our volume
  volumes:
      my-db:
  
  ```
  
  `$ docker-compose build`
  
  `$ docker-compose up`
  
  ---
  
  I have a basic golang rest api with the following `Dockerfile`:
  
  ```
  # Start from golang:1.12-alpine base image
  FROM golang:1.12-alpine
  
  # Adding git, bash and openssh to the image
  RUN apk update && apk upgrade && \
      apk add --no-cache bash git openssh
  
  # Set the Current Working Directory inside the container
  WORKDIR /app
  
  # Copy the source from the current directory to the Working Directory inside the container
  COPY . .
  RUN go get -d github.com/gorilla/mux
  RUN go get -d github.com/go-sql-driver/mysql
  RUN go get -d github.com/golang-migrate/migrate
  RUN go get -d github.com/golang-migrate/migrate/database/mysql
  RUN go get -d github.com/golang-migrate/migrate/source/file
  
  # Build the Go app
  RUN go build -o main .
  
  # Expose port 8080 to the outside world
  EXPOSE 8080
  
  # Run the executable
  CMD ["./main"]
  
  ```
  
  and the following function is called:
  
  ```
  func CreateDatabase() (*sql.DB, error) {
      serverName := "localhost:3306"
      user := "user"
      password := "password"
      dbName := "demo"
  
      connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=true&multiStatements=true", user, password, serverName, dbName)
      db, err := sql.Open("mysql", connectionString)
      if err != nil {
          return nil, err
      }
  
      if err := migrateDatabase(db); err != nil {
          return db, err
      }
  
      return db, nil
  }
  
  ```
  
  `$ docker run -p 80:8080 --network=<appname>-mysql_backend <imageid>`
  
  `$ Database connection failed: %sdial tcp 127.0.0.1:3306: connect: connection refused`
  
  ---
  
  I cannot get the api to establish a database connection with the database container?
  
  **Tags:** docker, docker-compose, containers, dockerfile, mysql
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-01-25. Score: 2
  
  Your golang api should be included in the compose file and added to the same network.
  
  It also looks like when you run the golang api that you are adding it to a network other than `backend`, which is the network that is created in your compose file.
  
  
  ---
  
  ### Answer by username_2 on 2020-02-07. Score: 1
  
  In addition to connecting app container with same `backend` network, you will also need to fix MySQL host parameter in connection string.
  
  Looking at `main` function you have MySQL host set to `localhost`
  
  `serverName := "localhost:3306"`
  
  However, MySQL is not listening at `lcoalhost` of appc container you will need to change it to MySQL service name in compose file, i.e. `db`
  
  
  ---
  
  