{"id":16678963,"url":"https://github.com/joncloud/dotnet-compose","last_synced_at":"2025-07-05T05:02:09.029Z","repository":{"id":78825191,"uuid":"67824657","full_name":"joncloud/dotnet-compose","owner":"joncloud","description":"Sample dotnet core application running in Docker using Compose.","archived":false,"fork":false,"pushed_at":"2020-06-13T21:06:06.000Z","size":59,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"publish","last_synced_at":"2025-03-06T04:36:31.561Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joncloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-09T18:59:47.000Z","updated_at":"2023-02-25T04:24:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"61432cfe-72b0-4a28-a795-b385b268e1b0","html_url":"https://github.com/joncloud/dotnet-compose","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncloud%2Fdotnet-compose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncloud%2Fdotnet-compose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncloud%2Fdotnet-compose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncloud%2Fdotnet-compose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joncloud","download_url":"https://codeload.github.com/joncloud/dotnet-compose/tar.gz/refs/heads/publish","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243360787,"owners_count":20278436,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T13:32:25.636Z","updated_at":"2025-03-13T07:27:42.128Z","avatar_url":"https://github.com/joncloud.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Quickstart: Docker Compose and .NET Core\nThis Quickstart guide will show you how to use Docker Compose to set up and run a .NET Core/PostgreSQL app. Before starting, you’ll need to have [Compose installed][].\n\n[Compose installed]: https://docs.docker.com/compose/install/\n\n## Define the project\nStart by setting up the three files you’ll need to build the app. First, since your app is going to run inside a Docker container containing all of its dependencies, you’ll need to define exactly what needs to be included in the container. This is done using a file called `Dockerfile`. To begin with, the Dockerfile consists of:\n\n```dockerfile\nFROM microsoft/dotnet\n\nRUN mkdir /myapp\nWORKDIR /myapp\n\nADD . /myapp\n```\nThat’ll put your application code inside an image that will build a container with .NET Core and all your dependencies inside it. For more information on how to write Dockerfiles, see the [Docker user guide][] and the [Dockerfile reference][].\n\n[Docker user guide]: https://docs.docker.com/engine/tutorials/dockerimages/#building-an-image-from-a-dockerfile\n[Dockerfile reference]: https://docs.docker.com/engine/reference/builder/\n\n`docker-compose.yml` is where the magic happens. This file describes the services that comprise your app (a database and a web app), how to get each one’s Docker image (the database just runs on a pre-made PostgreSQL image, and the web app is built from the current directory), and the configuration needed to link them together and expose the web app’s port.\n\n```yaml\nversion: '2'\nservices:\n    db:\n        image: postgres\n    web:\n        build: .\n        command: dotnet run\n        volumes:\n            - .:/myapp\n        ports:\n            - \"5000:5000\"\n        depends_on:\n            - db\n        environment:\n            ASPNETCORE_ENVIRONMENT: Development\n            ASPNETCORE_URLS: http://*:5000\n```\n## Build the project\nWith those three files in place, you can now generate the .NET Core skeleton app using `docker-compose run`:\n\n```bash\n$ docker-compose run web dotnet new -t Web\n```\n\nUpdate the Dockerfile to include `project.json`, because the `dotnet new` command does not have the ability to force generate.  Including this step will cache any packages that have been restored.\n\n```dockerfile\nFROM microsoft/dotnet\n\nRUN mkdir /myapp\nWORKDIR /myapp\n\nADD project.json /myapp/project.json\nADD project.lock.json /myapp/project.lock.json\nRUN dotnet restore\n\nADD . /myapp\n```\nFirst, Compose will build the image for the `web` service using the `Dockerfile`. Then it’ll run `dotnet new` inside a new container, using that image. Once it’s done, you should have generated a fresh app:\n\n```bash\n  $ ls -l\n  total 56\n  total 88\n  drwxr-xr-x  5 user  staff   170 Sep  9 13:07 Controllers\n  drwxr-xr-x  4 user  staff   136 Sep  9 13:07 Data\n  -rw-r--r--  1 user  staff    68 Sep  9 13:06 Dockerfile\n  drwxr-xr-x  5 user  staff   170 Sep  9 13:07 Models\n  -rwxr--r--  1 user  staff   550 Jun 21 20:31 Program.cs\n  -rwxr--r--  1 user  staff  2190 Jun 21 20:31 README.md\n  drwxr-xr-x  5 user  staff   170 Sep  9 13:07 Services\n  -rwxr--r--  1 user  staff  3105 Jun 21 20:31 Startup.cs\n  drwxr-xr-x  8 user  staff   272 Sep  9 13:07 Views\n  -rwxr--r--  1 user  staff   252 Jun 21 20:31 appsettings.json\n  -rwxr--r--  1 user  staff   204 Jun 21 20:31 bower.json\n  -rw-r--r--  1 user  staff   338 Sep  9 13:07 docker-compose.yml\n  -rwxr--r--  1 user  staff  1148 Jun 21 20:31 gulpfile.js\n  -rwxr--r--  1 user  staff   227 Jun 21 20:31 package.json\n  -rwxr--r--  1 user  staff  3211 Jun 21 20:31 project.json\n  -rwxr--r--  1 user  staff   549 Jun 21 20:31 web.config\n  drwxr-xr-x  6 user  staff   204 Sep  9 13:07 wwwroot\n```\nIf you are running Docker on Linux, the files `dotnet new` created are owned by root. This happens because the container runs as the root user. Change the ownership of the the new files.\n\n```bash\n  sudo chown -R $USER:$USER .\n```\n\nIf you are running Docker on Mac or Windows, you should already have ownership of all files, including those generated by `dotnet new`. List the files just to verify this.\n\nIn order to connect the application to PostgreSQL a couple more changes will be necessary.\n\nUpdate `project.json` to include references to PostgreSQL's .NET Core packages:\n\n```json\n\"dependencies\": {\n    ...\n    \"Npgsql.EntityFrameworkCore.PostgreSQL\": \"1.0.1\",\n    \"Npgsql.EntityFrameworkCore.PostgreSQL.Design\": \"1.0.1\"\n    ...\n}\n```\n\nUpdate `Startup.cs` to change the database connection to use PostgreSQL instead of SQLite:\n\n```csharp\n    public void ConfigureServices(IServiceCollection services)\n    {\n        // Add framework services.\n        services.AddDbContext\u003cApplicationDbContext\u003e(options =\u003e\n            options.UseNpgsql(Configuration.GetConnectionString(\"DefaultConnection\")));\n\n        ...\n    }\n```\n\nCreate `project.lock.json` as a simple json file:\n\n```json\n{}\n```\n\nNow that you’ve got a new `project.json`, you need to build the image again. (This, and changes to the Dockerfile itself, should be the only times you’ll need to rebuild.)\n\n```bash\n$ docker-compose build\n```\n\nTo ensure that the lock file gets updated with restored packages run `dotnet restore`:\n\n```bash\n$ docker-compose run web dotnet restore\n```\n\n## Connect the database\nThe app is now bootable, but you’re not quite there yet. By default, .NET Core expects a database to be running on `localhost` - so you need to point it at the `db` container instead. You also need to change the database and username to align with the defaults set by the `postgres` image.\n\nCreate a new configuration file `appsettings.Development.json` with the following:\n\n```json\n{\n  \"ConnectionStrings\": {\n    \"DefaultConnection\": \"User ID=postgres;Host=db;Database=postgres\"\n  }\n}\n```\n\nYou can now boot the app with:\n\n```bash\n$ docker-compose up\n```\n\nIf all’s well, you should see some PostgreSQL output, and then—after a few seconds—the familiar refrain:\n\n```bash\nweb_1  | Project myapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing                                                                                                                     \nweb_1  | Compiling myapp for .NETCoreApp,Version=v1.0                                                                            \nweb_1  |                                                                                                                         \nweb_1  | Compilation succeeded.                                                                                                  \nweb_1  |     0 Warning(s)                                                                                                        \nweb_1  |     0 Error(s)                                                                                                          \nweb_1  |                                                                                                                         \nweb_1  | Time elapsed 00:00:04.6780194                                                                                           \nweb_1  |                                                                                                                         \nweb_1  |                                                                                                                         \nweb_1  | info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]                                                \nweb_1  |       User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.                                                                                  \nweb_1  | Hosting environment: Development                                                                                        \nweb_1  | Content root path: /myapp                                                                                               \nweb_1  | Now listening on: http://*:5000                                                                                         \nweb_1  | Application started. Press Ctrl+C to shut down.\n```\n\nFinally, you need to create the database. In another terminal, run:\n\n```bash\n$ docker-compose run web dotnet ef database update\n```\n\nThat’s it. Your app should now be running on port 5000 on your Docker daemon. If you’re using [Docker Machine][], then `docker-machine ip MACHINE_VM` returns the Docker host IP address.\n\n[Docker Machine]: https://docs.docker.com/machine/overview/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoncloud%2Fdotnet-compose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoncloud%2Fdotnet-compose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoncloud%2Fdotnet-compose/lists"}