Skip to main content

Creating your first Docker Image

Now that you are familiar with some docker commands and have pulled some images out of docker hub, it’s time to make your first image from scratch.

  1. Create a file named Docker > Open the file in terminal

  2. Once you are in the terminal execute mkdir newjava //creates a new file cd newjava //you are now in this file code . //opens your file in your prefered IDE

  3. Make a new java project named Test.java and write a hello world program in it. Run the program to see if it works fine

  4. Now starts the fun. Create a file named “Dockerfile” (exactly as it is written) and write the following code in it.

FROM openjdk WORKDIR /usr/src/myapp COPY . /usr/src/myapp/ RUN javac Test.java CMD [ "java", "Test" ]

It should look something like this.

  1. Now you can either use the integrated terminal to build your image using build command or use VS code GUI for the same. Left click on Dockerfile and build image. Name your image as myjavaimage and enter.

It should look something like this.

  1. Your image should start building (if not use the build command in your terminal and make sure you are in the required directory)-

docker build -t myjavaimage . //if GUI does not work

  1. Open your Rancher Desktop > Images and see if your image visible

  2. Now it’s time to run your image and containerize it. Open your terminal where you were in your “newjava” file and run the following command docker run --name <YourContainerName> <YourImageName>

  3. Go to Rancher Desktop > Containers. You will see your container that you have built.

  4. Congratulations! You have built and ran your first java program

Was this page helpful?