How to Build Docker Image and Run the Container?

Hey there, fellow developers! In this tutorial, I’ll be teaching you how to build a Docker image from a Dockerfile and run a Docker container for your Spring Boot application. Trust me, it’s going to be a lot of fun, and you’ll gain valuable skills in the process!

But hold on for a second! Before we can build a Docker image, we need a Dockerfile first. If you’re new to Docker or haven’t created a Dockerfile before, no worries! I’ve got you covered. Head over to my “Creating Dockerfile for Spring Boot application” tutorial, where I explain everything you need to know about creating a Dockerfile for your Spring Boot application in simple, easy-to-understand terms.

Once you’ve created your Dockerfile, come back here, and we’ll dive right into building the Docker image and running a Docker container for your application.

Are you ready? Let’s get started!

Build Docker image from a Dockerfile

To build a Docker image from a Dockerfile, open your terminal, navigate to your project’s root folder where the Dockerfile is located, and run the following command:

docker build -t my-spring-boot-app .

Replace ‘my-spring-boot-app’ with a name of your choice for your Docker image. The -t flag is used to tag your image with a name, making it easier to reference later. The . at the end of the command tells Docker to look for the Dockerfile in the current directory.

Docker will now build the image using the instructions provided in the Dockerfile. Once it’s done, you can check that your image has been created by running this command:

docker images

Run Docker Container from Docker Image

Now that we have our Docker image let’s run a container from it. Use the following command:

docker run -p 8080:8080 my-spring-boot-app

Replace ‘my-spring-boot-app’ with the name you chose for your Docker image. The -p 8080:8080 part maps the container’s port 8080 (the default port for Spring Boot apps) to port 8080 on your host machine, allowing you to access your app using your host machine’s IP address.

Run a container in the background (detached mode)

Running a container in the background, also known as detached mode, allows the container to execute without occupying the terminal or command prompt. This is particularly useful when you want to continue using the terminal for other tasks while the container is running.

To run a container in the background (detached mode), you need to use the -d flag in the docker run command. Here’s an example:

docker run -d my-image-name

Replace ‘my-image-name’ with the name of your Docker image. When you execute this command, Docker will run the container in the background, and you’ll regain control of the terminal immediately. The command will return the container ID, which you can use to manage or inspect the container later.

How to Stop a Running Container?

And finally, to stop a container running in detached mode, use the docker stop command followed by the container ID or name:

docker stop my-container-name

Customizing the Container Startup Behavior

You can customize the container startup behaviour by setting environment variables and exposing ports. Let’s go through some examples:

Setting environment variables

To set environment variables, use the -e flag followed by the variable name and its value. For example, if you want to set an environment variable called ‘MY_ENV_VARIABLE’ with a value of ‘my-value’, you would run:

docker run -p 8080:8080 -e MY_ENV_VARIABLE=my-value my-spring-boot-app

You can set multiple environment variables by adding more -e flags, like this:

docker run -p 8080:8080 -e MY_ENV_VARIABLE1=my-value1 -e MY_ENV_VARIABLE2=my-value2 my-spring-boot-app

Exposing ports

We’ve already seen how to expose ports when we mapped port 8080 of the container to port 8080 on the host machine. If you want to expose a different port, simply change the values after the -p flag. For example, to expose port 8081 instead, you would run:

docker run -p 8081:8080 my-spring-boot-app

This maps the container’s port 8080 to port 8081 on your host machine. Remember to update your Spring Boot application’s configuration if you change the port.

Final words

And that’s it! You now know how to build a Docker image, run a container with the correct configuration for a Spring Boot application, and customize the container’s startup behaviour by setting environment variables and exposing ports.

Here are the key takeaways:

  1. Building a Docker image involves creating a Dockerfile with instructions for the container.
  2. Running a Docker container requires using the docker run command with the correct configuration for a Spring Boot app.
  3. Customizing container startup behaviour can be achieved by setting environment variables and exposing ports.

To learn more about Docker, please check Docker Tutorials page.

Have fun experimenting, and happy coding!