docker | database connection refused between two containers 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:
            # &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:


$ 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 &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"]


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&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
}


$ docker run -p 80:8080 --network=&lt;appname&gt;-mysql_backend &lt;imageid&gt;

$ 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?
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.