SPRING BOOT
DOCKER
       BASICS
     ROHAN THAPA
THAPAROHAN2019@GMAIL.COM
          What is a Docker?
Docker is a platform for developing, shipping,
and running applications inside containers.
Containers are isolated environments that
encapsulate everything an application needs
to run, including the code, runtime, libraries,
and system tools, ensuring that the
application runs consistently across different
computing environments.
   Containerization isolates applications,
   avoiding conflicts between libraries and
   dependencies.
   Portability is one of Docker's primary
   advantages, meaning you can "build once,
   run anywhere."
          Why Docker?
Consistency       Across     Environments:
Applications in Docker containers will run
the same regardless of the environment
(development, testing, production).
Efficiency: Containers use fewer resources
than virtual machines because they don’t
bundle a full operating system.
Fast Deployment: Containers can be
quickly created, deployed, and scaled,
facilitating continuous integration and
delivery (CI/CD).
Isolation: Each Docker container runs in
isolation     from     others,     avoiding
dependency conflicts.
Scalability: Containers can be easily scaled
across clusters of machines.
      Key Docker Concepts
Docker Containers
   Containers are lightweight, stand-alone,
   executable packages of software that
   include everything needed to run a piece
   of software: code, runtime, system tools,
   libraries, and settings.
   Difference from VMs: Containers share the
   same OS kernel, unlike VMs, which
   virtualize    entire     hardware,  making
   containers faster and more lightweight.
      Key Docker Concepts
Docker Images
   Images are the blueprints of containers.
   They include the application and its
   dependencies but are immutable and read-
   only. Containers are instances of images.
   Layers: Images are built in layers, with each
   command in a Dockerfile creating a new
   layer. This layering allows for efficient
   storage and reuse of common layers.
      Key Docker Concepts
Dockerfile
   A Dockerfile is a script containing a set of
   instructions that Docker uses to create a
   container image. Each instruction in a
   Dockerfile creates a new layer in the
   image.
Common Dockerfile Instruction
1. FROM :
Specifies the base image for the Docker image.
2.LABEL
Adds metadata to an image in the form of key-value
pairs.
3.WORKDIR
Sets the working directory inside the container for
subsequent commands.
4.COPY
Copies files or directories from the host filesystem to
the container.
Common Dockerfile Instruction
5.ADD
Similar to COPY, but also supports extracting tar files
and downloading URLs.
6.RUN
Executes commands inside the container during the
build process.
7.CMD
Specifies the default command to run when a
container starts (overridden by docker run
command).
8.ENTRYPOINT
Configures the container to run as an executable
with a command that can't be overridden.
Common Dockerfile Instruction
9.ENV
Sets environment variables inside the container.
10.ARG
Defines build-time variables (accessible only during
the build process).
11.EXPOSE
Informs Docker that the container listens on specific
network ports.
12.VOLUME
Defines a mount point with a specific path in the
container to persist data.
     Key Docker Concepts
Docker Hub
  Docker Hub is a cloud-based repository
  where Docker users can push and pull
  container images. It’s the default registry
  used by Docker, offering both official
  images and user-contributed images.
Docker Volumes
  Volumes are used to persist data
  generated by a container. Volumes are
  stored outside the container filesystem, so
  they aren't tied to the lifecycle of a
  container.
      Key Docker Concepts
Docker Networks
   Docker allows containers to communicate
   with each other using different networking
   modes, including:
      Bridge: Default, allows containers to
      communicate on the same host.
      Host: Shares the host’s network stack.
      Overlay: Enables swarm services to
      communicate across multiple Docker
      daemons.
       Docker Architecture
Docker has a client-server architecture:
   Docker Client: The primary user interface
   for Docker. Users interact with Docker by
   issuing commands using the Docker CLI.
   Docker Daemon: Responsible for building,
   running, and managing containers.
   Docker Registry: A place where Docker
   images are stored. Docker Hub is a public
   registry.
   Docker Objects: Images, containers,
   networks, and volumes are the objects
   Docker uses.
    Core Docker Operations
