BrandGhost

Configure Docker with Redis and C# in 10 Minutes!

I've put out a bunch of videos now on data access patterns in C#, and one of the exciting things we have to work with in ASP NET Core is of course... caching! The most popular key-value store that developers work with is Redis -- an excellent choice for your distributed cache. I figured we'd get up and running with Docker and Redis in just a few minutes!
View Transcript
Hi, my name is Nick Cosantino and I'm a principal software engineering manager at Microsoft. In this video, we're going to be looking at getting Docker Desktop up and running with a local Reddus instance and then connecting to that from C. Now, I wanted to put this video together because I have another playlist which I'll link up here if you haven't seen it already and that walks us through how we can use different database access patterns like a repository pattern. we can work with entity framework core Dapper and then we started to introduce caching and Reddus is something really popular when caching comes up. So I figured let's get a little tutorial together so other people can get set up with Reddus locally so that you don't have to worry about having that set up in the cloud just to be able to figure out how to run things on your own machine. If that sounds interesting, just a reminder to subscribe to the channel and check out that pin comment for my courses on domain. Now, let's jump over to Chrome and we'll see how we can get set up with our first step, which is Docker Desktop. In my browser, I just went ahead and searched for Docker Desktop and it brought me here. So, pick your favorite search engine, right? You have your own. That's fine. Docker Desktop. If I scroll down or on the left, I guess we can see Docker Desktop in terms of products. And then if we scroll down, we can see that there's an install tab here, right? So, pick your operating system. I'm on Windows. Boom. We click that and then I would go ahead and probably pick just the top one here. Uh I don't necessarily want to install it from the Microsoft store and I don't want ARM. So go ahead install that. I'm not going to show you the installation process. It's pretty lightweight. Once you have it up and running though, what you'll have is this little icon. It's pretty cool, right? A little whale with a shipping container kind of thing going on. This is Docker Desktop. And when you first get this up and running, there's really nothing that's going on yet. So, we haven't made the magic happen. If you're looking around for where Reddus is, it's not here yet. But there's lots of images that we can use with Docker Desktop. And you can use this on the command line, too. So, if you're more comfortable with the command line, there is a terminal button in the bottom right hand corner. You can go ahead and pop that up, right? You have to enable it down here. I'm just going to show you in the UI. I am a UI kind of person, so I like using that. But right at the top, you can see that I clicked on search, right? It gives us this little drop down. This is literally all we have to do, which is really cool. Reddus, and I got nervous there cuz nothing showed up, but Reddus, it just took a second for the results to come in. I can go ahead and install this. I can run it. Uh, so pulling will just get the image pulled down and then we can run it as well. I'm going to go ahead and press run. I think the last time I did this, it only pulled it and it didn't run it. So, we'll see. But you can see we have this progress bar. Boom. There we go. When you're running an image, you get a container from that. So, if I check containers, see, I did press the run button. It's not actually running. So, let's go back to images. This is where you go ahead and press run to have it started. So, I'm going to go and press run and then run. Now you can see it jumped us to the container tab. I didn't click that myself. It jumped us right there. And now we actually have Reddus running. At this point I skipped over a step briefly and I wanted to do that because we're going to see an error when we go to use this from C. And I wanted to do this because I think this is a really common issue. I myself make this every single time that I go between using something like Docker with an image like Reddus. There's always something that I'm missing when I go to run this and that's why I left it out. We'll come back, but I want to show you this so that you're familiar with it when we're working inside of Visual Studio. And that way you go, "Ah, yes. Okay, that's what Nick was talking about." If I leave this out and people follow this tutorial, I can guarantee a large majority of people will just get stuck on this step and spend tons of time debugging when they don't have to. So, I went ahead and I made a project inside of Visual Studio. Not going to walk you through doing that, right? Just file, new menu, pick a project. You can do this in whatever you want. I just have a console application because all that we're going to be doing in this video is showing that we can connect to Reddus, store some information, and read it back. Right? So, Reddis is just a key value store for the purpose of this conversation. If you are interested in seeing more that you can do with Reddus, like I said, I have that playlist that I linked at the beginning and that's going to walk you through different ways that you can use Reddus for caching. The Reddus package that we want is going to be Stack Exchange Reddus. I already have it in my search bar. Boom. So, we'll go ahead and pick that. This one here, I already have it installed. You can see at the time of recording I have 2.8.31, 8.31, which is pretty wild because I feel like I just did this the other day and it's already got an update. Let's just go ahead and update it. Why not? So, we have the latest and greatest installed and that way we can go ahead and jump over to our program CS file. Okay, there is not a lot of code here, which is great news for us. I'll zoom in a little bit more. This is really, really simple for us to get up and running with, which is great. What we want to do is get this connection multipplexer. And what we're going to do is because we're running this from Docker Desktop on our local machine, we're just going to connect to local host. Okay. From there, that means we have this Reddus instance. It's just the connection multiplexer. So you could call it multiplexer or whatever you want. That is something you can identify it as. Um I have a using statement because the multiplexer is disposable. the database itself is not. So if I put a using out the front here, notice how it does not allow it. So at the very bottom it says type use and using statement must implement I disposable. So I database is not disposable. The multiplexer is. So all that we're going to do is get the database from the multiplexer. Very complicated. And then what we have is just to be able to set and get. It is so simple to use from an API perspective. However, there's lots that you can do with Reddus. We're just not going to cover it all in this video. And this is just a brief interruption to remind you that I do have courses available on dome train focused on C. So whether you're interested in getting started in C, looking for a little bit more of an intermediate course focused on object-oriented programming and some async programming or you're just looking to update your refactoring skills and see some examples that we can walk through together, you can go ahead and check them out by visiting the links in the description and the comment below. Thanks and back to the video. To kick things off, what I would like to do is set a a key called my key, and it's going to be the value dev leader was here. This should feel like a dictionary, right? A dictionary is a key value pair. In this case, it's going to be string key, string value. It should feel very much like that dictionary collection, but it's not. It is a data store. It is going to be running in that Docker container. So, we set the key and then I'm going to read it back out. Then I'm just going to print it to the console. But if you remember, I did say that we messed something up. That was on purpose. We will fix it. But if I go ahead and press play, the idea here is that we should connect to Reddus, get the database, set the key, read the key back, and print it to the console. This will not work though. So, we'll go ahead and press play. And we should see very quickly that it comes back and says, "Nope, not going to work. Maybe it's not as quick as I thought, but we'll see in just a moment here." Boom. Okay, so it says it was not possible to connect to the Reddus servers. Uh, Eric connecting right now. To allow this multiplexer to continue retrying until it's able to connect, you can set some other setting on here. That's fine. That's not what we're going to do. So, why didn't this work? We went over to Docker Desktop and we were running this thing and it is running. We can see that's the image. That's the container. It is still running. It's using some of my CPU. What the heck is going on, right? It's on my local machine. Well, it all has to do with how we started it. Okay. And Docker Desktop is very simple to use, but the side effect is that because it's so simple, sometimes you can just click through things like I did on purpose and make simple mistakes. Let me go ahead and stop that. If I go to um run this again, right, this already is not going to do what we need to do. I just started it up the exact same way. Let me go ahead and delete this because there's nothing in here. I'm going to go back to images. If I want to go run this image again to make a new container, if I press play, this is the screen that we had when I first downloaded the Reddus image, but I skipped over it because you can just press run. But we need to be able to make sure that we're setting up a port properly. So, what is the port that we need to work with? because by default what it will do is it will randomize the port for the uh the actual connection between Docker and your host machine. So if I want to go pick 11337 as the port, let's go use that. I can go ahead and run that. There is a default port for Reddus as well, but I just want to show you explicitly setting it so that you're familiar with how to do that. That is running. I can go put the port back into here. Now when we go run this, it should connect. We should be able to set this key. We should get it back. And we should see dev leader was here in the console. Let's go ahead and run this. And very quickly, we can see that it did connect. And right at the top, we have dev leader was here. So this is a great way that we can make sure that we do have connectivity with our local Reddus instance running. Now, just to prove that that's actually what's happening, it's not just happening in memory, that kind of thing, right? If I wanted to, I can go comment this out, right? I've stopped the application. It's not running. Reddus is still running. That means if I go run this again, we're not setting the key. We're just reading whatever was in Reddus, right? So, let's go run it. And that value should come right back. And there we go. Yes, it does. Dev leader was here. So, remember, we're not setting it. We're only reading it back. And that's currently what's in the running reddus instance. That's just a very brief tutorial with how you can get Reddus set up running locally on your machine with Docker Desktop. We got to see that we can run the image to start up a container and we need to make sure that we're understanding what the port settings are. Right? So I explicitly set my port to 1 1337. You can set it to something else that's available on your machine. You can use the default Docker port as well. If you just want the default port, it is 6379. So if we go back into Docker very briefly and we stop this when you go to run this again, you would press the run button into here and you could do 6379. Right? So the problem is that it looks like by default that's what it's doing, but we need to specify the host port. If you do this and run it and that way we go back into here, you could just leave it as localhost. With all of that said, you have a very simple application that can connect to your local Reddus instance and that way you can do more complex things from here. So, like I said at the beginning of this video, if you want to see how you can start leveraging Reddus for caching with your ASP.NET Core applications, you can go ahead and check out the playlist that I linked at the beginning of the video or you can check out the videos up here. Thanks, and I'll see you next time.

Frequently Asked Questions

What is the purpose of using Redis in this tutorial?

In this tutorial, I'm demonstrating how to set up a local Redis instance using Docker, which is commonly used for caching in applications. This allows developers to test and develop their applications without needing a cloud setup.

How do I resolve connection issues when trying to connect to Redis from C#?

If you encounter connection issues, it's often due to incorrect port settings. Make sure to explicitly set the port when running the Redis container in Docker. By default, Docker may randomize the port, so I recommend using a specific port like 11337 or the default Redis port 6379.

Can I use the command line instead of the UI to set up Docker and Redis?

Absolutely! While I prefer using the UI in this video, you can also use the command line to pull and run the Redis image. The command line offers a lot of flexibility, and if you're comfortable with it, feel free to use that method.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload