Java with Docker in VS Code

This 15 minutes tutorial will walk you through the process of building a Docker image for running a Java application in Visual Studio Code.

We will continue using the same Spring Boot application we built in our first Java Tutorial.

Docker is a Linux container management toolkit which allows users to publish container images and consume those published by others. A Docker image is a recipe for running a containerized process. In this tutorial, we will build an Docker image for the web app, run the image locally and then finally deploy it to the cloud.

Before you begin

In addition to the Java tools you needed to install for the Java Tutorial, you would also need to have tools for Docker. See the Install Docker documentation for details on setting Docker up for your machine. Before proceeding further, verify you can run Docker commands from the shell.

You would also need to have your Azure account ready for the deployment steps.

Install the Docker extension

To enable fully integrated Docker experience, you can install the Docker extension for VS Code. This extension makes it easy to build and deploy containerized applications from Visual Studio Code. To install the Docker extension, open the Extension view by pressing ⇧⌘X (Windows, Linux Ctrl+Shift+X) and search for vscode-docker to filter the results. Select the Docker Support extension.

Install Docker

For more information, please check Working with Docker.

Containerize the application

Build your project. Navigate to the complete folder of the Sprint Boot application, and run below Maven command in the terminal to create the Java assembly files.

mvn clean package

Docker has a simple Dockerfile file format that it uses to specify the "layers" of an image. Now create a Dockerfile in our project under the complete folder with the following content:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
ADD target/gs-spring-boot-0.1.0.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar

Now right click the DockerFile from the Explorer, and choose the Build Image command from the context menu. You will be prompted to name your image and once it's done, you can see the image in your Docker Explorer provided by the Docker Extension.

Build Image

Run your container image locally

Click the Run command by right clicking the image you just built and your Docker image will start running locally.

Run Container

Test the web app by browsing to http://localhost:8080 using a web browser. You should see the following message displayed: "Greetings from Spring Boot!".

Greeting from Spring

Push your image to Docker Hub

You can deploy your docker image to Azure from either public or private container registory. In this tutorial, we will use Docker Hub. If you do not have a DockerHub account, create one from Docker Hub

The first time you expand the DockerHub node in Docker Explorer, you'll be prompted to log into your DockerHub account.

DockerHub Login

Your user name and password are stored in your operating system credentials vault (for example, MacOS keychain or Windows Credential Store) so that you don't need to log in every time. You can log out of DockerHub by right clicking on the DockerHub label and choosing Log Out. This will delete the credentials from the OS store.

Then push your image to DockHub. Make sure the name of your image starts with your Docker ID.

Push Image

Deploying images to Azure App Service

With the Docker Explorer, you can deploy images from DockerHub Registries or Azure Container Registries, directly to an Azure App Service instance. This functionality requires installing the Azure Account extension and an Azure Subscription. If you do not have an Azure subscription, sign up today for a free 30 day account and get $200 in Azure Credits to try out any combination of Azure services.

To log into Azure, run Azure Login from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). You can then sign into your account using the Device Login flow. Click on Copy & Open to open your default browser.

Azure Login

Paste in the access code and continue the sign in process.

Azure Login

You can now right click on an image in DockerHub or an Azure Container Registry and choose Deploy Image to Azure App Service.

Deploy to Azure

From here you will be prompted for a Resource Group, location, an App Service Plan, and a globally unique website name. Once it's deployed to Azure App Service, you will get a URL for the web app running in the cloud!

Next steps