From 10680c1bfb21249a5db1d7f27a4ba38a088ee69c Mon Sep 17 00:00:00 2001 From: David Bates Date: Sun, 17 Dec 2017 18:08:39 -0600 Subject: [PATCH] Docker changes (#5) * Docker Changes Initial changes - Refactor how the docker container is started - working on getting the startup / setup scripts working * Working on getting the docker to build and start correctly * Changes to the Docker files and scripts. It now brings both containers up. Runs the DB Migrations and sets everything up * Fixes to make Docker Work - Startup.cs : Added debug values for some Variables to make sure they are passing correctly - Dockerfile: refactored the build and added some directory removals (I've found they cause the build to error) - README.md : added info on docker usage - docker-compose : removed old entries * Refactor Refactored the docker-compose, as my in-experince with docker containers skewed how I did some things.. this shoule be more streamlined * Refactor for using ENV Variables This refactors the WebAPI Code so that it will use ENV variables if they are present * fixup! removes mysql port * Adds more debug info --- Dockerfile | 15 ++++++++----- LidarrAPI/Startup.cs | 22 +++++++++++-------- README.md | 9 ++++++++ docker-compose.yml | 19 ++++++++-------- .../LidarrAPI/docker-entrypoint.sh | 12 ++++++++++ 5 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 docker-services/LidarrAPI/docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 00f32a7..6456c32 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,15 @@ FROM microsoft/dotnet:2.0-sdk WORKDIR /app -# copy csproj and restore as distinct layers -COPY LidarrAPI/*.csproj ./ -RUN dotnet restore - # copy everything else and build COPY LidarrAPI/* ./ -RUN dotnet publish -c Release -o out +COPY docker-services/LidarrAPI/docker-entrypoint.sh ./ + +# Windows screws with Line Endings, so do this to be 100% sure +RUN sed -i 's/\o015/\n/g' docker-entrypoint.sh + +# Run needed things on build +RUN dotnet restore && dotnet publish -c Release -o out -ENTRYPOINT ["dotnet", "out/LidarrAPI.dll"] +# Docker Entry +ENTRYPOINT ["./docker-entrypoint.sh"] \ No newline at end of file diff --git a/LidarrAPI/Startup.cs b/LidarrAPI/Startup.cs index dccc58e..a8a755a 100644 --- a/LidarrAPI/Startup.cs +++ b/LidarrAPI/Startup.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using NLog; using NLog.Extensions.Logging; using NLog.Web; using Octokit; @@ -20,21 +21,24 @@ namespace LidarrAPI { public class Startup { - public Startup(IConfiguration configuration, IHostingEnvironment env) + public Startup(IHostingEnvironment env) { - Config = configuration; + // Loading .NetCore style of config variables from json and environment + var builder = new ConfigurationBuilder() + .SetBasePath(env.ContentRootPath) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) + .AddEnvironmentVariables(); + + Config = builder.Build(); ConfigLidarr = Config.GetSection("Lidarr").Get(); env.ConfigureNLog("nlog.config"); - - // If env variables exist, read those in instead - ConfigLidarr.Database = Environment.GetEnvironmentVariable("Database") ?? ConfigLidarr.Database; - ConfigLidarr.DataDirectory = Environment.GetEnvironmentVariable("DataDirectory") ?? ConfigLidarr.DataDirectory; - ConfigLidarr.ApiKey = Environment.GetEnvironmentVariable("ApiKey") ?? ConfigLidarr.ApiKey; - ConfigLidarr.AppVeyorApiKey = Environment.GetEnvironmentVariable("AppVeyorApiKey") ?? ConfigLidarr.AppVeyorApiKey; - SetupDataDirectory(); SetupDatadog(); + + Logger logger = LogManager.GetCurrentClassLogger(); + logger.Debug($"Config Variables\n----------------\nDataDirectory : {ConfigLidarr.DataDirectory}\nDatabase : {ConfigLidarr.Database}\nAPIKey : {ConfigLidarr.ApiKey}\nAppVeyorApiKey : {ConfigLidarr.AppVeyorApiKey}\n\n"); } public IConfiguration Config { get; } diff --git a/README.md b/README.md index 3361654..e0ad99a 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,12 @@ This is the update API of [https://github.com/Lidarr/Lidarr](https://github.com/ ## Development If you want to work on **LidarrAPI.Update**, make sure you have [.NET Core 2.0 SDK](https://www.microsoft.com/net/download/core) installed and [Visual Studio 2017 RC](https://www.visualstudio.com/vs/visual-studio-2017-rc/). + +## Using Docker + +If you would like to use the docker setup we have for this project, follow these directions: +- Setup Environment Variables + - Make sure you set an environment variable PRIOR to running docker-compose up called `MYSQL_ROOT_PASSWORD` OR + - Setup and .env file or another way of passing variables as documented here: [Docker Compose](https://docs.docker.com/compose/environment-variables/#the-env-file) + +The most important thing is the `ApiKey`, the rest can be used **AS-IS**, but if the ApiKey is not set, fetching updates from AppVeyor and Github will not function correctly. \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 7fcdf5d..eb56363 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,24 +1,25 @@ version: '3' services: mysql: - image: mysql - + image: mysql/mysql-server environment: - - MYSQL_ROOT_PASSWORD= - + - MYSQL_ROOT_PASSWORD + - MYSQL_DATABASE=lidarrupdate + - MYSQL_USER=root + - MYSQL_PASSWORD=${MYSQL_ROOT_PASSWORD} + lidarrupdate: build: . - ports: - - "5001:5000" + - "5000:5000" links: - mysql environment: - - DataDirectory=/data - - Database="server=127.0.0.1;user id=root;password=${MYSQL_ROOT_PASSWORD};database=lidarrupdate;CharSet=utf8mb4" - - ApiKey= + - Lidarr:DataDirectory=/data + - Lidarr:Database=server=mysql;user id=root;password=${MYSQL_ROOT_PASSWORD};database=lidarrupdate;CharSet=utf8mb4 + - Lidarr:ApiKey= - ASPNETCORE_URLS=http://0.0.0.0:5000 volumes: diff --git a/docker-services/LidarrAPI/docker-entrypoint.sh b/docker-services/LidarrAPI/docker-entrypoint.sh new file mode 100644 index 0000000..ce4d36d --- /dev/null +++ b/docker-services/LidarrAPI/docker-entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +# Entrypoint for the docker container + +# First, we need to make sure the database is there +echo "[Entrypoint-LidarAPI] Running Database Migrations" +dotnet ef database update + +#Second, start the Service +echo "[Entrypoint-LidarAPI] Starting LidarrAPI Service" +dotnet out/LidarrAPI.dll \ No newline at end of file