Basic Docker Commands
  docker pull: Fetches an image from a
  registry.
  docker build: Builds an image from a
  Dockerfile.
  docker run: Runs a container from an
  image.
  docker ps: Lists running containers.
  docker stop: Stops a running container.
  docker rm: Removes a stopped container.
  docker exec: Runs a command inside a
  running container
    Basic Docker Commands
check version
pull image from dockerhub
list images
    Basic Docker Commands
search images
run the image
    Basic Docker Commands
view container and stop container
remove the container
    Basic Docker Commands
remove the image
    Basic Docker Commands
docker login :
Logs into a Docker registry (usually Docker
Hub). You will be prompted to enter your
credentials.
docker logout : Logs out from a Docker
registry.
    Core Docker Operations
Managing Containers
  docker logs: Retrieves logs from a
  container.
  docker stats: Shows resource usage
  statistics for containers.
Docker Compose
   Docker Compose is used to define and
   manage multi-container applications. You
   create a docker-compose.yml file, which
   defines services, networks, and volumes.
  Advanced Docker Concepts
Docker Swarm
   Docker    Swarm     is  Docker's      native
   clustering and orchestration tool. It allows
   Docker hosts to group together into a
   cluster, where containers can be scaled
   and managed across multiple machines.
Kubernetes
   Kubernetes is an open-source container
   orchestration platform, which is often
   used with Docker to automate the
   deployment, scaling, and management of
   containerized applications across clusters
   of machines.
  Advanced Docker Concepts
Docker in CI/CD
   Docker plays a crucial role in Continuous
   Integration and Continuous Delivery
   pipelines. It allows teams to run automated
   tests, build images, and deploy the same
   container to any environment (dev,
   staging, production), ensuring consistency.
           Docker Security
Docker offers several layers of security:
   Namespaces: Provide isolation for a
   container’s processes, users, file system,
   and network.
   Control Groups (Cgroups): Limit resources
   a container can consume (CPU, memory,
   etc.).
   Seccomp Profiles: Limit system calls that
   containers are allowed to make, reducing
   potential attack vectors.
                Example:
Here’s a basic example of how to containerize
a Spring Boot application and run it with
Docker:
1. Create a Spring Boot Application
                Example:
2. Create a Dockerfile
Next, we need to create a Dockerfile. This file
contains the instructions Docker uses to
create the container.
In the root directory of your Spring Boot
application (where pom.xml or build.gradle is
located), create a file called Dockerfile:
                Example:
Explanation:
   Firstly we add a finalName tag in pom.xml
   to set the fix name for .jar file.
   Then we compile the Spring             Boot
   application and create a .jar file.
   Then we use a lightweight OpenJDK image
   to run the .jar file. The resulting container
   will run on Java 22, and expose port 9898
                Example:
3. Build the Docker Image
After creating the Dockerfile, you can now
build the Docker image.
Open a terminal in the root directory of your
Spring Boot application and run the following
command:
This command builds the Docker image from
the Dockerfile and tags it as springbootimage.
               Example:
4. Run the Docker Container
Now that we have the Docker image, we can
run it in a container. Run the following
command:
-p 9898:8080: This maps the container’s port
8080 to your local machine's port 9898.
               Example:
5. Test the Application
Once the container is running, open your
browser and go to http://localhost:9898/api.
You should see the message:
Docker vs Virtual Machines
Benefits of Dockerizing a Spring Boot
             Application
  Consistency: The application will behave
  the same in any environment because it
  includes all dependencies.
  Scalability: Docker containers can be
  easily replicated and deployed across
  multiple hosts.
  Portability: You can ship the application as
  a single image that can run anywhere
  Docker is installed.
  Isolation: Each Docker container runs in its
  own environment without affecting the
  host system or other containers.
              Conclusion
Docker has transformed the way we develop,
test, and deploy software. It provides a
lightweight,    scalable      solution     for
containerizing     applications,      ensuring
consistency across environments.
Whether you’re using Docker for local
development, CI/CD, or managing production
workloads, Docker’s flexibility and speed make
it an indispensable tool for modern software
development.
With Docker, the phrase "It works on my
machine!" becomes irrelevant — it works
everywhere.
Thank You
        Rohan Thapa
 thaparohan2019@gmail.com