我有一个docker-compose.yml
文件:
版本:“3”
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_USER: root
MYSQL_DATABASE: bl
ports:
- "3306:3306"
volumes:
- ./data:/var/lib/mysql
server:
env_file: "./server/.env"
build:
context: ./server
dockerfile: ./Dockerfile
image: "server"
ports:
- "5000:5000"
depends_on:
- db
Dockerfile:
FROM node:lts-alpine
WORKDIR /server
COPY . .
COPY package*.json ./
COPY .env ./
RUN yarn install
EXPOSE 5000
EXPOSE 3306
ENV NODE_ENV=docker
RUN yarn typeorm:migrate
RUN yarn typeorm:run-migrations
CMD ["yarn", "start"]
当尝试构建这两个时,我在这一行得到一个错误:RUN yarn typeorm:migrate
.
这是我得到的错误:
> [7/8] RUN yarn typeorm:migrate:
#10 0.857 yarn run v1.22.19
#10 0.943 $ yarn create:migrate-repo && yarn run typeorm -- -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration
#10 1.344 $ mkdir -p ./src/migrations
#10 1.757 warning From Yarn 1.0 onwards, scripts don't require "--" for options to be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.
#10 1.769 $ ts-node ./node_modules/typeorm/cli -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration
#10 7.524 Error during migration generation:
#10 7.527 Error: connect ECONNREFUSED 127.0.0.1:3306
#10 7.527 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
#10 7.527 errno: -111,
#10 7.527 code: 'ECONNREFUSED',
#10 7.527 syscall: 'connect',
#10 7.527 address: '127.0.0.1',
#10 7.527 port: 3306,
#10 7.527 fatal: true
#10 7.527 }
#10 7.565 error Command failed with exit code 1.
#10 7.565 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
#10 7.594 error Command failed with exit code 1.
#10 7.594 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------
发生错误的原因是服务器无法与db
映像建立连接。
这是我的DataSource
:
export const appDataSource = new DataSource({
type: "mysql",
host,
port,
username: rootUser,
password,
database,
entities: [Company, Order],
migrationsTableName: "migrations",
migrations: [`${__dirname}/../migrations/*.ts`],
});
下面是我特灵做的事情的列表。为了解决这个问题:
1.在两个映像之间创建网桥。它不起作用。
version: "3"
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_USER: root
MYSQL_DATABASE: bl
ports:
- "3306:3306"
volumes:
- ./data:/var/lib/mysql
networks:
- my-network
server:
env_file: "./server/.env"
build:
context: ./server
dockerfile: ./Dockerfile
image: "server"
ports:
- "5000:5000"
depends_on:
- db
networks:
- my-network
networks:
my-network:
driver: bridge
1.将host
的名称更改为我的db
映像的名称(有网桥和没有网桥)。
1.我甚至试图获得db
图像的直接IP,但没有成功。
docker inspect db | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.23.0.2",
2条答案
按热度按时间u5rb5r591#
问题是
db
地址在构建时不可用。从Dockerfile中删除这些行,并添加到
server
服务中的命令指令中:y1aodyip2#
好吧,我是这么解决的:
我按照Andromeda的建议在
docker-compose.yml
文件中添加了一行command
。我的
docker-compose
文件看起来像这样:我的
Dockerfile
看起来像这样:我从
package.json
运行的scripts
是:为了在我的服务中使用
db
镜像,我需要使用“db”作为host
: