top of page

Dockerizing Spring Boot Application - Important Docker Commands

Writer's picture: CODING Z2MCODING Z2M

Updated: May 7, 2023

What is Docker?

Open source containerization platform and it enables developers to package applications into containers—standardized executable components that combine application source code with all the operating system (OS) libraries and dependencies required to run the code in any environment. Docker Desktop

Building the Docker Image:


Step 1. Create a Dockerfile, and add the following Base Docker image for Java 17 in the docker file.

FROM openjdk:17-alpine

LABEL maintainer="codingz2m.com"

COPY target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar spring-cloud-config-server.jar

ENTRYPOINT ["java", "-jar", "/spring-cloud-config-server.jar"]


This Dockerfile consists of three commands:

  1. FROM openjdk:17-alpine: This command sets the base image for the Docker image to be built. In this case, it sets the base image as openjdk:17-alpine, which is an image of Alpine Linux with OpenJDK 17 installed.

  2. LABEL maintainer="codingz2m.com": This command adds a label to the Docker image. Labels provide metadata about the image, such as the name and email address of the person who maintains the image. In this case, the label specifies the maintainer of the image.

  3. COPY target/savings-account-0.0.2-SNAPSHOT.jar savings-account.jar: This command copies the application jar file named savings-account-0.0.2-SNAPSHOT.jar from the target directory to the root directory of the Docker image and renames it to savings-account.jar.

  4. ENTRYPOINT ["java", "-jar", "/savings-account.jar"]: This command specifies the command to run when the container starts. In this case, it runs the Java command to start the application jar file (savings-account.jar) using the java command and the -jar flag to specify the jar file to run. The jar file is located in the root directory of the container.


Step 2:

Run, maven clean & install to create jar file


Step 3:

Run the following command to buid docker image:

> docker build -t docker-repo-name/spring-cloud-config-server:1.0.0 .

NOTE: The 'dot' in the above command, indicates that 'Dockerfile' is in the root of your project.


Pushing Docker Image to Docker Hub:

Run the following commands in the command prompt (Make sure that have installed docker desktop on your machine)


> docker login

> docker images ( you will see the newly created docker image)

> docker push codingz2m/spring-cloud-config-server:1.0.0

> docker run -p 9091:9091 codingz2m/spring-cloud-config-server:1.0.0



Fixing the issue on Mac:

Cannot run program "docker-credential-desktop": error=2, No such file or directoryon Mac

On macOS this file is located in: /Users/YourUserName/.docker/config.json

{

"stackOrchestrator" : "swarm",

"credsStore" : "osxkeychain",

"experimental" : "disabled",

"auths" : {

}

}



Check the docker version:

> docker --version


Running the docker container Image:

> docker run -p 8080:8080 codingz2m/trending-movies-service:0.0.1-SNAPSHOT


Important Docker Concepts: Registry, Repository, Tag, Image and Container: Docker Registry - The Registry is a open source, stateless, highly scalable server side application (public repository) that stores and lets you distribute Docker images.


A registry can have multiple repositories. Example, https://hub.docker.com/repository/docker/codingz2m/trending-movies-service


A repository will store multiple versions (container images) of the specific application.

Container Image contains everything, you need to run the application.


When you run docker run command, container image will be downloaded from the registry to our machine and the local image will be created. Once it is downlaod, it will as an application in your machine. So, Container is a running verion of the Image.


Running the container in the background - Detaching the container from the terminal:

>docker run -p 8080:8080 -d codingz2m/trending-movies-service:0.0.1-SNAPSHOT

>docker logs <container id>


Checking the running containers: >docker container ls


Running another container from the same Image? >docker run -p 8081:8080 -d codingz2m/trending-movies-service:0.0.1-SNAPSHOT

>docker logs <container id>


Now, we are running two instances of the application or two container are running in the same Image. Let me check: >docker container ls


Accessing the application:

localhost:8080/<Path>


Showing the local docker images:

>docker images


Listing all the containers irrespective of their status: >docker container ls -a


Stopping the container: >docker container stop <container id>


Note: All the cloud providers provide number of services around docker. Azure provides a service called Azure Container Service, AWS provides a service called Elastic Container Service. So using the docker on the cloud is also easy.


Note: We can use docker toolbox, to remove the unwanted docker images. You can also start any number of containers for the single container image.


Working with Docker Images

> docker images


Giving multiple tags to single image

> docker tag codingz2m/trending-movies-service:0.0.1-SNAPSHOT codingz2m/trending-movies-service:latest


Pulling Docker Images ( Downloading image from registry to your local)

> docker pull mysql

Now you can see the mysql image when run this following command.

> docker images


Searching Docker official Images

> docker search <name of image>

ex: docker search mysql, docker search tomcat


Looking history of the image

> docker images

> docker image hsitory <image id> - Shows, certain steps involed in creating an image.


Inspecting Docker Images and check it out Repo Tags, Java Environment and so on.

> docker image inspect <image-id>


Removing the image from local machine ( not from docker registry)

> docker image remove <image id>


Note: You can also use either command prompt or docker tool box to remove the unwanted images. Kindly ensure that you have stopped the respective container gracefully and removed the stopped container as shown below.


Working with Docker Containers

Previously we created container using host, port, repository name followed by tag - Creating a container from this specific image.

>docker container run -p 8080:8080 codingz2m/trending-movies-service:0.0.1-SNAPSHOT


Inspecting Docker Container

> docker container ls -a

> docker container inspect <container-id>


Shutting Down Gracefully

> docker container stop <container-id>

Note: You can also use docker tool box to stop and delete the containers.


Removing all the stopped containers

>docker container prune


Starting the container with 'Restart' policy - container will be automatically re started when you restart your docker tool box. Run the 'docker container ls' command and check it out.

> docker run -p 8080:8080 --restart=always codingz2m/trending-movies-service:0.0.1-SNAPSHOT


Seeing the all the processes which are running in the specific container.

> docker top <container-id>


Displaying the 'stats' regrading the containers which are running.

>docker stats


Assigning CPU Limits & Memory usage: >docker container run -p 8080:8080 -m 512m --cpu-quota 5000 -d codingz2m/trending-movies-service:0.0.1-SNAPSHOT

>docker system df


Pausing the running container:

> docker container pause <container id>

> docker container unpause <container id>



61 views0 comments

Comments


Join us today and take the first step towards advancing your IT career!

More codingZ2M

Never miss an update

Thanks for submitting!

bottom of page