简体   繁体   English

连接数据库 - docker-compose up

[英]Connect with database - docker-compose up

I'm trying to make one Docker Compose file to up an WEB (reactJS), API (.NET Core 2.1) and an SQL Server instance.我正在尝试制作一个 Docker Compose 文件来启动一个 WEB (reactJS)、API (.NET Core 2.1) 和一个 SQL Server 实例。

When I init the database and run .NET with dotnet cli, it works (using a connection string Server=localhost ).当我初始化数据库并使用 dotnet cli 运行 .NET 时,它可以工作(使用连接字符串Server=localhost )。 However what I've been googling is that localhost does not work on containers.然而,我一直在谷歌搜索的是 localhost 不适用于容器。 And when using container I can't get my .NET Core to connect with my SQL Server.当使用容器时,我无法让我的 .NET Core 与我的 SQL Server 连接。

Can anyone shed some light what am I doing wrong?任何人都可以阐明我做错了什么?

I have this repo: https://github.com/lucasgozzi/sagetest我有这个仓库: https : //github.com/lucasgozzi/sagetest

And I'm currently using a branch names 'docker'.我目前正在使用分支名称“docker”。 Here is my Docker files and composer in case you don't want to clone the repo.这是我的 Docker 文件和 Composer,以防您不想克隆 repo。

Backend dockerfile:后端dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1
WORKDIR /src
COPY . .
RUN dotnet restore "./Api/Api.csproj"
RUN dotnet build "Api/Api.csproj" -c Release -o /app/build
RUN dotnet publish "Api/Api.csproj" -c Release -o /app/publish
EXPOSE 5000
WORKDIR /app/publish
ENTRYPOINT ["dotnet", "Api.dll"]

Frontend dockerfile:前端dockerfile:

# base image
FROM node:12.2.0-alpine

# set working directory
WORKDIR /app
EXPOSE 3000

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY . .
RUN npm install --silent
RUN npm install react-scripts -g --silent

# start app
CMD ["npm", "start"]

Docker compose: Docker 组成:

version: '3.1'

services:
  api:
    container_name: "teste-sage-api"
    image: 'teste-sage-api'
    build: 
      context: ./backend
      dockerfile: Dockerfile
    volumes:
      - ./backend:/var/www/backend
    ports:
      - "5000:5000"
    depends_on:
      - "database"
    networks:
      - sagetest-network
  web:
    container_name: "teste-sage-web"
    image: 'teste-sage-web'
    build: 
      context: ./frontend_react
      dockerfile: Dockerfile
    ports:
      - "3000:3000" 
    depends_on:
      - "api"
    networks:
      - sagetest-network
  database:
    container_name: "sql-server"
    image: "mcr.microsoft.com/mssql/server"
    environment:
      SA_PASSWORD: "Teste@123"
      ACCEPT_EULA: "Y"
    ports: 
      - "1433:1433"
    networks:
      - sagetest-network
networks:
  sagetest-network:
    driver: bridge 

You can access the database form your containers on the service name, in your case this will be database .您可以通过服务名称从容器访问数据库,在您的情况下,这将是database But you need to make sure that the database container is up and running before trying to connect to it.但是在尝试连接之前,您需要确保数据库容器已启动并正在运行。 depends_on is not enough for this case.在这种情况下, depends_on是不够的。 you may need to implement a waitfor in your dotnet container.您可能需要在 dotnet 容器中实现等待。 check this for more info https://docs.docker.com/compose/startup-order/查看更多信息https://docs.docker.com/compose/startup-order/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM