gpt4 book ai didi

mysql - 创建一个简单的dot net core和mysql docker容器组合

转载 作者:行者123 更新时间:2023-11-29 15:41:56 25 4
gpt4 key购买 nike

我是 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"]
  • 创建了一个包含以下内容的 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
    ...

    我做错了什么?

    最佳答案

    您的配置存在几个问题。

    Main:您的 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端口绑定(bind) - '5000:80' -它应该是:'80:5000',因为左侧是主机端口,右侧是容器内的端口。

    此外,您不需要从应用程序容器EXPOSE进行移植,数据库不会访问它 - 应用程序将访问数据库。

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

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

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

    我希望这些引用资料对您有所帮助:)

    关于mysql - 创建一个简单的dot net core和mysql docker容器组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545330/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com