Java with Docker in VS Code
This tutorial will walk you through building and deploying a Docker image for a Java application with Visual Studio Code.
We will continue using the same Spring Boot application we built in our first Java Tutorial.
Docker is a container management toolkit which allows users to publish and reuse container images. 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 deploy it to the cloud.
Before you begin
In addition to the Java tools that you needed to install for the Java Tutorial, you also need the 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 also need to have an Azure account ready for the deployment steps.
Install the Docker extension
To have a fully integrated Docker experience, install the Docker extension for VS Code. This extension makes it easy to build and deploy containerized applications from within Visual Studio Code. To install the Docker extension, open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)) and search for
docker to filter the results. Select the Microsoft Docker extension.
For more information, see Working with Docker.
Create a Container
To build your project, navigate to the
complete folder of the Sprint Boot application, and run the Maven command below in a terminal to create the Java assembly files (JAR).
mvn clean package
Docker has a simple Dockerfile file format that it uses to specify the "layers" of an image. Create a Dockerfile in your 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
Right click the Dockerfile from the File 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 the Docker Explorer provided by the Docker Extension.
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.
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!".
Push your image to Docker Hub
You can deploy your Docker image to Azure from either a public or private container registry. In this tutorial, we will use Docker Hub. If you do not have a Docker Hub account, create one from Docker Hub
The first time you expand the Docker Hub node in Docker Explorer, you'll be prompted to log into your Docker Hub account.
Your username 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 Docker Hub by right clicking on the Docker Hub label and choosing Docker Hub Logout. This will delete the credentials from the OS store.
Now push your image to Docker Hub. Make sure the name of your image starts (is 'tagged') with your Docker ID.
Deploying images to Azure App Service
With the Docker Explorer, you can deploy images from Docker Hub 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, after installing the Azure Account extensions, you can run Azure: Sign in from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). You then sign into your account using the Device Login flow. Click on Copy & Open in the notification to open your default browser.
Paste in the access code and continue the sign in process.
You can now right click on an image in Docker Hub or an Azure Container Registry and choose Deploy Image to Azure App Service.
From here you will be prompted for a Resource Group, a 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!