Skip to content

Commit

Permalink
Docker changes (Radarr#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dabates authored and danielunderwood committed Dec 18, 2017
1 parent 9ab1fad commit 10680c1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
15 changes: 9 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
22 changes: 13 additions & 9 deletions LidarrAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Config>();

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; }
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 10 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
12 changes: 12 additions & 0 deletions docker-services/LidarrAPI/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 10680c1

Please sign in to comment.