Docker Compose with .NET 8, PostgreSQL, and Redis (step by step)

Поділитися
Вставка
  • Опубліковано 27 тра 2024
  • Get the source code for this video for FREE → the-dotnet-weekly.ck.page/doc...
    ☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
    📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
    🚀 Support me on Patreon to access the source code: / milanjovanovic
    Docker is a platform designed to help developers build, share, and run container applications. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. You can easily containerize your .NET applications with a Dockerfile. Visual Studio can help you by scaffolding a Compose YAML file you can further customize. I'll show you how to start with Docker Compose in this video.
    Check out my courses: bit.ly/3PupkOJ
    5 Awesome C# Refactoring Tips
    www.milanjovanovic.tech/blog/...
    Join my weekly .NET newsletter:
    www.milanjovanovic.tech
    Read my Blog here:
    www.milanjovanovic.tech/blog
    Chapters
    0:00 Adding the Docker-Compose project
    1:15 Reviewing the Dockerfile
    2:14 Reviewing the Docker Compose YAML file
    3:08 Adding PostgreSQL using Docker compose
    5:15 Connecting to PostgreSQL running inside Docker
    6:33 Running with Docker Compose from Visual Studio
  • Наука та технологія

КОМЕНТАРІ • 145

  • @MilanJovanovicTech
    @MilanJovanovicTech  4 місяці тому +7

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/docker-compose
    P.S. How often do you use Docker and/or Docker Compose in your projects?

    • @markovertus3909
      @markovertus3909 4 місяці тому +1

      In every project, I love it. However, it's still an issue for real development. I would prefer to have a real time build and compile on every file save + debug if the app is running inside docker-compose. When I saw the title of your video, was low key hoping to see that, as it's still eludes me on .NET.

    • @Its_Kaleb_
      @Its_Kaleb_ 4 місяці тому

      Been messing around with docker a bit more recently, particularly for using dev containers in VS Code. Made for some interesting, config-as-code dev environments using compose to wire up dependencies too. Have you done much with dev containers?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому

      @@markovertus3909 I recently learned about 'Docker Compose Watch' - worth looking into

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому

      @@Its_Kaleb_ No, but I've played around with Testconatiners for my integration and E2E tests

    • @markovertus3909
      @markovertus3909 4 місяці тому

      @@MilanJovanovicTech yea, Ive played with it as well. I dont like that you need to mount your dlls for it to have any effect. But other then that, I agree with you, the support for docker and compose especially in Visual Studio is pretty insane.

  • @igorbarros9620
    @igorbarros9620 4 місяці тому +14

    I'm going to make myself a cup of coffee and get ready for some knowledge from the Wizard Milan himself. \m/

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +3

      Hope I delivered, then 😁

    • @igorbarros9620
      @igorbarros9620 4 місяці тому +2

      @@MilanJovanovicTech You sure did! Hey, man. Any chance I can a discount on your course? I live in Brazil, so for me to pay, it's like 5x times the current price (1 dollar is like 4.98 reais). I have been watching your videos since last december, and you have been helping a lot on my work! And thank you for that :))

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +4

      @@igorbarros9620 Check the course page, there should be a purple banner on top with a discont code (for Brazil)

  • @akeemaweda1716
    @akeemaweda1716 4 місяці тому +4

    Thanks Milan for this great and straight-to-the-point video.
    Stay awesome 🎉

  • @itssalmanvlogs_
    @itssalmanvlogs_ 4 місяці тому +2

    I always learn something new watching your videos, Thank you.

  • @DaminGamerMC
    @DaminGamerMC 4 місяці тому +3

    Great video, just what i needed for my job

  • @stochasticity86
    @stochasticity86 4 місяці тому +2

    Amazing and to the point. Love it!

  • @shuvo9131
    @shuvo9131 4 місяці тому +2

    Thanks for awesome explanation. This is to the point

  • @ertackosucu
    @ertackosucu 4 місяці тому +2

    Excellent as usual. Thank you.

  • @UberEverywhereSKRT
    @UberEverywhereSKRT 3 місяці тому +3

    Milan your videos are the best. I been following you for a minute now and ur amazing.
    Would be nice if u could do a video on how to setup a full .net development flow including dev, stage, prod env with docker , how to handle env variables during local vs prod (in cicd for example).
    I get confused on where the env variables (and secret) should be stored in local dev vs prod (where and when they are fetched from, etc).
    Ty in advance!

  • @feinirl
    @feinirl 4 місяці тому +2

    great video my guy :3 thanks for pca v2 btw you're insane

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      You're welcome. I'd appreciate any feedback on V2 😁

  • @juanlopeztello7315
    @juanlopeztello7315 4 місяці тому +4

    Excelent. It would be nice a video about setting up a development environment using devcontainers in vscode

  • @paolosartiano1454
    @paolosartiano1454 3 місяці тому +1

    very thanks! fantastic your video!

  • @giorgospanagopoulos3993
    @giorgospanagopoulos3993 4 місяці тому +2

    Excellent video. If you also showed how to connect to the data source via postgres & redis client UIs it would have been a perfect one!

  • @PatricSjoeoe
    @PatricSjoeoe 4 місяці тому

    Really nice and easy explained! =)

  • @mohamedalaa8212
    @mohamedalaa8212 4 місяці тому +2

    Another fantastic video, as always! Keep up the great work! 💥
    I'm also curious about the process by which Docker Compose build the productapi image and assigning it the dev tag.

  • @Fikusiklol
    @Fikusiklol 3 місяці тому

    Cool stuff, Milan
    Just a couple of things:
    1) you want to depend_on a database in compose file to be created first
    2) you can specify environments in compose for appSettings file using ConnectionString__Database | Redis, cause those might be different for production/dev

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      2) Which is why I'm using appsettings.Development.json, which shouldn't be used in production anyhow

  • @kj2w
    @kj2w 4 місяці тому +3

    This video is awesome. I shows a great use case for using Docker with an already working system. Correct me if I'm wrong but didn't you also do a video where integration tests can leverage a docker container?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      Yes: ua-cam.com/video/tj5ZCtvgXKY/v-deo.html
      And publishing a functional testing video soon

  • @selmangulmez6476
    @selmangulmez6476 4 місяці тому +3

    Hey Milan,
    Cache GetAsync doesnt have overload callback function
    which extension package gives that chance for us?
    Thanks for informations.

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +3

      It's this: github.com/mgravell/DistributedCacheDemo/blob/main/DistributedCacheExtensions.cs

    • @UnseenScofield
      @UnseenScofield 4 місяці тому

      Thanks @@MilanJovanovicTech

  • @onosnow6500
    @onosnow6500 Місяць тому

    Hi Milan, thanks for the amazing video explaining the docker-compose. Just a very quick question, if I am using Amazon RDS, how to specify the volume (04:35)

  • @TrungTran-le2ht
    @TrungTran-le2ht 3 місяці тому

    Thanks for the content. I have a question
    What is the benefit when using DbContext directly compare to Repository/UoW pattern ?

  • @Slyzor1
    @Slyzor1 2 місяці тому

    What a great video, thank you very much!

  • @andersjuul8310
    @andersjuul8310 3 місяці тому

    Love it ❤

  • @antondoit
    @antondoit 2 дні тому +1

    Great as always

  • @winstochurgle9133
    @winstochurgle9133 4 місяці тому +2

    I've been thinking why I can't establish a connection with Redis container. You need to add the appropriate network in the docker-compose.yml so that they act in the same context 🤓

  • @hamitkarahan2973
    @hamitkarahan2973 4 місяці тому +1

    Hey Milan thanks for great explanation. I'd like to ask you that what is the difference using docker compose over just debugging our project from visual studio development environment? And do developers usually use docker compose in their local machines for debugging the project, or they using it in environments(Development, Staging or Production) also?

  • @Liumbert
    @Liumbert 2 місяці тому +1

    oh man u r awesome, u won a new subscriber

  • @Corvin_
    @Corvin_ 4 місяці тому +2

    Really cool video, could you please clarify when or why you would use this? I can never really find scenarios on my own where docker makes my life easier. Does it make local development easier compared to setting up a localdb? Can you for example connect a db from the cloud for the production environment?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +2

      Local development would be the main use case. Everyone in your team has the same environment.

    • @Corvin_
      @Corvin_ 4 місяці тому

      @@MilanJovanovicTech Alright cool. I guess it is a tiny bit simpler than applying migrations for localdb on everyone's pc. But not by a ton.

  • @cleitoncarlospereira2006
    @cleitoncarlospereira2006 3 місяці тому +1

    Hello Milan, how are you?
    When I run the application, the EF migration control table is created, however, the tables in the database are not created.
    Do I need to look at anything else?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      Strange? The migrations are there? They're applied at startup - app.ApplyMigrations()

    • @petiriesz1916
      @petiriesz1916 Місяць тому

      Same for me, I'm using .net 8 maybe that caused the problem. I applied the migration via cli and it works.

  • @vibes992000
    @vibes992000 3 місяці тому +1

    Thanks a lot milan. Any comparison video for Docker Compose versus .Net Aspire ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      Not yet, but that's a great idea

    • @vibes992000
      @vibes992000 3 місяці тому

      @@MilanJovanovicTechThanks for considering. Expecting it to be a detailed video on pros and cons of both worlds and if possible multiple cases where each fits best. Thanks in advance.

  • @lourenceplay9017
    @lourenceplay9017 3 місяці тому

    How would you cache a ddd class that only has private constructor and Create() static method? There will be an error related to deserializing.
    I stuck on this error: System.NotSupportedException:
    Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported.

  • @TheOverBack
    @TheOverBack 3 місяці тому

    So. Docker Compose is useful when you want to run the whole solution with their dependencies? But if I have 2 MVC apps, a background application with cron jobs, and a lot of broker consumers, they are dependent on db, Redis, rabbit, and mongoDB. And if I want to run one MVC website with consumers or another MVC app with jobs, do I have to create a lot of docker compose files to cover all combinations?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      You can place all of them inside the same docker-compose setup

  • @montanomariano
    @montanomariano 4 місяці тому +1

    Great video! What if you were using database-first code-first approach? How would you make sure you get a copy of your existing database?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      This example is code-first. The migrations are applied when the app starts. But you can connect to the DB as any other database, and apply your changes.

  • @enciphered7650
    @enciphered7650 4 місяці тому +1

    Would you ever recommend caching result sets ?

  • @user-xm7sh3vw8o
    @user-xm7sh3vw8o 3 місяці тому +1

    Can you explain more? Many of you don't understand. From the comments, it can be concluded that Redis is just a client? How to publish it online for use?

  • @enis7x
    @enis7x 4 місяці тому +1

    When utilizing Docker, how do you create EF migrations? The environment variable I have placed in the Docker container for the database connection does not get read when I run EF commands. Except for doing EF migrations, the application functions as intended and the db-server connection is working fine.

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +2

      Yeah, you'll have to use the actual connection strings for running migrations. You can specify it manually when running EF commands. learn.microsoft.com/en-us/ef/core/cli/powershell#update-database

    • @enis7x
      @enis7x 4 місяці тому

      @@MilanJovanovicTech That works when you are updating the database but not when you are trying to create migrations. I'm still trying to find a workaround for the problem but thanks for the help, at least it works with the update command.

  • @usamabinahmed8012
    @usamabinahmed8012 4 місяці тому +4

    Hi Milan ,
    Where we can get this code example code available ? I think that will help new bees to get more things out of the concepts .

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      I share all the source code over on Patreon

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      But I'll see if I can release this pro-bono on GitHub, I agree it would be helpful

  • @DaminGamerMC
    @DaminGamerMC 2 місяці тому

    You can apply migrations with code like taht, but to add a migration you have to change the startup project which is kinda weird. Is there a better way?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 місяці тому

      "dotnet ef" command line tool instead of using the VS console

  • @nathangrosvenor7661
    @nathangrosvenor7661 2 місяці тому

    What would you say is better and faster to get to production out of .NET Aspire and Docker Compose

  • @bojned
    @bojned 3 місяці тому +1

    Nice and practical... I don't spend as much time coding these days (at least as not as much I would like), and videos like these keeps my knowledge fresh and up to date.

  • @LucianDunca
    @LucianDunca 4 місяці тому

    The video is awesome so thank you for that and everything you are doing.
    However there are some points which are missing, or might be difficult for an average Jr-Mid developer to figure out by himself without accessing the source code...
    1. Postgres setup and creating the database initial migration, with docker is hard to do this following your video; On short you need Postgres running in order to create your first migration
    2. RedisCache part. I think you are relying on an extension that allows for the cache asside pattern, the Default GetAsync method in the IDistributedCache interface does only have the key and cancellation token as parameters.
    This might be on purpose to get people to want the source code :)
    Nontheless your vieos are great and an invaluable source of learning!
    Stay awesome!

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому

      It's more out of convenience - I already have videos on running EF migrations at startup and quite a few videos on caching, Redis, etc.

  • @timur2887
    @timur2887 6 днів тому

    should you clear the cache on updates or just update the cache?

    • @MilanJovanovicTech
      @MilanJovanovicTech  5 днів тому +1

      Either one works fine. If you have the data ready, updating the cache makes sense.

  • @tienviet7442
    @tienviet7442 2 місяці тому

    what is theme you are using in vs?

  • @_miranHorvat
    @_miranHorvat 2 місяці тому

    In a real world scenario your app is deployed to multiple on-prem instances. Each instance needs its own settings and when you spin up a new updated version it has to use the same settings. So, where do you put your settings?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 місяці тому

      You want all of them to use the same settings? Why not something like Azure App Configuration?

    • @_miranHorvat
      @_miranHorvat 2 місяці тому

      @@MilanJovanovicTech No. Different settings and local resources on different locations. When you update i.e. install a new version, it has to use the same settings as the old one.

  • @MateStier-hd4nv
    @MateStier-hd4nv 2 місяці тому

    What kind of postgres gui do you use?:)

  • @BRH90593
    @BRH90593 3 місяці тому +1

    Docker container does not run when Visual Studio is closed. What am I doing wrong

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      If you start it from VS - I'd expect it to also shut down with VS

  • @sauravbhatta5303
    @sauravbhatta5303 4 місяці тому +1

    I was working on the pragmatic course and facing the same configuration null error in docker run. Could be same server name issue.
    You should also add a video to run debugger in docker.

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому

      Did you manage to solve it?

    • @sauravbhatta5303
      @sauravbhatta5303 4 місяці тому

      Checked it earlier. everything looks good and no problem locally but in Docker I am getting unhandled exception. ArgumentNullException: Value can’t be null. Parameter configuration.
      It’s at Infra.DI.AddInfrastructure(IConfiguration)
      Seems like not finding my appsetting.

    • @sauravbhatta5303
      @sauravbhatta5303 4 місяці тому

      That error is gone by adding appsetting file to the project. But still my port does not show up in docker. Port 5001 is not visible. I can open swagger with the 5001 port. Any idea why it’s not picking up from the docker file or compose file.

  • @jamesreynolds3167
    @jamesreynolds3167 4 місяці тому +1

    With the new container features in the build tools, you no longer need a docker file.
    You can configure it all within the csproj

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому

      That's a nice improvement. Maybe I'll make a YT short about it

  • @flacky3690
    @flacky3690 2 місяці тому

    I followed the steps but then I removed the docker-compose file and trying to add it back again but whatever I do I get this error "Unable to find any supported orchestrators for this type of project" any ideas?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 місяці тому

      No idea, haven't seen that issue before

    • @flacky3690
      @flacky3690 2 місяці тому

      @@MilanJovanovicTech Got it to work! Had to remove all the docker compose configuration and also go inside the project folder and remove all the docker related files that had been created. They didnt get removed automatically when I removed the docker compose project that's what caused the issue.

  • @baxram97
    @baxram97 3 місяці тому +1

    But what if I have migration files?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      You can automate that part - like I'm doing here (for local development) and applying it via DbContext.Database.Migrate()
      Or create the migration scripts, and run them yourself

  • @artemivanov2141
    @artemivanov2141 2 місяці тому

    Why do you need build and then publish in dockerfile? I think build is redundant here, publish already has build inside

  • @minimalstory
    @minimalstory 12 днів тому

    good!!!

  • @user-bz9ep9ep3x
    @user-bz9ep9ep3x 3 місяці тому

    Please publish a video by adding docker support for your CQRS pattern project(Gatherly)

  • @mad_t
    @mad_t 4 місяці тому +1

    It's a pity MS doesn't support podman (yet?), and you need extra actions to make it work with some limitations :(

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +1

      Yeah, that's annoying. I'll see if there's a nice solution for that, and maybe make another video.

  • @ivandrofly
    @ivandrofly Місяць тому

    4:59 - Connection string dns-naming

  • @lettuceturnipthebeets790
    @lettuceturnipthebeets790 4 місяці тому +1

    just when Nana posted her Zero-to-Hero vid on Docker Compose 😅
    looks suss jk jk it's fine to take inspiration from others)

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 місяці тому +2

      Pretty cool, thanks for introducing me to a new tech channel on YT 😅
      P.S. I can show you this was recorded December 12th last year. I typically have one-two months of videos scheduled ahead.

    • @harkiratsingh4947
      @harkiratsingh4947 4 місяці тому +2

      Milan is right , I have been following him over 1 year now and I have seen docker before as well. Milan is Azure AD B2C in your plan as well for sometime ahead ?

  • @capnbootyberry
    @capnbootyberry 2 місяці тому

    Hey, great video! Quick question regarding launch profiles. When I was just running my API using a Dockerfile, without docker-compose, I hade a `Docker` debug launch profile that I could set environment variables with when debugging. I know there's a Docker Compose launch profile now, but this doesn't seem to be exposed in the same manner as before. For instance, I can't go to the debug properties of my API project and add environment variables to this launch profile. Are you able to explain this for me?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 місяці тому

      You would add the environment variables in the docker-compose.yml file

  • @ChandanDey-cw4zq
    @ChandanDey-cw4zq Місяць тому

    Hi, I'm getting following error
    Npgsql.NpgsqlException: 'Failed to connect to 172.18.0.3:5432'
    public static class MigrationExtensions
    {
    public static void ApplyMigrations(this IApplicationBuilder app)
    {
    using IServiceScope scope = app.ApplicationServices.CreateScope();
    using ApplicationDbContext dbContext =
    scope.ServiceProvider.GetRequiredService();
    dbContext.Database.Migrate(); // error here
    }
    }

  • @capnbootyberry
    @capnbootyberry 2 місяці тому

    Can host machine environment variables be accessed by docker compose? Does something like `POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}` work when declaring a container's environment variables?