1

here is my db.js file

let connection = mysql.createConnection({
host: process.env.DATABASE_HOST || '127.0.0.1',
user: 'root',
database: 'bc2k19',
password: 'joeydash',
port: 33060
});

here is my docker-compose.yml file

version: '3.2'
services:
  app:
    build: ./app
    ports:
    - "3000:3000"
    depends_on:
    - db
    environment:
    - DATABASE_HOST=db
  db:
    build: ./db
    ports:
    - "3306:3306"

here is my dockerfile for mysql

FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD joeydash
ENV MYSQL_DATABASE bc2k19
ENV MYSQL_USER joeydash
ENV MYSQL_PASSWORD joeydash

ADD setup.sql /docker-entrypoint-initdb.d

here is my dockerfile for my app

# Use Node v4 as the base

 image.
FROM node:latest

# Add everything in the current directory to our image, in the 'app' folder.
ADD . /app

# Install dependencies
RUN cd /app; \
    npm install --production

# Expose our server port.
EXPOSE 3000

# Run our app.
CMD ["node", "/app/bin/www"]

I don't know why everytime I try to do docker-compose up and connect it shows

     Error: connect ECONNREFUSED 127.0.0.1:3306
app_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)

It shoes like the connection is not set but it is set

David Maze
  • 130,717
  • 29
  • 175
  • 215
joeydash
  • 81
  • 1
  • 12
  • you should share your compose file here too – erik258 Nov 09 '18 at 18:37
  • (But that's not _quite_ right...your code is clearly incorrectly trying to connect to 127.0.0.1, but you've got the `DATABASE_HOST=db` environment variable setting that should be passed through. The port in your code is wrong, but also doesn't match the port in the error message?) – David Maze Nov 09 '18 at 18:47

2 Answers2

2

There are 3 things happening here:

  1. Your port number in the nodejs app (db.js) has a typo.
    • change 33060 to 3306
  2. Your user is root yet the user in the environment is joeydash
    • Change root to joeydash
  3. Mysql might block your connection since it's set to listen to localhost per default. Within the container world localhost most of the times if not always points to within the container.

To fix point two you you should check your mysql config for the bind section. (look for bind-address) Make sure it allows connections from the ip your other container is running on.

  • If MySQL binds to 127.0.0.1, then only software on the same computer will be able to connect (because 127.0.0.1 is always the local computer).
  • If MySQL binds to 192.168.0.2 (and the server computer's IP address is 192.168.0.2 and it's on a /24 subnet), then any computers on the same subnet (anything that starts with 192.168.0) will be able to connect.
  • If MySQL binds to 0.0.0.0, then any computer which is able to reach the server computer over the network will be able to connect.

Quoting from here https://stackoverflow.com/a/3552946/2724940

If we go through every scenario:

  1. The first one fails since all containers have their own localhost
  2. The 2nd option could work if the correct ip is set to your mysql config.
  3. The 3rd option works as mysql is now configured to allow all remote connections.

my.cnf (located in /etc/mysql/my.cnf

[mysqld]
bind-address = 0.0.0.0
Baklap4
  • 3,914
  • 2
  • 29
  • 56
1

You might need to create a network or link containers depending on your version of docker compose.

At the end of docker-compose.yml you could add:

networks:
  backend:
    driver: 'bridge'

And for each container add:

networks:
- backend
Baklap4
  • 3,914
  • 2
  • 29
  • 56
Nick Surmanidze
  • 1,671
  • 1
  • 11
  • 20
  • 2
    Any Docker Compose new enough to understand `version: '3.2'` does this automatically behind the scenes for you. – David Maze Nov 09 '18 at 21:05