发布到docker

时间:2019-05-12 19:46:12

标签: docker .net-core

我已经研究了一段时间,似乎无法为我的dotnet核心应用程序获得正确的docker设置。与dotnet run一起运行时,效果很好。这是我运行docker-compose up后的docker文件,docker撰写文件,控制器文件和应用程序日志。

DockerFile:

# build application
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

COPY ./ ./
RUN dotnet restore
RUN dotnet publish -c Release

# run application
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env app/bin/Release/netcoreapp2.2/publish/ /app
COPY appsettings.json /app
EXPOSE 80
EXPOSE 5000

ENTRYPOINT ["dotnet", "imdb_id_retrieval.dll"]

docker-compose.yml:

version: '3.7'

volumes:
  imdb_id_service_volume:
    external:
      name: imdb_id_service_volume

networks:
  app_network:
    driver: bridge

services:
  postgres_imdb_id_db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - imdb_id_service_volume:/var/lib/postgresql/data
    networks:
      - app_network

  docker_imdb_id_service:
    image: docker_retrieve_data
    depends_on: 
      - "postgres_imdb_id_db"
    restart: always
    environment:
      - ASPNETCORE_ENVIRONMENT=development
    build:
      context: .
      dockerfile: Dockerfile
    networks:
      - app_network
    ports: 
      - "5000:80"

控制器:

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using imdb_data_retrieval.Logic;

namespace imdb_data_retrieval.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ImdbIdController : ControllerBase
    {
        private IRetrieveImdbIdService _retrieveImdbIdService;

        public ImdbIdController(IRetrieveImdbIdService retrieveImdbIdService){
            _retrieveImdbIdService = retrieveImdbIdService;
        }

        // GET api/values
        [HttpGet]
        public async Task<ActionResult<string>> Get(string title, string releaseYear)
        {
            var imdbId = await _retrieveImdbIdService.ScrapeImdbIdAsync(title, releaseYear);
            if (imdbId == Constants.InvalidYearResponse)
                return BadRequest(Constants.InvalidYearResponse);
            if (imdbId == null) 
                return NotFound(Constants.MovieWasNotFoundResponse);
            return Ok(imdbId);
        }
    }
}

因此,在这一点上,我运行“ docker-compose build”,然后运行“ docker-compose up”。这是输出:

[user@user-pc imdb_id_retrieval]$ docker-compose up
Starting imdb_id_retrieval_postgres_imdb_id_db_1 ... done
Recreating imdb_id_retrieval_docker_imdb_id_service_1 ... done
Attaching to imdb_id_retrieval_postgres_imdb_id_db_1, imdb_id_retrieval_docker_imdb_id_service_1
postgres_imdb_id_db_1     | 2019-05-12 12:37:58.035 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_imdb_id_db_1     | 2019-05-12 12:37:58.035 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_imdb_id_db_1     | 2019-05-12 12:37:58.112 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_imdb_id_db_1     | 2019-05-12 12:37:58.267 UTC [23] LOG:  database system was shut down at 2019-05-12 12:37:12 UTC
postgres_imdb_id_db_1     | 2019-05-12 12:37:58.555 UTC [1] LOG:  database system is ready to accept connections
docker_imdb_id_service_1  | derp
docker_imdb_id_service_1  | host=postgres_imdb_id_db; Username=postgres; Port=5432; Database=ImdbIds;
docker_imdb_id_service_1  | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
docker_imdb_id_service_1  |       No XML encryptor configured. Key {964dd0aa-58de-4714-b544-3c48e2b80bb9} may be persisted to storage in unencrypted form.
docker_imdb_id_service_1  | Hosting environment: development
docker_imdb_id_service_1  | Content root path: /app
docker_imdb_id_service_1  | Now listening on: http://[::]:80
docker_imdb_id_service_1  | Application started. Press Ctrl+C to shut down.

现在,我转到浏览器并转到“ http://localhost:5000/api/imdbid?title=iron%20man&releaseYear=2008”,然后收到404响应。我第一次尝试击中终点时看到

docker_imdb_id_service_1  | warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
docker_imdb_id_service_1  |       Failed to determine the https port for redirect.

然后没有连续命中的日志。我认为我不需要配置https。

关于我在做什么错的任何想法?

编辑:

我的Dockerfile现在看起来像这样。

# build application
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

COPY ./ ./
RUN dotnet restore
RUN dotnet publish -c Release

# run application
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env app/bin/Release/netcoreapp2.2/publish/ /app
COPY appsettings.json /app
EXPOSE 80
EXPOSE 5000

# changed this
ENTRYPOINT ["dotnet", "imdb_id_retrieval.dll", "--urls", "http://0.0.0.0:5000"] 

0 个答案:

没有答案