1

I'm new to docker and I'm trying to create a simple dot net core web api and mysql container composition.

I've done the following:

  1. Generated a starter project with the dot net sdk: dotnet new webapi -o example-app
  2. Created a Dockerfile in the project with the following content:
FROM microsoft/aspnetcore-build

WORKDIR /usr/src/app

COPY . .

EXPOSE 5000

ENTRYPOINT ["dotnet", "watch", "run"]
  1. Created a docker-compose.yml file with the following content:
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:

When i run docker-compose up I get the following output:

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
...

What am i doing wrong?

2
  • I suggest you follow the setup at https://hub.docker.com/r/microsoft/aspnetcore-build. Under Example usage there is a working example. Commented Aug 18, 2019 at 17:55
  • Maybe use ENTRYPOINT ["dotnet", "myapp.dll"]? Commented Aug 18, 2019 at 20:03

1 Answer 1

1

There are several problems with your configuration.

Main: you base your Dockerfile on deprecated image - microsoft/aspnetcore-build
I changed it to mcr.microsoft.com/dotnet/core/sdk:2.2 which contains dotnet watch tool inside.

Moreover to use Dotnet watch inside Docker you should declare environment variable:

ENV DOTNET_USE_POLLING_FILE_WATCHER 1

otherwise file-change events may not trigger (I can't find impartial source of this information but I've seen it in several articles and one book).

Furthermore there is problem with 127.0.0.1 address in dotnet watch inside container, so I have changed:
"applicationUrl": "https://localhost:5001;http://localhost:5000"
in launchSettings.json to:
"applicationUrl": "http://0.0.0.0:5000".
Explanation is written here: Docker dotnet watch run error: Unable to bind to https://localhost:5000 on the IPv6 loopback interface

Third mistake is docker-compose port bindings which you defined for app - '5000:80' - it should be: '80:5000' because left side is port for host, right side is port inside container.

Also you don't need to EXPOSE port from you app container, DB won't access it - app will access DB.

Now with simple docker-compose up --build you can run the system and see results from browser: http://localhost:80/api/values.

My suggestion is to delve a little into what is going on in Dockerfile and docker-compose file, to understand the flow and to be able to debug it.

I have written introduction to docker which explains example Dockerfile line by line and introduction to docker compose, based on .NET Core app composed with MySQL DB. Whole source code from the latter article is hosted on GitHub - if you want to skip article you can just see working example.

I hope you find these references helpful :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.