Is there a way to change IP on MySQL using Dockerfile? I have a problem with my app.
I have this configuration:
let ormConfig: any;
if (process.env.NODE_ENV === 'test') {
// test environment database should be drop every time. so synchronize should be true for this config
ormConfig = {
type: 'mysql',
host: '0.0.0.0',
// host: 'localhost', // not needed if run on docker
username: 'root',
password: 'password',
database: 'test_db',
synchronize: true,
dropSchema: true,
entities: ['src/models/**/*.ts']
};
} else {
ormConfig = {
type: 'mysql',
host: 'db', // docker service database name
// host: 'localhost', // not needed if run on docker
// port: 3306, // not needed if run on docker
username: 'root',
password: 'password',
database: 'development_db',
synchronize: false,
logging: true,
entities: ['src/models/**/*.ts'],
migrations: ['db/migrations/**/*.ts'],
subscribers: ['src/subscribers/**/*.ts'],
cli: {
entitiesDir: 'src/models',
migrationsDir: 'db/migrations',
subscribersDir: 'src/subscribers'
}
};
}
export = ormConfig;
I'm using typeorm for my ORM. On else block I have a host name db
this works when connecting to my db if I'm using my application, but when I tried running a migration I have to change the host to 0.0.0.0
and found out that it's because of the docker MySQL container (server_db image):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aaaa4d178537 server_server "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:8000->8000/tcp server_server_1
e5021687b86b server_db "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, 33060/tcp server_db_1
I'm just tired of changing and changing it before and after migration.
Any idea just to use localhost? or custom IP like 1.2.3.456
something like that?
Also here's my docker-compose file:
version: "3.3"
services:
db:
build: ./db
restart: always
env_file:
- .env
ports:
- "3306:3306"
networks:
- app_network
server:
depends_on:
- "db" # this is important for sql connection. it has to wait for mysql to establish connection then run this bish
build: .
command: ["npm", "run", "dev"]
restart: always
ports:
- "8000:8000"
volumes:
- /server/node_modules/ # added this because of bcrypt elm error
- type: bind
source: .
target: /server # this name is from dockerfile workdir
networks:
- app_network
networks:
app_network:
volumes:
app_volume:
and my db Dockerfile
FROM mysql:8.0.18
EXPOSE 3306
COPY ./init_db.sql /docker-entrypoint-initdb.d/
Update
To answer @ckaserer's question:
Where are you running the migration from? e.g. are you trying to connect from your workstation/docker host to the mysql container?
I'm running it outside the container through npm with a command npm run migration:run
Any error logs you can share?
The error is saying cannot connect to db:3306
, that's why I have to change it to 0.0.0.0:3306
.
Can you clarify your migration workflow and where it fails? (including from which system it is executed. e.g. container x, workstation,..)
The migration happens outside the container using typeOrm cli, I create a migration, then cli will check my ormconfig.ts
and will check the else block for the type and host. (I'm not sure if this is what your asking)
Update
Here's the error if I run the migration:
Error: getaddrinfo ENOTFOUND db db:3306
the IP:Port is the problem. grrrr.
Link to Code repository: https://github.com/artoodeeto/ggpo-server