DockerTutorialsFree and quality Java programming tutorials, articles, guides, code examples, sample projects and best practiceshttps://www.codejava.net/docker2024-05-18T14:09:17-05:00Joomla! - Open Source Content ManagementDownload and Install Docker Desktop for Mac2022-06-06T00:21:48-05:002022-06-06T00:21:48-05:00https://www.codejava.net/docker/install-docker-desktop-for-macNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>Docker is the most popular platform for building, shipping and running applications in isolated environment called container. To get started with Docker, you need to have Docker software installed on your computer, which is called Docker Desktop on Windows/Mac environment.</p> <p>In this article, I’d like to guide you how to download and install Docker Desktop for Mac, on macOS operating system.</p> <p>&nbsp;</p> <h2>1. What is Docker Desktop?</h2> <p>You know, Docker Desktop is a suite of software applications that enable you to build and share containerized applications and microservices on Windows or Mac environment. It is free for small business that have under 250 employees.</p> <p>When you install Docker Desktop, you get the following programs and tools installed:</p> <p>- Docker daemon: is the Docker engine that powers containerized apps</p> <p>- Docker Client: is the command-line client which you can use to connect to Docker engine</p> <p>- Docker Compose: is used to build and run multi-container apps</p> <p>- Docker Content Trust: is used to digitally sign and verify Docker images</p> <p>- Kubernetes: is the container orchestration system</p> <p>- Credential Helper: is used to safely store Docker credentials in native stores</p> <p>Note that on macOS operating system, the Docker daemon runs in a LinuxKit virtual machine, which runs on the Hypervisor framework provided by Apple’s OS. That means Docker is not running natively on Mac - it requires a virtualized Linux OS to operate.</p> <h2><br />2. Download Docker Desktop for Mac</h2> <p>In order to install Docker Desktop for Mac, your macOS must be version 10.15 or newer. That means you can install Docker Desktop on macOS Catalina, Big Sur, Monterey or newer.</p> <p>Click <a href="https://www.docker.com/products/docker-desktop/" rel="nofollow" target="_blank">this link</a> to head over to the Docker Desktop’s official download page. You’ll see the following download options:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/docker_desktop_for_mac_options.png" alt="docker desktop for mac options" width="580" height="331" /></p> <p>Click the button <i>Mac with Intel Chip</i> if your Mac running on Intel’s CPU (amd64 architecture); or click the button <i>Mac with Apple Chip</i> if your Mac running on Apple’s CPU (arm64 architecture) such as Apple M1.</p> <p>The result is you have the Docker.dmg file gets downloaded on to your computer.</p> <h2><br />3. Install Docker Desktop on macOS</h2> <p>Install Docker Desktop for Mac is pretty easy. Firstly, double click the Docker.img file to launch the installer. Then you will see the following dialog:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/drag_and_drop_docker_on_macos.png" alt="drag and drop docker on macos" width="580" height="276" /></p> <p>Drag the Docker’s whale icon into the Applications folder. And wait for a moment while it is copying the data files:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/copying_docker_to_applications.png" alt="copying docker to applications" width="580" height="128" /></p> <p>Then you will see the Docker icon appears in the Launchpad. Double click it to start Docker Desktop for Mac. For the first time you need to provide administrator’s password to get Docker Helper installed:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/install_docker_helper.png" alt="install docker helper" width="620" height="406" /></p> <p>Then the following screen appears:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/accept_dockers_terms.png" alt="accept dockers terms" width="700" height="529" /></p> <p>Check the option <i>I accept the terms</i>, and click Accept to proceed. Wait for a while, then you will see Docker Desktop started as below:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/docker_desktop_started.png" alt="docker desktop started" width="800" height="553" /></p> <p>That’s done! You have successfully installed Docker Desktop for Mac on macOS. It’s time to explore and have fun containerizing your applications.</p> <p>You can also watch the following video to see the installation in action:</p> <p>https://www.youtube.com/watch?v=tlr18csEwdc</p> <p>&nbsp;</p> <h3 style="font-family: 'Open Sans', sans-serif; color: #333333;">Related Articles:</h3> <ul style="font-size: 14.3px;"> <li><a style="color: #095197; cursor: pointer;" href="https://codejava.net/docker/create-docker-image-for-spring-boot-app" rel="nofollow" target="_blank">Create Docker Image for Spring Boot Application</a></li> <li><a href="docker/install-docker-desktop-for-windows" target="_blank">Download and Install Docker Desktop for Windows</a></li> </ul></div><div class="feed-description"><p>Docker is the most popular platform for building, shipping and running applications in isolated environment called container. To get started with Docker, you need to have Docker software installed on your computer, which is called Docker Desktop on Windows/Mac environment.</p> <p>In this article, I’d like to guide you how to download and install Docker Desktop for Mac, on macOS operating system.</p> <p>&nbsp;</p> <h2>1. What is Docker Desktop?</h2> <p>You know, Docker Desktop is a suite of software applications that enable you to build and share containerized applications and microservices on Windows or Mac environment. It is free for small business that have under 250 employees.</p> <p>When you install Docker Desktop, you get the following programs and tools installed:</p> <p>- Docker daemon: is the Docker engine that powers containerized apps</p> <p>- Docker Client: is the command-line client which you can use to connect to Docker engine</p> <p>- Docker Compose: is used to build and run multi-container apps</p> <p>- Docker Content Trust: is used to digitally sign and verify Docker images</p> <p>- Kubernetes: is the container orchestration system</p> <p>- Credential Helper: is used to safely store Docker credentials in native stores</p> <p>Note that on macOS operating system, the Docker daemon runs in a LinuxKit virtual machine, which runs on the Hypervisor framework provided by Apple’s OS. That means Docker is not running natively on Mac - it requires a virtualized Linux OS to operate.</p> <h2><br />2. Download Docker Desktop for Mac</h2> <p>In order to install Docker Desktop for Mac, your macOS must be version 10.15 or newer. That means you can install Docker Desktop on macOS Catalina, Big Sur, Monterey or newer.</p> <p>Click <a href="https://www.docker.com/products/docker-desktop/" rel="nofollow" target="_blank">this link</a> to head over to the Docker Desktop’s official download page. You’ll see the following download options:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/docker_desktop_for_mac_options.png" alt="docker desktop for mac options" width="580" height="331" /></p> <p>Click the button <i>Mac with Intel Chip</i> if your Mac running on Intel’s CPU (amd64 architecture); or click the button <i>Mac with Apple Chip</i> if your Mac running on Apple’s CPU (arm64 architecture) such as Apple M1.</p> <p>The result is you have the Docker.dmg file gets downloaded on to your computer.</p> <h2><br />3. Install Docker Desktop on macOS</h2> <p>Install Docker Desktop for Mac is pretty easy. Firstly, double click the Docker.img file to launch the installer. Then you will see the following dialog:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/drag_and_drop_docker_on_macos.png" alt="drag and drop docker on macos" width="580" height="276" /></p> <p>Drag the Docker’s whale icon into the Applications folder. And wait for a moment while it is copying the data files:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/copying_docker_to_applications.png" alt="copying docker to applications" width="580" height="128" /></p> <p>Then you will see the Docker icon appears in the Launchpad. Double click it to start Docker Desktop for Mac. For the first time you need to provide administrator’s password to get Docker Helper installed:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/install_docker_helper.png" alt="install docker helper" width="620" height="406" /></p> <p>Then the following screen appears:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/accept_dockers_terms.png" alt="accept dockers terms" width="700" height="529" /></p> <p>Check the option <i>I accept the terms</i>, and click Accept to proceed. Wait for a while, then you will see Docker Desktop started as below:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-mac/docker_desktop_started.png" alt="docker desktop started" width="800" height="553" /></p> <p>That’s done! You have successfully installed Docker Desktop for Mac on macOS. It’s time to explore and have fun containerizing your applications.</p> <p>You can also watch the following video to see the installation in action:</p> <p>https://www.youtube.com/watch?v=tlr18csEwdc</p> <p>&nbsp;</p> <h3 style="font-family: 'Open Sans', sans-serif; color: #333333;">Related Articles:</h3> <ul style="font-size: 14.3px;"> <li><a style="color: #095197; cursor: pointer;" href="https://codejava.net/docker/create-docker-image-for-spring-boot-app" rel="nofollow" target="_blank">Create Docker Image for Spring Boot Application</a></li> <li><a href="docker/install-docker-desktop-for-windows" target="_blank">Download and Install Docker Desktop for Windows</a></li> </ul></div>Create Docker Image for Spring Boot Application2022-05-05T16:49:01-05:002022-05-05T16:49:01-05:00https://www.codejava.net/docker/create-docker-image-for-spring-boot-appNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>Docker is widely-used open platform for developing, shipping and running applications. In this article, I’d like to share with you guys, about how to create and build Docker image for a Spring Boot application, i.e. containerizing your Spring Boot application.</p> <p>You’ll also learn how to run the created Docker image, and share your Docker image on Docker Hub. I suppose that you have the following things ready on your computer:</p> <ul> <li>Docker Desktop (for Windows or macOS) or Docker Engine for Linux</li> <li>An account on Docker Hub</li> <li>An existing Spring Boot project without database server (I’ll share how to create Docker image for Spring boot app with database in a separate article). Also you need to have JDK and Maven installed.</li> </ul> <p>&nbsp;</p> <h2>1. What is Docker?</h2> <p>Let’s understand briefly about Docker. You know, Docker enables you to separate your applications from infrastructure so you can deliver software quickly. It provides the ability to package and run application in a container - a loosely isolated Linux environment.</p> <p>Compared to virtual machines, containers are more lightweight and faster as they share the same operating system. Whereas each virtual machine needs to run on its own OS. Docker engine is the software that manages containers running on a computer.</p> <p>You can use Docker for the following purposes (not limited to):</p> <ul> <li>Deploy your application as a container</li> <li>Share your app as a Docker image so everyone can get the same container that works in the same way</li> <li>Test different versions of your app in separate containers, without affecting development and production environment</li> <li>Streamline development with Continuous Integration and Continuous Delivery (CI/CD) workflows.</li> </ul> <h2><br />2. Package Spring Boot Project</h2> <p>Open a new command prompt (Windows) or terminal (macOS/Linux). Change the current directory to the project’s root directory. For example:</p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">cd SpringBootFormExample</span></p> <p>Then type the following commands to clean and package the project:</p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">mvn clean</span></p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">mvn package</span></p> <p>Depending on the packaging type specified in the project’s <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">pom.xml </span>file, a WAR or JAR file will be created under the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">target</span> directory. As you can see in the Maven’s ouput:</p> <pre class="brush:text">[INFO] Processing war project [INFO] Copying webapp resources [f:\SpringBoot\SpringBootFormExample\src\main\webapp] [INFO] Webapp assembled in [303 msecs] [INFO] Building war: f:\SpringBoot\SpringBootFormExample\target\SpringBootFormExample-1.0.war</pre> <p>Basically, you can use the <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">java -jar filename</span></strong></span> command to run a Spring Boot application packaged in a WAR or JAR file.</p> <h2><br />3. Create Dockerfile</h2> <p>To create a Docker image for the Spring Boot application packaged in the previous step, you need to create a text file called <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">Dockerfile</span> under the project’s root (Note: no extension, just <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">Dockerfile</span>). And specify the following content:</p> <pre class="brush:text">FROM openjdk:11 COPY target/SpringBootFormExample-1.0.war SpringBootFormExample-1.0.war ENTRYPOINT ["java", "-jar", "/SpringBootFormExample-1.0.war"]</pre> <p>The first line uses the command FROM that specifies an image name which can be found on Docker Hub registry. The image name <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">openjdk:11</span></strong></span> is an official image created by Docker. It contains a Linux operating system with OpenJDK version 11 installed.</p> <p>The second line (COPY) copies the WAR file from the project’s target directory to the root directory of the operating system in the created image.</p> <p>The third line (ENTRYPOINT) specifies the command that will launch the Spring Boot application when a container started from the image.</p> <p>&nbsp;</p> <h2>4. Build Docker Image for Spring Boot Application</h2> <p>Next, type the following command to build a Docker image for the current Spring Boot project:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker build -t form-example .</span></strong></span></p> <p>Note that there is a dot (.) at the end, denoting the current directory. You’ll see the following output:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_build_command.png" alt="run docker build command" width="900" height="360" /></p> <p>You can see it pulls the image openjdk:11 from registry-1.docker.io, copies the WAR file and creates the image. Then type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker images</span></strong></span> to verify:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_images_command.png" alt="run docker images command" /></p> <p>You can see the image created with the name form-example with the size is about 681MB.</p> <p>&nbsp;</p> <h2>5. Run Spring Boot Application as a Docker Container</h2> <p>With the Docker image built in the previous step, you can now run it as a container. Docker allows you to run a container in either detached mode or attached mode.</p> <p>&nbsp;</p> <h3><span style="line-height: 107%;">Run Docker image in attached mode:</span></h3> <p>In attached mode, the Docker command-line client’s console is attached to the output of the application running in the container. Type the following command to run the image form-example:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker run -p 8080:8080 form-example</span></strong></span></p> <p><span style="font-size: 11pt; line-height: 107%; font-family: Calibri, sans-serif;">Here, the -p flag specifies port bind between the port number on the host OS with the port number in the container. You’ll the familiar startup logs of a Spring Boot application:</span></p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/docker_run_image_in_attached_mode.png" alt="docker run image in attached mode" width="900" height="577" /></p> <p>If you see this, it means the Spring Boot application is running inside a Docker container, ready to accept incoming requests via port 8080. You can now type <i>http://localhost:8080</i> in your browser to access the Spring Boot application deployed as a Docker container.</p> <p>Note that the current command prompt/terminal is attached to the application’s output. So open a new command prompt and type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker ps</span></strong></span> to see a list of running containers:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_ps_command.png" alt="run docker ps command" width="1024" height="112" /></p> <p>You see, there’s one container running from the image form-example. And the container is assigned a random, friendly name brave_lamport.</p> <p>To stop the container, type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker stop</span></strong> </span>command as below:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_stop_command.png" alt="run docker stop command" /></p> <p>You can specify the container’s assigned name or container ID as seen by the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">docker ps</span> command.</p> <p>&nbsp;</p> <h3><span style="line-height: 107%;">Run Docker image in detached mode:</span></h3> <p>Type the following command to run a container from the image form-example in detached mode:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker run -dp 80:8080 form-example</span></strong></span></p> <p><span style="font-size: 11pt; line-height: 107%; font-family: Calibri, sans-serif;">Here, the -d flag is for detached mode. The command starts a new container and returns immediately, resulting the container running in background. That means you won’t see the output of your Spring Boot application:</span></p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/docker_run_image_in_detached_mode.png" alt="docker run image in detached mode" /></p> <p>Now, type <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">docker ps</span> command, you’ll see another container created and running:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_ps_command_2nd_time.png" alt="run docker ps command 2nd time" width="1024" height="106" /></p> <p>You see, this time it binds the port 80 in host machine with the port 8080 in the container, which means you should access your Spring Boot application via URL <i>http://localhost</i>.</p> <p>&nbsp;</p> <h2>6. Share Docker Image on Docker Hub</h2> <p>You can share your Docker image on Docker Hub (a public registry managed by Docker) so everyone can get the same container that works in the same way.</p> <p>Sign in to <a href="https://hub.docker.com/" rel="nofollow" target="_blank">https://hub.docker.com</a> and click Create Repository button to create a new repository (choose the visibility is public). For example, I created the repository <i>namhm/form-example</i>.</p> <p>Then in the command line, type the following command to update tag name of the image according to the repository name:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker tag form-example namhm/form-example</span></strong></span></p> <p>Then type the following command to log into Docker Hub in command line:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker login -u your_user_name</span></strong></span></p> <p>You need to provide password. Then type this command to share the image:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker push namhm/form-example</span></strong></span></p> <p>Done. Wait a while for the image being uploaded to Docker registry. Then you can switch to your Docker Hub account to verify the image pushed successfully.</p> <p>That’s my tutorial about Creating Docker image for Spring Boot application. You also learn how to run Docker image and share the image on Docker Hub. To see the steps in action, I recommend you watch the following video:</p> <p>https://www.youtube.com/watch?v=T8LqONZzMQI</p> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">Reference Links:</span></strong></p> <ul> <li><a href="https://www.docker.com/" rel="nofollow" target="_blank">Docker Home page</a></li> <li><a href="https://docs.docker.com/get-started/overview/" rel="nofollow" target="_blank">Docker Documentation</a></li> <li><a href="https://hub.docker.com/" rel="nofollow" target="_blank">Docker Hub</a></li> <li><a href="https://docs.docker.com/engine/reference/builder/" rel="nofollow" target="_blank">Dockerfile Reference</a></li> <li><a href="https://docs.docker.com/engine/reference/commandline/cli/" rel="nofollow" target="_blank">Docker Command-line Reference</a></li> </ul> <p>&nbsp;</p> <h3>Related Articles:</h3> <ul> <li><a href="docker/install-docker-desktop-for-windows" target="_blank">Download and Install Docker Desktop for Windows</a></li> </ul> <p>&nbsp;</p> <p><strong>You might be also interested in these articles:</strong></p> <ul style="font-size: 14.3px;"> <li><a style="color: #095197; cursor: pointer;" href="heroku/deploy-simple-spring-boot-project" target="_blank">Deploy Simple Spring Boot Project to Heroku using Git and Heroku CLI</a></li> <li><a style="color: #095197; cursor: pointer;" href="heroku/deploy-spring-boot-app-with-mysql" target="_blank">Deploy Spring Boot App with MySQL Database on Heroku</a></li> </ul></div><div class="feed-description"><p>Docker is widely-used open platform for developing, shipping and running applications. In this article, I’d like to share with you guys, about how to create and build Docker image for a Spring Boot application, i.e. containerizing your Spring Boot application.</p> <p>You’ll also learn how to run the created Docker image, and share your Docker image on Docker Hub. I suppose that you have the following things ready on your computer:</p> <ul> <li>Docker Desktop (for Windows or macOS) or Docker Engine for Linux</li> <li>An account on Docker Hub</li> <li>An existing Spring Boot project without database server (I’ll share how to create Docker image for Spring boot app with database in a separate article). Also you need to have JDK and Maven installed.</li> </ul> <p>&nbsp;</p> <h2>1. What is Docker?</h2> <p>Let’s understand briefly about Docker. You know, Docker enables you to separate your applications from infrastructure so you can deliver software quickly. It provides the ability to package and run application in a container - a loosely isolated Linux environment.</p> <p>Compared to virtual machines, containers are more lightweight and faster as they share the same operating system. Whereas each virtual machine needs to run on its own OS. Docker engine is the software that manages containers running on a computer.</p> <p>You can use Docker for the following purposes (not limited to):</p> <ul> <li>Deploy your application as a container</li> <li>Share your app as a Docker image so everyone can get the same container that works in the same way</li> <li>Test different versions of your app in separate containers, without affecting development and production environment</li> <li>Streamline development with Continuous Integration and Continuous Delivery (CI/CD) workflows.</li> </ul> <h2><br />2. Package Spring Boot Project</h2> <p>Open a new command prompt (Windows) or terminal (macOS/Linux). Change the current directory to the project’s root directory. For example:</p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">cd SpringBootFormExample</span></p> <p>Then type the following commands to clean and package the project:</p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">mvn clean</span></p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">mvn package</span></p> <p>Depending on the packaging type specified in the project’s <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">pom.xml </span>file, a WAR or JAR file will be created under the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">target</span> directory. As you can see in the Maven’s ouput:</p> <pre class="brush:text">[INFO] Processing war project [INFO] Copying webapp resources [f:\SpringBoot\SpringBootFormExample\src\main\webapp] [INFO] Webapp assembled in [303 msecs] [INFO] Building war: f:\SpringBoot\SpringBootFormExample\target\SpringBootFormExample-1.0.war</pre> <p>Basically, you can use the <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">java -jar filename</span></strong></span> command to run a Spring Boot application packaged in a WAR or JAR file.</p> <h2><br />3. Create Dockerfile</h2> <p>To create a Docker image for the Spring Boot application packaged in the previous step, you need to create a text file called <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">Dockerfile</span> under the project’s root (Note: no extension, just <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">Dockerfile</span>). And specify the following content:</p> <pre class="brush:text">FROM openjdk:11 COPY target/SpringBootFormExample-1.0.war SpringBootFormExample-1.0.war ENTRYPOINT ["java", "-jar", "/SpringBootFormExample-1.0.war"]</pre> <p>The first line uses the command FROM that specifies an image name which can be found on Docker Hub registry. The image name <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">openjdk:11</span></strong></span> is an official image created by Docker. It contains a Linux operating system with OpenJDK version 11 installed.</p> <p>The second line (COPY) copies the WAR file from the project’s target directory to the root directory of the operating system in the created image.</p> <p>The third line (ENTRYPOINT) specifies the command that will launch the Spring Boot application when a container started from the image.</p> <p>&nbsp;</p> <h2>4. Build Docker Image for Spring Boot Application</h2> <p>Next, type the following command to build a Docker image for the current Spring Boot project:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker build -t form-example .</span></strong></span></p> <p>Note that there is a dot (.) at the end, denoting the current directory. You’ll see the following output:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_build_command.png" alt="run docker build command" width="900" height="360" /></p> <p>You can see it pulls the image openjdk:11 from registry-1.docker.io, copies the WAR file and creates the image. Then type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker images</span></strong></span> to verify:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_images_command.png" alt="run docker images command" /></p> <p>You can see the image created with the name form-example with the size is about 681MB.</p> <p>&nbsp;</p> <h2>5. Run Spring Boot Application as a Docker Container</h2> <p>With the Docker image built in the previous step, you can now run it as a container. Docker allows you to run a container in either detached mode or attached mode.</p> <p>&nbsp;</p> <h3><span style="line-height: 107%;">Run Docker image in attached mode:</span></h3> <p>In attached mode, the Docker command-line client’s console is attached to the output of the application running in the container. Type the following command to run the image form-example:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker run -p 8080:8080 form-example</span></strong></span></p> <p><span style="font-size: 11pt; line-height: 107%; font-family: Calibri, sans-serif;">Here, the -p flag specifies port bind between the port number on the host OS with the port number in the container. You’ll the familiar startup logs of a Spring Boot application:</span></p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/docker_run_image_in_attached_mode.png" alt="docker run image in attached mode" width="900" height="577" /></p> <p>If you see this, it means the Spring Boot application is running inside a Docker container, ready to accept incoming requests via port 8080. You can now type <i>http://localhost:8080</i> in your browser to access the Spring Boot application deployed as a Docker container.</p> <p>Note that the current command prompt/terminal is attached to the application’s output. So open a new command prompt and type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker ps</span></strong></span> to see a list of running containers:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_ps_command.png" alt="run docker ps command" width="1024" height="112" /></p> <p>You see, there’s one container running from the image form-example. And the container is assigned a random, friendly name brave_lamport.</p> <p>To stop the container, type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker stop</span></strong> </span>command as below:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_stop_command.png" alt="run docker stop command" /></p> <p>You can specify the container’s assigned name or container ID as seen by the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">docker ps</span> command.</p> <p>&nbsp;</p> <h3><span style="line-height: 107%;">Run Docker image in detached mode:</span></h3> <p>Type the following command to run a container from the image form-example in detached mode:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker run -dp 80:8080 form-example</span></strong></span></p> <p><span style="font-size: 11pt; line-height: 107%; font-family: Calibri, sans-serif;">Here, the -d flag is for detached mode. The command starts a new container and returns immediately, resulting the container running in background. That means you won’t see the output of your Spring Boot application:</span></p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/docker_run_image_in_detached_mode.png" alt="docker run image in detached mode" /></p> <p>Now, type <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">docker ps</span> command, you’ll see another container created and running:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/run_docker_ps_command_2nd_time.png" alt="run docker ps command 2nd time" width="1024" height="106" /></p> <p>You see, this time it binds the port 80 in host machine with the port 8080 in the container, which means you should access your Spring Boot application via URL <i>http://localhost</i>.</p> <p>&nbsp;</p> <h2>6. Share Docker Image on Docker Hub</h2> <p>You can share your Docker image on Docker Hub (a public registry managed by Docker) so everyone can get the same container that works in the same way.</p> <p>Sign in to <a href="https://hub.docker.com/" rel="nofollow" target="_blank">https://hub.docker.com</a> and click Create Repository button to create a new repository (choose the visibility is public). For example, I created the repository <i>namhm/form-example</i>.</p> <p>Then in the command line, type the following command to update tag name of the image according to the repository name:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker tag form-example namhm/form-example</span></strong></span></p> <p>Then type the following command to log into Docker Hub in command line:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker login -u your_user_name</span></strong></span></p> <p>You need to provide password. Then type this command to share the image:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">docker push namhm/form-example</span></strong></span></p> <p>Done. Wait a while for the image being uploaded to Docker registry. Then you can switch to your Docker Hub account to verify the image pushed successfully.</p> <p>That’s my tutorial about Creating Docker image for Spring Boot application. You also learn how to run Docker image and share the image on Docker Hub. To see the steps in action, I recommend you watch the following video:</p> <p>https://www.youtube.com/watch?v=T8LqONZzMQI</p> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">Reference Links:</span></strong></p> <ul> <li><a href="https://www.docker.com/" rel="nofollow" target="_blank">Docker Home page</a></li> <li><a href="https://docs.docker.com/get-started/overview/" rel="nofollow" target="_blank">Docker Documentation</a></li> <li><a href="https://hub.docker.com/" rel="nofollow" target="_blank">Docker Hub</a></li> <li><a href="https://docs.docker.com/engine/reference/builder/" rel="nofollow" target="_blank">Dockerfile Reference</a></li> <li><a href="https://docs.docker.com/engine/reference/commandline/cli/" rel="nofollow" target="_blank">Docker Command-line Reference</a></li> </ul> <p>&nbsp;</p> <h3>Related Articles:</h3> <ul> <li><a href="docker/install-docker-desktop-for-windows" target="_blank">Download and Install Docker Desktop for Windows</a></li> </ul> <p>&nbsp;</p> <p><strong>You might be also interested in these articles:</strong></p> <ul style="font-size: 14.3px;"> <li><a style="color: #095197; cursor: pointer;" href="heroku/deploy-simple-spring-boot-project" target="_blank">Deploy Simple Spring Boot Project to Heroku using Git and Heroku CLI</a></li> <li><a style="color: #095197; cursor: pointer;" href="heroku/deploy-spring-boot-app-with-mysql" target="_blank">Deploy Spring Boot App with MySQL Database on Heroku</a></li> </ul></div>Download and Install Docker Desktop for Windows2022-06-02T05:56:22-05:002022-06-02T05:56:22-05:00https://www.codejava.net/docker/install-docker-desktop-for-windowsNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>Docker is the most popular platform for building, shipping and running applications in isolated environment called container. To get started with Docker, you need to have Docker software installed on your computer, which is called Docker Desktop on Windows/Mac environment.</p> <p>In this article, I’d like to guide you how to download and install Docker Desktop for Windows, on Windows 10 operating system.</p> <p>&nbsp;</p> <h2>1. What is Docker Desktop?</h2> <p>You know, Docker Desktop is a suite of software applications that enable you to build and share containerized applications and microservices on Windows or Mac environment. It is free for small business that have under 250 employees.</p> <p>When you install Docker Desktop, you get the following programs and tools installed:</p> <p>- Docker daemon: is the Docker engine that powers containerized apps</p> <p>- Docker Client: is the command-line client which you can use to connect to Docker engine</p> <p>- Docker Compose: is used to build and run multi-container apps</p> <p>- Docker Content Trust: is used to digitally sign and verify Docker images</p> <p>- Kubernetes: is the container orchestration system</p> <p>- Credential Helper: is used to safely store Docker credentials in native stores</p> <h2><br />2. Download Docker Desktop for Windows</h2> <p>Visit the official <a href="https://www.docker.com/products/docker-desktop/" rel="nofollow" target="_blank">download page</a> of Docker Desktop to download Docker Desktop for Windows installer program. Or click <a href="https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe" rel="nofollow" target="_blank">this link</a> to download the installer directly.</p> <p>You’ll get the Docker Desktop Installer.exe file downloaded on to your computer (around 480 MB).</p> <h2><br />3. Installation Requirement</h2> <p>Docker is based on Linux technology so it can’t run natively on Windows. The Docker Desktop for Windows requires Hyper-V or WSL (Windows Subsystem for Linux) that provides virtualized Linux kernel. The latest version of Docker Desktop for Windows works best with WSL 2, so make sure your Windows 10 operating system meets the following requirement: Home or Pro 2004 (build 19041) or higher, or Enterprise or Education 1909 (build 18363) or higher.</p> <p>Open a new command prompt and you should see the version and build number of your Windows operating system:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/check_windows_version.png" alt="check windows version" /></p> <p>If you don’t meet the requirement mentioned above, you should upgrade your OS before installation of Docker Desktop for Windows.</p> <h2><br />4. Install Docker Desktop on Windows</h2> <p>Double click the Docker Desktop Installer.exe file to launch Docker Desktop installer. You will see the following dialog appears:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/install_Docker_configuration.png" alt="install Docker configuration" width="480" height="215" /></p> <p>By default, WSL 2 is checked. Click OK to proceed. Just wait for a while and you will end up this screen:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/docker_installation_succeeded.png" alt="docker installation succeeded" width="480" height="215" /></p> <p>Close this dialog. Then double click the Docker Desktop icon on desktop to launch Docker Desktop application. The following screen appears:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/install_docker_accept_terms.png" alt="install docker accept terms" width="600" height="450" /></p> <p>Check <i>I accept the terms</i>, and click Accept to proceed. Then you will see the massage Docker Desktop stopping… appears few times. Don’t worry, it’s because WSL has not been installed, as shown in the next dialog:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/wsl_2_installation_warning.png" alt="wsl 2 installation warning" width="600" height="220" /></p> <p>Click the link presented in the warning dialog (<a href="https://aka.ms/wsl2kernel" rel="nofollow" target="_blank">https://aka.ms/wsl2kernel</a>) to complete the installation of WSL 2. You’ll be directed to the step 4 of WSL 2 Manual Installation guide on Microsoft’s website:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/install_wsl_2_-_step_4.png" alt="install wsl 2 - step 4" width="800" height="521" /></p> <p>On this page, click the link <i>WSL2 Linux kernel update package for x64 machines</i>, you will be asked to download the wsl_update_x64.msi file. Download and run this file to launch Windows Subsystem for Linux Update Setup, as shown below:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/WSL2_update_setup.png" alt="WSL2 update setup" width="450" height="352" /></p> <p>Click Next to install. And when it’s done, return to the warning dialog, and click Restart:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/Restart_Docker_Desktop.png" alt="Restart Docker Desktop" width="600" height="220" /></p> <p>Wait for a while for Docker Desktop restarting. Finally, you should see the following screen:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/docker_desktop_started.png" alt="docker desktop started" width="700" height="526" /></p> <p>Congratulations! You have successfully installed Docker on your Windows operating system. It’s time to explore and have fun with containerized applications. To see the installation in action, watch the following video:</p> <p>https://www.youtube.com/watch?v=C3w59EHoxC0</p> <p>&nbsp;</p> <h3>Related Articles:</h3> <ul> <li><a href="https://codejava.net/docker/create-docker-image-for-spring-boot-app" rel="nofollow" target="_blank">Create Docker Image for Spring Boot Application</a></li> <li><a href="docker/install-docker-desktop-for-mac" target="_blank">Download and Install Docker Desktop for Mac</a></li> </ul></div><div class="feed-description"><p>Docker is the most popular platform for building, shipping and running applications in isolated environment called container. To get started with Docker, you need to have Docker software installed on your computer, which is called Docker Desktop on Windows/Mac environment.</p> <p>In this article, I’d like to guide you how to download and install Docker Desktop for Windows, on Windows 10 operating system.</p> <p>&nbsp;</p> <h2>1. What is Docker Desktop?</h2> <p>You know, Docker Desktop is a suite of software applications that enable you to build and share containerized applications and microservices on Windows or Mac environment. It is free for small business that have under 250 employees.</p> <p>When you install Docker Desktop, you get the following programs and tools installed:</p> <p>- Docker daemon: is the Docker engine that powers containerized apps</p> <p>- Docker Client: is the command-line client which you can use to connect to Docker engine</p> <p>- Docker Compose: is used to build and run multi-container apps</p> <p>- Docker Content Trust: is used to digitally sign and verify Docker images</p> <p>- Kubernetes: is the container orchestration system</p> <p>- Credential Helper: is used to safely store Docker credentials in native stores</p> <h2><br />2. Download Docker Desktop for Windows</h2> <p>Visit the official <a href="https://www.docker.com/products/docker-desktop/" rel="nofollow" target="_blank">download page</a> of Docker Desktop to download Docker Desktop for Windows installer program. Or click <a href="https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe" rel="nofollow" target="_blank">this link</a> to download the installer directly.</p> <p>You’ll get the Docker Desktop Installer.exe file downloaded on to your computer (around 480 MB).</p> <h2><br />3. Installation Requirement</h2> <p>Docker is based on Linux technology so it can’t run natively on Windows. The Docker Desktop for Windows requires Hyper-V or WSL (Windows Subsystem for Linux) that provides virtualized Linux kernel. The latest version of Docker Desktop for Windows works best with WSL 2, so make sure your Windows 10 operating system meets the following requirement: Home or Pro 2004 (build 19041) or higher, or Enterprise or Education 1909 (build 18363) or higher.</p> <p>Open a new command prompt and you should see the version and build number of your Windows operating system:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/check_windows_version.png" alt="check windows version" /></p> <p>If you don’t meet the requirement mentioned above, you should upgrade your OS before installation of Docker Desktop for Windows.</p> <h2><br />4. Install Docker Desktop on Windows</h2> <p>Double click the Docker Desktop Installer.exe file to launch Docker Desktop installer. You will see the following dialog appears:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/install_Docker_configuration.png" alt="install Docker configuration" width="480" height="215" /></p> <p>By default, WSL 2 is checked. Click OK to proceed. Just wait for a while and you will end up this screen:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/docker_installation_succeeded.png" alt="docker installation succeeded" width="480" height="215" /></p> <p>Close this dialog. Then double click the Docker Desktop icon on desktop to launch Docker Desktop application. The following screen appears:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/install_docker_accept_terms.png" alt="install docker accept terms" width="600" height="450" /></p> <p>Check <i>I accept the terms</i>, and click Accept to proceed. Then you will see the massage Docker Desktop stopping… appears few times. Don’t worry, it’s because WSL has not been installed, as shown in the next dialog:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/wsl_2_installation_warning.png" alt="wsl 2 installation warning" width="600" height="220" /></p> <p>Click the link presented in the warning dialog (<a href="https://aka.ms/wsl2kernel" rel="nofollow" target="_blank">https://aka.ms/wsl2kernel</a>) to complete the installation of WSL 2. You’ll be directed to the step 4 of WSL 2 Manual Installation guide on Microsoft’s website:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/install_wsl_2_-_step_4.png" alt="install wsl 2 - step 4" width="800" height="521" /></p> <p>On this page, click the link <i>WSL2 Linux kernel update package for x64 machines</i>, you will be asked to download the wsl_update_x64.msi file. Download and run this file to launch Windows Subsystem for Linux Update Setup, as shown below:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/WSL2_update_setup.png" alt="WSL2 update setup" width="450" height="352" /></p> <p>Click Next to install. And when it’s done, return to the warning dialog, and click Restart:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/Restart_Docker_Desktop.png" alt="Restart Docker Desktop" width="600" height="220" /></p> <p>Wait for a while for Docker Desktop restarting. Finally, you should see the following screen:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/docker/install-docker-win/docker_desktop_started.png" alt="docker desktop started" width="700" height="526" /></p> <p>Congratulations! You have successfully installed Docker on your Windows operating system. It’s time to explore and have fun with containerized applications. To see the installation in action, watch the following video:</p> <p>https://www.youtube.com/watch?v=C3w59EHoxC0</p> <p>&nbsp;</p> <h3>Related Articles:</h3> <ul> <li><a href="https://codejava.net/docker/create-docker-image-for-spring-boot-app" rel="nofollow" target="_blank">Create Docker Image for Spring Boot Application</a></li> <li><a href="docker/install-docker-desktop-for-mac" target="_blank">Download and Install Docker Desktop for Mac</a></li> </ul></div>