Why can't I do knex migrate:latest using Docker-Composer?
See original GitHub issueI 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:
- Created 7 years ago
- Comments:28 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey guys, I found the reason and the solution. The solution is generally speaking the same as @Bram-Zijp . Here is my knexfile.js:
The key is to set
hostwith the name of the db service in docker-compose. Settingportto 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:3306and0.0.0.0:13306and so on. The reason is thelocalhost:3306actually refers the container itself, not the docker host.Plus, make sure the migration is executed after db fully initializing.
References:
OK. I’ll dig deeper, and will let you know.