Create Docker Environment locally and deploy a sample web application
Installing Docker Desktop
Previously, when we were working on a Windows or Mac Environment, setting up docker was a difficult task but Docker Desktop is designed to let you build, share and run containers as easily on Mac and Windows as you do on Linux. Docker handles the tedious and complex setup so you can focus on writing code.
Pre-requisites for Docker Desktop
- Configure WSL 2 ( Recommended for Windows 10, 11 - All Editions)
- Hyper-V backend ( Recommended for Windows 10, 11 - Professional, Enterprise, Education Editions)
We will use WSL 2 since its a easiest way and most importantly, it supports all Editions.
Go to Docker website and download the docker desktop, in this walkthrough, I will be focusing on Windows Environment. Double-click on Docker Desktop Installer.exe to run the installer. This will take few mins to get installed.
Once Installed, you will see below message
Now, Open Docker Desktop. Accept the terms and conditions which will start the Docker Desktop. For more details look over the tutorials provided by clicking on the start button
Note: If Docker freezes with Docker is Starting then, go to troubleshoot icon in top right and click on Clean/Purge data and select WSL 2 option and click on Delete
Background (FYI): Once Docker Desktop starts, you can observe a Linux distributions for Docket desktop starts running, you can see by running below command
wsl -l -v
Done, now we are running a Docker Environment locally under WSL (Windows Subsystem for Linux Distributions) without any hiccups.
Visual Studio Code Environment for Docker
Creating a Dev Environment now using Docker Desktop is very simple, currently its is preview mode. Let's create a Dev Environment. Make sure you have the VS code installed in your environment.
Click on Create New Environment, and setup the Environment based on your requirement. I am going with the local environment and if you have code setup centrally in git then provide the Git path accordingly
Click on Continue and will take few mins to setup the VS code environment, once done, click on Open in VS Code Icon which will open VS code app
Deploy a Sample Web app
Now, we have the Docker up and running. Along with that we have a VS code Dev Environment, let's deploy a web app using a Dockerfile.
Below is the simple Dockerfile to deploy a war file inside the tomcat web server and exposing it to port 8080
You can clone my repo to get the Dockerfile and sample.war file.
FROM tomcat:9.0.27-jdk8-openjdkVOLUME /tmpRUN chmod -R 777 $CATALINA_HOME/webappsENV CATALINA_HOME /usr/local/tomcatCOPY sample.war $CATALINA_HOME/webapps/sample.warEXPOSE 8080CMD ["catalina.sh","run"]
Below is the folder structure in VS code
Build the Dockerfile by running the below command in the vs code teminal
docker build -t mywebapp .
Building the Docker File will creates an image for us, you can observe the docker image inside the Docker Desktop app or by running the command "Docker images"
Now, we will run the image to create the container. Lets use our own port to expose the web application. Run the following command to create the container and map the port as per your requirement
docker run -p 8989:8080 mywebapp
We can check from the browser by hitting the url http://localhost:8989/sample/
Conclusion
In this blog post, I have shown you on how to install Docker Desktop on Windows using WSL. Along with that we created a VS code Environment and build and deployed a sample web application from the Docker file. Hope this blog helped you in your similar use case.Thank you for reading!
Comments
Post a Comment