question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why can't I do knex migrate:latest using Docker-Composer?

See original GitHub issue

I have the following docker-compose.yml where I’m starting two services:

  • postgres
  • nodejs app

After the Postgres DB is up I try to run knex migrate:latest to create the DB structure. But for some reason the migration keep telling me that is unable to connect. This is strange because:

  • I’m able to connect to the DB using Postico and work with the DB no problem.
  • The nodejs app is able to connect because I get an error message telling me that the table that I’m trying to select from dose not exists.
  • I’m able to ping the DB container from the Web container

Question

What am I missing?

File: docker-compose.yml

version: '2'

services:

  postgres:
    image: "postgres"
    ports:
     - "5433:5432"
    networks:
     - curted
    environment:
     POSTGRES_DB: dbname
     POSTGRES_USER: admin
     POSTGRES_PASSWORD: admin
    volumes:
     - database:/var/lib/postgresql/data

  example.com:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - .:/code
    networks:
     - example
    working_dir: /code
    environment:
     DATABASE_URL: postgres://admin:admin@172.18.0.2:5433/dbname
     NODE_ENV: local
    command: bash -c "sleep 10 && npm run knex migrate:latest --file knexfile.js"
    depends_on:
     - postgres

networks:
  curted:
    driver: bridge

volumes:
  database:

The knexfile.js

module.exports = {
	local: {
		client: 'pg',
		connection: process.env.DATABASE_URL,
		ssl: false,
		migrations: {
			tableName: 'knex_migrations'
		}
	},
	staging: {
		client: 'pg',
		connection: process.env.DATABASE_URL,
		ssl: true,
		migrations: {
			tableName: 'knex_migrations'
		},
		pool: {
			min: 1,
			max: 2
		}
	},
	production: {
		client: 'pg',
		connection: process.env.DATABASE_URL,
		ssl: true,
		migrations: {
			tableName: 'knex_migrations'
		},
		pool: {
			min: 1,
			max: 2
		}
	}
};

Erro Message

curted.com_1  | > CuratedProducts@1.0.0 knex /home/app
curted.com_1  | > knex "migrate:latest"
curted.com_1  |
curted.com_1  | Using environment: local
curted.com_1  | Error: connect ECONNREFUSED 172.18.0.2:5433
curted.com_1  |     at Object.exports._errnoException (util.js:1022:11)
curted.com_1  |     at exports._exceptionWithHostPort (util.js:1045:20)
curted.com_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:28 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
vulgurcommented, May 31, 2018

Hey guys, I found the reason and the solution. The solution is generally speaking the same as @Bram-Zijp . Here is my knexfile.js:

module.exports = {

  development: {
    client: 'mysql2',
      debug:true,
    connection: {
        host:'docker-mysql',
        user: 'root',
        password: '1qaz',
        port: '',
        database: 'docker_db'
    },
      migrations: {
          tableName: 'knex_migrations'
      }
  },
};

The key is to set host with the name of the db service in docker-compose. Setting port to empty string or 3306 both will okay ( I don’t know why but it works).

The ECONNREFUSED error means the web app cannot connect the db, and I’ve tried many hosts and ports but all the tries failed, like localhost:3306 and 0.0.0.0:13306 and so on. The reason is the localhost:3306 actually refers the container itself, not the docker host.

Plus, make sure the migration is executed after db fully initializing.

References:

  1. https://medium.com/@niratattri/building-a-node-js-application-and-deploying-through-docker-meet-docker-aa8ae677ea12
  2. https://stackoverflow.com/questions/45399941/node-js-connect-to-mysql-docker-container-econnrefused
11reactions
davidgatticommented, Mar 3, 2017

OK. I’ll dig deeper, and will let you know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run knex migrations from Dockerfile or docker-compose
In the migration service you use knex migrate and in the app service you use npm run start .
Read more >
How to run knex migrations from Dockerfile or docker ...
Chaining the command or using and entrypoint is less optimal in case you want to horizontally scale your application. Then all replicas will...
Read more >
The Ultimate Node/Docker Setup: Step-by-Step (2022 Update)
To run the migrations, we'll want to do this from inside the Docker container where we have access to the DATABASE_URL environment variable....
Read more >
Database Migrations with Knex - Perk
One of the best features of Knex is it's robust migration support. To create a new migration simply use the knex cli: knex...
Read more >
Using Docker containers to beat flaky tests - LogRocket Blog
We'll use Postgres for persistence, but we'll use knex to manage migrations, seeds, and build query objects. Setup and installation. First, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found