
Lesson Description
The "Building a Docker Image" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:
Josh builds a containerized version of the application using Docker, which allows it to be built for other platforms. Application properties are passed to the Docker image on startup through the Docker CLI.
Transcript from the "Building a Docker Image" Lesson
[00:00:00]
>> Josh Long: Okay, so that's this. That's one thing you can do is graalvm, and I think that's a worthwhile thing to do. The other thing you might want to do is to turn it into a native image. So you can do this even if you don't have GRAALVM on your local machine.
[00:00:11]
So, sorry, we're going to turn it into a Docker image now. So what we're going to do is we're going to say, d, skip tests, P, native spring boot colon, build image. Can you all do this? Okay, this right here. Trying to copy and paste it, but it's going poorly.
[00:00:38]
There you go, okay. Do that in the root of your project and that'll build you a Docker image that's also native. And even if you don't have GRAALVM installed in your local Mac, this should still work, as far as I know. So how do we do everybody get a Docker image?
[00:01:02]
This will take a little while. Takes a minute or two. Because it's going to do the native image compilation inside of Docker, you're going to want to start creating some environment variables. So export, spring, data source, URL equals username equals password equals. It'll be secret, it'll be my user.
[00:01:27]
It'll be JDBC, PostgreSQL, Localhost, my database. Make sure you've got those set in the shell in which you've built the Docker image. I'm going to copy them over here. Paste. Okay, and now you should have a Docker image down here. This is the Docker image name. So you're going to want to set that for the Docker.
[00:01:53]
So spring, data source, password equals. Actually, you know what? This might be easier to do in a script. Put that in a file here, okay and then the name of the build. Okay, so let me see if that works. Whoops. It's going to be. Remember we talked about this yesterday?
[00:03:32]
Host.docker.internal I think it is. Or is it docker.host.internal, that looks right. And then you want to run it on the same port. You want to export the port. So -P8080:8080. There you go, there you go. So now localhost 8080 customers, voila. So do this. Replace this with whatever the Docker image that it should have printed out at the end of the build a few minutes ago.
[00:04:13]
What your Docker image is.
>> Student: And then sorry, are we actually making a script or are we configuring something.
>> Josh Long: I just run this in the shell or create a script or whatever. It's just there's so many moving parts. I just put it in a script. But you can do whatever you want.
[00:04:26]
You just need to set up. You can actually hard code those values in here. I'm just it's nothing too scary. I'm just setting environment variables for the Docker image and I'm using variables to keep it a little cleaner. We've done a sort of speed run of building an application, talking to a database, introducing observability and then packaging it in a way that's suitable for production.
[00:04:45]
And you all basically got there. I'm like, this is easy, right? If I can do this, you can definitely do this. This is an easy thing to do and it's basic stuff. This is all the kind of stuff, this is all the kind of like bread and butter.
[00:05:00]
Every app needs to do this kind of stuff. You're going to have something talking to a database, you might have something on the web, you're going to need to put it into a container and take this to any cloud platform around the world. You can take it to Kubernetes, take it to Cloud Foundry, ticket to anything, right?
[00:05:13]
This is a wonderful result. This is production worthy software. You don't have to look elsewhere for things to dockerize your image. You don't have to look elsewhere for ways to turn it into this nice super fast thing. You don't need to look elsewhere for tools to make it observable.
[00:05:27]
Just add the right starters and then run the right command and it's all provided for you out of the box.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops