{"id":41480,"date":"2016-11-04T15:00:52","date_gmt":"2016-11-04T13:00:52","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=41480"},"modified":"2019-03-29T14:15:48","modified_gmt":"2019-03-29T12:15:48","slug":"docker-hello-world-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/","title":{"rendered":"Docker Hello World Example"},"content":{"rendered":"<p>In this example we will explore different ways of running basic <em>&#8220;Hello, World&#8221;<\/em> containers in Docker.<\/p>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>Creating a Docker Hello World container and getting it to work verifies that you have the Docker infrastructure set up properly in your development system.&nbsp; We will go about this in 4 different ways:<\/p>\n<ol>\n<li>Run Docker hello world image provided by Docker<\/li>\n<li>Get a &#8220;Hello, world&#8221; printed from another basic Docker image<\/li>\n<li>Write a Simple &#8220;Hello, World&#8221; Program in Java and Run it Within a Docker Container<\/li>\n<li>Execute a &#8220;Hello, World&#8221; Program in Java Using Gradle and Docker<\/li>\n<\/ol>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#hello_world_image\">2. Run Docker Hello World image provided by Docker<\/a><\/dt>\n<dt><a href=\"#alpine_hello_world\">3. Get a &#8220;Hello, world&#8221; Printed from Another Basic Docker Image<\/a><\/dt>\n<dt><a href=\"#java_docker_hello_world\">4. Write a Simple &#8220;Hello, World&#8221; Program in Java and Run it Within a Docker Container<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#create_hello_world_java\">4.1. Create HelloWorld.java<\/a><\/dt>\n<dt><a href=\"#create_dockerfile\">4.2. Create a Dockerfile<\/a><\/dt>\n<dt><a href=\"#create_docker_image_and_run\">4.3. Create Docker Hello World Image and Run it<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#gradle_docker_hello_world\">5. Execute a &#8220;Hello, World&#8221; Program in Java Using Gradle and Docker<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#gradle_init\">5.1. Initialize a New Java Project and Create HelloWorld.java<\/a><\/dt>\n<dt><a href=\"#create_dockerfile_2\">5.2. Create a Dockerfile<\/a><\/dt>\n<dt><a href=\"#gradle_docker_plugin\">5.3. Include Gradle-Docker Plugin<\/a><\/dt>\n<dt><a href=\"#gradle_docker_tasks\">5.4. Configure the Docker Tasks in Gradle Build File<\/a><\/dt>\n<dt><a href=\"#create_docker_image_and_run_2\">5.5. Create Docker Container and Run it<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#summary\">6. Summary<\/a><\/dt>\n<dt><a href=\"#download\">7. Download<\/a><\/dt>\n<\/dl>\n<\/div>\n<p>You should have Docker installed already to follow the examples.&nbsp; Please go to the Docker home page <a href=\"https:\/\/docs.docker.com\/engine\/installation\/\">to install<\/a> the Docker engine before proceeding further, if needed.&nbsp; On Linux, please do ensure that your user name is added into the &#8220;docker&#8221; group while installing so that you need not invoke sudo to run the Docker client commands.&nbsp; You should also have Java and Gradle installed to try examples 3 and 4.&nbsp; Install your favorite J2SE distribution to get Java and install Gradle from <a href=\"https:\/\/gradle.org\/download\">gradle.org<\/a>.<\/p>\n<p>So let us get started!<\/p>\n<h2><a name=\"hello_world_image\"><\/a>2. Run Docker Hello World image provided by Docker<\/h2>\n<p>This is the simplest possible way to verify that you have Docker installed correctly.&nbsp; Just run the <a href=\"https:\/\/hub.docker.com\/_\/hello-world\/\">hello-world<\/a> Docker image in a terminal.<\/p>\n<pre class=\"brush:bash\"> $ docker run hello-world<\/pre>\n<p>This command will download the <code>hello-world<\/code> Docker image from the Dockerhub, if not present already, and run it.&nbsp; As a result, you should see the below output if it went well.<\/p>\n<p><figure id=\"attachment_41556\" aria-describedby=\"caption-attachment-41556\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-run-hello-world-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41556\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-run-hello-world-2.jpg\" alt=\"Run Docker Hello World Image\" width=\"860\" height=\"526\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-run-hello-world-2.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-run-hello-world-2-300x183.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-run-hello-world-2-768x470.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41556\" class=\"wp-caption-text\">Run Docker Hello-World Image<\/figcaption><\/figure><\/p>\n<h2><a name=\"alpine_hello_world\"><\/a>3. Get a &#8220;Hello, world&#8221; Printed from Another Basic Docker Image<\/h2>\n<p>This too is simple.&nbsp; Docker provides a few <a href=\"https:\/\/docs.docker.com\/engine\/reference\/glossary\/#base-image\">baseimages<\/a> that can be used directly to print a &#8220;Hello, World&#8221;.&nbsp; This can be done as shown below and it verifies that you have a successful installation of Docker.<\/p>\n<pre class=\"brush:bash\"> $ docker run alpine:latest \"echo\" \"Hello, World\"<\/pre>\n<p>This command downloads the <a href=\"https:\/\/hub.docker.com\/_\/alpine\/\">Alpine<\/a> baseimage the first time and creates a Docker container.&nbsp; It then runs the container and executes the echo command.&nbsp; The echo command echoes the &#8220;Hello, World&#8221; string.&nbsp; As a result, you should see the output as below.<\/p>\n<p><figure id=\"attachment_41557\" aria-describedby=\"caption-attachment-41557\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-run-alpine-echo-hello-world-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41557\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-run-alpine-echo-hello-world-2.jpg\" alt=\"Use the Alpine Docker Image to Print Hello World\" width=\"860\" height=\"472\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-run-alpine-echo-hello-world-2.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-run-alpine-echo-hello-world-2-300x165.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-run-alpine-echo-hello-world-2-768x422.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41557\" class=\"wp-caption-text\">Use the Alpine Docker Image to Print Hello World<\/figcaption><\/figure><\/p>\n<h2><a name=\"java_docker_hello_world\"><\/a>4. Write a Simple &#8220;Hello, World&#8221; Program in Java and Run it Within a Docker Container<\/h2>\n<p>Let us take it up a notch now.&nbsp; Let us write a simple hello world program in Java and execute it within a Docker container.<\/p>\n<h3><a name=\"create_hello_world_java\"><\/a>4.1. Create HelloWorld.java<\/h3>\n<p>First of all, let us create a simple Java program that prints &#8220;Hello, World&#8221;.&nbsp; Open up your favorite text editor and enter the following code:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">public class HelloWorld {\n    public static void main(String[] args) {\n        System.out.println(\"Hello, World\");\n    }\n}\n<\/pre>\n<p>This is a standard HelloWorld program in Java very closely resembling the one <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/getStarted\/application\/\">provided in the Official Java tutorial<\/a>.&nbsp; Save the file as <code>HelloWorld.java<\/code>.&nbsp; Now, compile this file using the Java compiler.<\/p>\n<pre class=\"brush:bash\">$ javac HelloWorld.java\n<\/pre>\n<p>This should create the class file <code>Helloworld.class.<\/code>&nbsp;Normally, we would use the java command to execute HelloWorld as below<\/p>\n<p><figure id=\"attachment_41558\" aria-describedby=\"caption-attachment-41558\" style=\"width: 563px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-java-hello-world-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41558\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-java-hello-world-2.jpg\" alt=\"Run HelloWorld.main() in Your Host OS\" width=\"563\" height=\"275\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-java-hello-world-2.jpg 563w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-java-hello-world-2-300x147.jpg 300w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/a><figcaption id=\"caption-attachment-41558\" class=\"wp-caption-text\">Run HelloWorld.main() in Your Native OS<\/figcaption><\/figure><\/p>\n<p>But we want to run this from within a Docker container.&nbsp; To accomplish that we need to create a Docker image.&nbsp; Let us do that now.<\/p>\n<h3><a name=\"create_dockerfile\"><\/a>4.2. Create a Dockerfile<\/h3>\n<p>To create a new Docker image we need to create a Dockerfile.&nbsp; A Dockerfile defines a docker image.&nbsp; Let us create this now.&nbsp; Create a new file named <code>Dockerfile<\/code> and enter the following in it and save it.<\/p>\n<pre class=\"brush:bash\">FROM alpine:latest\nADD HelloWorld.class HelloWorld.class\nRUN apk --update add openjdk8-jre\nENTRYPOINT [\"java\", \"-Djava.security.egd=file:\/dev\/.\/urandom\", \"HelloWorld\"]\n<\/pre>\n<p>The complete Dockerfile syntax can be found from <a href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/\">Docker docs<\/a> but here is briefly what we have done.&nbsp; We extend our image from the <a href=\"https:\/\/hub.docker.com\/_\/alpine\/\">Alpine baseimage<\/a>.&nbsp; We next added <code>HelloWorld.class<\/code> into the image with the same name.&nbsp; Later we installed a JRE environment using <a href=\"http:\/\/openjdk.java.net\/\">OpenJDK<\/a>.&nbsp; Finally, we gave the command to execute when this image is run &#8211; that is to run our HelloWorld in the JVM.<\/p>\n<h3><a name=\"create_docker_image_and_run\"><\/a>4.3. Create Docker Hello World Image and Run it<\/h3>\n<p>Now, build an image from this Dockerfile by executing the below command.<\/p>\n<pre class=\"brush:bash\"> $ docker build --tag \"docker-hello-world:latest\" .<\/pre>\n<p>As a result of this you should see the following output<\/p>\n<p><figure id=\"attachment_41560\" aria-describedby=\"caption-attachment-41560\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-build-docker-hello-world-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41560\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-build-docker-hello-world-2.jpg\" alt=\"Build Docker Image Called docker-hello-world\" width=\"860\" height=\"396\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-build-docker-hello-world-2.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-build-docker-hello-world-2-300x138.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-build-docker-hello-world-2-768x354.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41560\" class=\"wp-caption-text\">Build Docker Image Called docker-hello-world<\/figcaption><\/figure><\/p>\n<p>Finally, run the Docker image to see the <code>\"Hello, World\"<\/code> printed.<\/p>\n<pre class=\"brush:bash\"> $ docker run docker-hello-world:latest<\/pre>\n<p>As a result of this you should see the below output<\/p>\n<p><figure id=\"attachment_41562\" aria-describedby=\"caption-attachment-41562\" style=\"width: 611px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-run-docker-hello-world-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41562\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-run-docker-hello-world-2.jpg\" alt=\"Run the Docker Image docker hello world\" width=\"611\" height=\"275\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-run-docker-hello-world-2.jpg 611w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-run-docker-hello-world-2-300x135.jpg 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/a><figcaption id=\"caption-attachment-41562\" class=\"wp-caption-text\">Run the Docker Image docker-hello-world<\/figcaption><\/figure><\/p>\n<p>That&#8217;s it.&nbsp; This verifies that you can create your own custom Docker images too and run them.<\/p>\n<h2><a name=\"gradle_docker_hello_world\"><\/a>5. Execute a &#8220;Hello, World&#8221; Program in Java Using Gradle and Docker<\/h2>\n<p>Let us take it up another notch now.&nbsp; Real world programs are more complex than the ones shown above.&nbsp; So let us create another hello world Java program and run it in a Docker container using a few best-practices of the Java ecosystem.&nbsp; Furthermore, let us set up a Java project using Gradle, add the Docker plugin for Gradle into it, create a Docker image and run it using Gradle.<\/p>\n<h3><a name=\"gradle_init\"><\/a>5.1. Initialize a New Java Project and Create HelloWorld.java<\/h3>\n<p>First of all, Create a new folder called &#8220;docker-hello-world-example&#8221;, open a terminal and change into this folder.&nbsp; Next, use Gradle to initialize a new Java project.<\/p>\n<pre class=\"brush:bash\">$ gradle init --type java-library\n<\/pre>\n<p>Once done you should see the following folder structure created. You can freely delete the files <code>Library.java<\/code> and <code>LibraryTest.java<\/code>.&nbsp; We will not be needing them.<\/p>\n<p><figure id=\"attachment_41564\" aria-describedby=\"caption-attachment-41564\" style=\"width: 755px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-tree-gradle-init-java-project-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41564\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-tree-gradle-init-java-project-2.jpg\" alt=\"Folder structure for the Sample Java Project Created by Gradle\" width=\"755\" height=\"439\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-tree-gradle-init-java-project-2.jpg 755w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-tree-gradle-init-java-project-2-300x174.jpg 300w\" sizes=\"(max-width: 755px) 100vw, 755px\" \/><\/a><figcaption id=\"caption-attachment-41564\" class=\"wp-caption-text\">Folder structure for the Sample Java Project Created by Gradle<\/figcaption><\/figure><\/p>\n<p>Now, create a new file <code>src\/main\/java\/com\/javacodegeeks\/examples\/HelloWorld.java<\/code> and enter the following in it.&nbsp; Save it once done.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.examples;\n\npublic class HelloWorld {\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"Hello, World\");\n\t}\n}\n<\/pre>\n<h3><a name=\"create_dockerfile_2\"><\/a>5.2. Create a Dockerfile<\/h3>\n<p>Next, create a Docker file in src\/main\/resources called &#8220;src\/main\/resources\/Dockerfile&#8221; and enter the following.&nbsp; Save and close the file once done.[ulp id=&#8217;6PVIvOz3kDbYmNRn&#8217;]<\/p>\n<pre class=\"brush:bash\">FROM alpine:latest\nRUN apk --update add openjdk8-jre\nCOPY docker-hello-world-example.jar app.jar\nENTRYPOINT [\"java\", \"-Djava.security.egd=file:\/dev\/.\/urandom\", \"-jar\", \"app.jar\"]\n<\/pre>\n<p>Let us quickly go through what we did in the above Dockerfile.&nbsp; We derive our image from the Alpine Linux base image.&nbsp; Next, we installed J2SE from OpenJDK.&nbsp; Next, we copied the jar file generated by the build process as <code>app.jar<\/code> into the Docker image.&nbsp; Finally, we set the command to be run when the Docker container is run which is to execute the app.jar file.<\/p>\n<h3><a name=\"gradle_docker_plugin\"><\/a>5.3. Include Gradle-Docker Plugin<\/h3>\n<p>Next, make the following changes into the <code>build.gradle<\/code> file to update the Jar manifest.<\/p>\n<pre class=\"brush:java\">mainClassName = 'com.javacodegeeks.examples.HelloWorld'\n\njar {\n    manifest {\n        attributes(\n            'Main-Class': mainClassName\n        )\n    }\n}\n<\/pre>\n<p>We set the <code>mainClassName<\/code> to <code>com.javacodegeeks.examples.HelloWorld<\/code> and set the same attribute to be inserted into the manifest file of the jar that will be created.<\/p>\n<p>Next, insert the following changes at the very top of <code>build.gradle<\/code> file to configure and add the Gradle plugins for Docker.<\/p>\n<pre class=\"brush:java\">buildscript {\n    repositories {\n        maven {\n            url \"https:\/\/plugins.gradle.org\/m2\/\"\n        }\n    }\n    dependencies {\n        classpath \"gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.9.1\"\n    }\n}\n\nplugins {\n    id 'com.palantir.docker' version '0.9.1'\n    id 'com.palantir.docker-run' version '0.9.1'\n}\n\napply plugin: 'com.palantir.docker'\n<\/pre>\n<p>Here we basically added the Gradle plugins called <code>docker<\/code> and <code>docker-run<\/code> provided <a href=\"https:\/\/github.com\/palantir\/gradle-docker\">by Palantir<\/a>.&nbsp; These plugins enable us to create Docker containers and run them using Gradle.&nbsp; We added the URL to tell where to find the gradle-docker plugin (<code>https:\/\/plugins.gradle.org\/m2\/<\/code>).&nbsp; We also added the plugins into the classpath as build dependencies.<\/p>\n<h3><a name=\"gradle_docker_tasks\"><\/a>5.4. Configure the Docker Tasks in Gradle Build File<\/h3>\n<p>Next, insert the following changes at the bottom of build.gradle file to configure the details of the Docker image.<\/p>\n<pre class=\"brush:java\">docker {\n    name 'com.javacodegeeks.examples\/docker-hello-world:1'\n    tags 'latest'\n    dockerfile 'src\/main\/docker\/Dockerfile'\n    dependsOn tasks.jar\n}\n<\/pre>\n<p>Here we configured the details of the docker image.&nbsp; We assigned the image name <code>com.javacodegeeks.examples\/docker-hello-world<\/code> with version <code>1<\/code>.&nbsp; We assigned it the tag <code>latest<\/code> and gave the path where to find the Docker file.&nbsp; Finally, we made it depend on the Gradle task <code>jar<\/code> so that the output of the jar task will be fed to this task.<\/p>\n<p>Finally, insert the following changes at the bottom of build.gradle file to configure the details of the Docker container.<\/p>\n<pre class=\"brush:java\">dockerRun {\n    name 'docker-hello-world-container'\n    image 'com.javacodegeeks.examples\/docker-hello-world:1'\n    command 'java', '-Djava.security.egd=file:\/dev\/.\/urandom', '-jar', 'app.jar'\n}\n<\/pre>\n<p>Here we configured the details of the docker container.&nbsp; We assigned the container name &#8220;docker-hello-world-container&#8221;.&nbsp; We assigned it the image from where to create the container and gave it a command to execute when running the container.<\/p>\n<h3><a name=\"create_docker_image_and_run_2\"><\/a>5.5. Create Docker Container and Run it<\/h3>\n<p>Now we are ready to build and run this.&nbsp; Use Gradle to build and run this program<\/p>\n<pre class=\"brush:bash\">$ .\/gradlew clean build docker dockerRun<\/pre>\n<p>You will notice that the code was built, jar was created, Docker image and containers were created, and the container was run too. However, there is no &#8220;Hello, World&#8221; actually printed on the screen here.<\/p>\n<p><figure id=\"attachment_41567\" aria-describedby=\"caption-attachment-41567\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-gradle-clean-build-docker-dockerRun-2.jpg\"><img decoding=\"async\" class=\"wp-image-41567 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-gradle-clean-build-docker-dockerRun-2.jpg\" alt=\"Run the Gradle Tasks clean, build, docker, and dockerrun\" width=\"860\" height=\"665\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-gradle-clean-build-docker-dockerRun-2.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-gradle-clean-build-docker-dockerRun-2-300x232.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-gradle-clean-build-docker-dockerRun-2-768x594.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41567\" class=\"wp-caption-text\">Run the Gradle Tasks clean, build, docker, and dockerrun<\/figcaption><\/figure><\/p>\n<p>The last line simply says that the Docker container &#8216;docker-hello-world-container&#8217; is running.&nbsp; To see if it printed &#8220;Hello, World&#8221; successfully, we need to check the logs created by the Docker container. This can be checked by executing the following command.<\/p>\n<pre class=\"brush:bash\">$ docker logs docker-hello-world-container<\/pre>\n<p><figure id=\"attachment_41569\" aria-describedby=\"caption-attachment-41569\" style=\"width: 584px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-logs-docker-hello-world-container-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41569\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-logs-docker-hello-world-container-2.jpg\" alt=\"Check the Docker Container Logs to Check if Hello world was Printed\" width=\"584\" height=\"197\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-logs-docker-hello-world-container-2.jpg 584w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-logs-docker-hello-world-container-2-300x101.jpg 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><figcaption id=\"caption-attachment-41569\" class=\"wp-caption-text\">Check the Docker Container Logs to Check if Hello world was Printed<\/figcaption><\/figure><\/p>\n<p>There it is!<\/p>\n<h2><a name=\"summary\"><\/a>6. Summary<\/h2>\n<p>In this example we learned how to create Docker Hello world type containers in 4 different ways:<\/p>\n<ol>\n<li>First, we learned how to run the hello-world image provided by Docker.&nbsp; This printed Hello World as soon as it was run<\/li>\n<li>Next, we learned how to run the Alpine image and print a Hello World using the echo shell command<\/li>\n<li>Next, we learned how to get the basic Hello World program into a Docker image and run it to print Hello World.&nbsp; The source code for this can be downloaded from the links given below.<\/li>\n<li>Finally, we learned how to set up a structured Java project using Gradle and generate Docker images and Containers using Gradle plugins.&nbsp; We used this structure to create a Hello World Java project and got it to run in a container.&nbsp; Later, we verified the Docker logs for the container to see that Hello World was printed when the container was run.&nbsp; The source code for this also can be downloaded from the links given below.<\/li>\n<\/ol>\n<h2><a name=\"download\"><\/a>7. Download the Source Code<\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nThe source code for example 3 can be downloaded here: <strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/docker-hello-world.zip\">docker-hello-world.zip<\/a><\/strong><br \/>\nThe source code for example 4 can be downloaded here: <strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/docker-hello-world-example.zip\">docker-hello-world-example.zip<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we will explore different ways of running basic &#8220;Hello, World&#8221; containers in Docker. 1. Introduction Creating a Docker Hello World container and getting it to work verifies that you have the Docker infrastructure set up properly in your development system.&nbsp; We will go about this in 4 different ways: Run Docker hello &hellip;<\/p>\n","protected":false},"author":103,"featured_media":31013,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1354],"tags":[],"class_list":["post-41480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Docker Hello World Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Do you want to create basic Hello World containers using Docker? This post explores a few different ways to create Docker Hello World containers\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Docker Hello World Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Do you want to create basic Hello World containers using Docker? This post explores a few different ways to create Docker Hello World containers\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-04T13:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-29T12:15:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Hariharan Narayanan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Hariharan Narayanan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\"},\"author\":{\"name\":\"Hariharan Narayanan\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3\"},\"headline\":\"Docker Hello World Example\",\"datePublished\":\"2016-11-04T13:00:52+00:00\",\"dateModified\":\"2019-03-29T12:15:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\"},\"wordCount\":1714,\"commentCount\":28,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"articleSection\":[\"Docker\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\",\"name\":\"Docker Hello World Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"datePublished\":\"2016-11-04T13:00:52+00:00\",\"dateModified\":\"2019-03-29T12:15:48+00:00\",\"description\":\"Do you want to create basic Hello World containers using Docker? This post explores a few different ways to create Docker Hello World containers\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"DevOps\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/devops\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Docker\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/devops\/docker\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Docker Hello World Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3\",\"name\":\"Hariharan Narayanan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg\",\"caption\":\"Hariharan Narayanan\"},\"description\":\"Hari graduated from the School of Computer and Information Sciences in the University of Hyderabad. Over his career he has been involved in many complex projects in mobile applications, enterprise applications, distributed applications, micro-services, and other platforms and frameworks. He works as a consultant and is mainly involved with projects based on Java, C++ and Big Data technologies.\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/hariharan-narayanan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Docker Hello World Example - Java Code Geeks","description":"Do you want to create basic Hello World containers using Docker? This post explores a few different ways to create Docker Hello World containers","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/","og_locale":"en_US","og_type":"article","og_title":"Docker Hello World Example - Java Code Geeks","og_description":"Do you want to create basic Hello World containers using Docker? This post explores a few different ways to create Docker Hello World containers","og_url":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-11-04T13:00:52+00:00","article_modified_time":"2019-03-29T12:15:48+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","type":"image\/jpeg"}],"author":"Hariharan Narayanan","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Hariharan Narayanan","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/"},"author":{"name":"Hariharan Narayanan","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3"},"headline":"Docker Hello World Example","datePublished":"2016-11-04T13:00:52+00:00","dateModified":"2019-03-29T12:15:48+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/"},"wordCount":1714,"commentCount":28,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","articleSection":["Docker"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/","url":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/","name":"Docker Hello World Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","datePublished":"2016-11-04T13:00:52+00:00","dateModified":"2019-03-29T12:15:48+00:00","description":"Do you want to create basic Hello World containers using Docker? This post explores a few different ways to create Docker Hello World containers","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"DevOps","item":"https:\/\/examples.javacodegeeks.com\/category\/devops\/"},{"@type":"ListItem","position":3,"name":"Docker","item":"https:\/\/examples.javacodegeeks.com\/category\/devops\/docker\/"},{"@type":"ListItem","position":4,"name":"Docker Hello World Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3","name":"Hariharan Narayanan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg","caption":"Hariharan Narayanan"},"description":"Hari graduated from the School of Computer and Information Sciences in the University of Hyderabad. Over his career he has been involved in many complex projects in mobile applications, enterprise applications, distributed applications, micro-services, and other platforms and frameworks. He works as a consultant and is mainly involved with projects based on Java, C++ and Big Data technologies.","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/hariharan-narayanan\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/41480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=41480"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/41480\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/31013"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=41480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=41480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=41480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}