【问题标题】:Creating a simple dot net core and mysql docker container composition创建一个简单的 dot net core 和 mysql docker 容器组合
【发布时间】:2023-03-21 18:46:01
【问题描述】:

我是 docker 新手,我正在尝试创建一个简单的 dot net core web api 和 mysql 容器组合。

我做了以下事情:

  1. 使用 dot net sdk 生成了一个启动项目: dotnet new webapi -o example-app
  2. 在项目中创建了一个Dockerfile,内容如下:
FROM microsoft/aspnetcore-build

WORKDIR /usr/src/app

COPY . .

EXPOSE 5000

ENTRYPOINT ["dotnet", "watch", "run"]
  1. 创建了一个包含以下内容的 docker-compose.yml 文件:
version: "3"

services:
  dot-net-rest-api:
    container_name: docker-dot-net-rest-api
    restart: always
    build: .
    environment:
      - DBHOST=dot-net-mysql-db
      - ASPNETCORE_ENVIRONMENT=Development
    ports: 
      - '5000:80'
    depends_on:
      - dot-net-mysql-db

  dot-net-mysql-db:
    container_name: docker-dot-net-mysql-db
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: dot_net_rest_api_db
      MYSQL_USER: user
      MYSQL_PASSWORD: user
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3306:3306
    volumes:
      - mysqldbdata:/var/lib/mysql

volumes:
  mysqldbdata:

当我运行docker-compose up 时,我得到以下输出:

Creating network "example-app_default" with the default driver
Creating docker-dot-net-mysql-db ... done
Creating docker-dot-net-rest-api ... done
Attaching to docker-dot-net-mysql-db, docker-dot-net-rest-api
docker-dot-net-rest-api | /bin/sh: 1: [dotnet: not found
docker-dot-net-mysql-db | 2019-08-18T13:36:03.515008Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
docker-dot-net-mysql-db | 2019-08-18T13:36:03.515151Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 1
docker-dot-net-mysql-db | 2019-08-18T13:36:04.256200Z 0 [System] [MY-010229] [Server] Starting crash recovery...
docker-dot-net-mysql-db | 2019-08-18T13:36:04.267569Z 0 [System] [MY-010232] [Server] Crash recovery finished.
docker-dot-net-mysql-db | 2019-08-18T13:36:04.311086Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
docker-dot-net-mysql-db | 2019-08-18T13:36:04.315279Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
docker-dot-net-mysql-db | 2019-08-18T13:36:04.357081Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
docker-dot-net-mysql-db | 2019-08-18T13:36:04.481530Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
docker-dot-net-rest-api exited with code 127
docker-dot-net-rest-api exited with code 127
docker-dot-net-rest-api exited with code 127
docker-dot-net-rest-api exited with code 127
docker-dot-net-rest-api exited with code 127
docker-dot-net-rest-api exited with code 127
docker-dot-net-rest-api exited with code 127
...

我做错了什么?

【问题讨论】:

  • 我建议您按照https://hub.docker.com/r/microsoft/aspnetcore-build 的设置进行操作。在Example usage 下有一个工作示例。
  • 也许使用ENTRYPOINT ["dotnet", "myapp.dll"]

标签: mysql docker .net-core


【解决方案1】:

您的配置有几个问题。

主要:您的 Dockerfile 基于已弃用的映像 - microsoft/aspnetcore-build
我将其更改为 mcr.microsoft.com/dotnet/core/sdk:2.2,其中包含 dotnet watch 工具。

此外,要在 Docker 中使用 Dotnet watch,您应该声明环境变量:

ENV DOTNET_USE_POLLING_FILE_WATCHER 1

否则文件更改事件可能不会触发(我找不到此信息的公正来源,但我在几篇文章和一本书中看到过)。

此外容器内dotnet watch中的127.0.0.1地址有问题,所以我改了:
"applicationUrl": "https://localhost:5001;http://localhost:5000"
launchSettings.json 中发送至:
"applicationUrl": "http://0.0.0.0:5000"
解释写在这里:Docker dotnet watch run error: Unable to bind to https://localhost:5000 on the IPv6 loopback interface

第三个错误是您为应用定义的 docker-compose 端口绑定 - '5000:80' - 应该是:'80:5000' 因为左边是主机的端口,右边是容器内的端口。

此外,您不需要从您的应用容器EXPOSE端口,DB 不会访问它 - 应用将访问 DB。

现在使用简单的docker-compose up --build,您可以运行系统并通过浏览器查看结果:http://localhost:80/api/values

我的建议是深入研究 Dockerfiledocker-compose 文件中发生的事情,以了解流程和 能够调试它。

我已经写了introduction to docker,它逐行解释了示例 Dockerfileintroduction to docker compose,基于由 MySQL DB 组成的 .NET Core 应用程序。 整个 source code from the latter article 托管在 GitHub 上 - 如果您想跳过文章,您可以查看工作示例。

希望这些参考资料对您有所帮助:)

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多