18 Docker
18 Docker
18 Docker
Docker is a platform that allows you to automate the deployment, scaling, and
management of applications in lightweight containers. Containers are isolated
environments that package an application and all its dependencies, ensuring it runs
consistently across different computing environments.
Key Concepts
2. Setting Up Docker
1. Installation:
Download and install Docker Desktop from the official Docker website.
2. Basic Commands:
o docker --version: Check the Docker version installed.
o docker ps: List running containers.
o docker images: List downloaded images.
o docker build: Build an image from a Dockerfile.
o docker run: Run a container from an image.
1. Project Metadata:
o Project: Maven Project
o Language: Java
o Spring Boot: 3.0.0 (or latest stable version)
o Group: com.example
o Artifact: demo
o Name: demo
o Packaging: Jar
o Java: 17 (or your preferred version)
2. Dependencies:
o Spring Web
java
Copy code
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String hello() {
return "Hello, Docker!";
}
}
bash
Copy code
./mvnw clean package
In the root of your project directory, create a file named Dockerfile with the following
content:
dockerfile
Copy code
# Start with a base image containing Java runtime
FROM openjdk:17-jdk-alpine
bash
Copy code
docker build -t spring-boot-demo .
This command will create an image named spring-boot-demo based on the instructions
in your Dockerfile.
To run the application inside a Docker container, use the following command:
bash
Copy code
docker run -p 8080:8080 spring-boot-demo
This command will run the container, mapping the container's port 8080 to your local
machine's port 8080. You can access the application at http://localhost:8080.
bash
Copy code
docker ps
Stop a container:
bash
Copy code
docker stop <container_id>
Remove a container:
bash
Copy code
docker rm <container_id>
Remove an image:
bash
Copy code
docker rmi <image_id>
6. Next Steps
Now that you've containerized a simple Spring Boot application, you can explore more
advanced topics like:
Pom.xml plugin
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>gmantrig/bootapp1</repository>
<tag>11.8</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
</plugin>
</plugins>
====================================================
====================================================
====================================
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>docker.io</id>
<username>gmantrig</username>
<password>Perfect@12345</password>
</server>
</servers>
<pluginGroups>
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
</settings>
1. What is Docker?
Answer:
Docker is a platform that makes it easy to create, deploy, and run applications in
containers. Containers are lightweight and portable environments that include
everything needed to run an application, ensuring it works the same way regardless
of where it's deployed.
Answer:
A Docker container is a lightweight, standalone, and executable package that includes
an application and all its dependencies. Containers run the same regardless of the
environment, providing consistency across different development, testing, and
production environments.
Answer:
A Docker image is a read-only template that contains the instructions for creating a
Docker container. It includes everything needed to run a piece of software, such as
the code, runtime, libraries, and configurations.
4. What is a Dockerfile?
Answer:
A Dockerfile is a script containing a set of instructions to build a Docker image. It
specifies the base image, the application code, and any other dependencies required
for the application.
Answer:
Docker Hub is a cloud-based repository where Docker users and partners create, test,
store, and distribute container images. It is like a public library for Docker images,
where you can find and share images.
Answer:
Docker Container: Lightweight, shares the host system's kernel, starts quickly,
and uses less system resources.
Virtual Machine: Heavier, includes a full operating system, starts slowly, and
uses more system resources.
Answer:
You create a Docker container by running an image using the command:
bash
Copy code
docker run <image_name>
For example, to run a container from an image called nginx, you would use:
bash
Copy code
docker run nginx
Answer:
You can list all running Docker containers with the command:
bash
Copy code
docker ps
Answer:
You can stop a running Docker container using the command:
bash
Copy code
docker stop <container_id>
Answer:
You can delete a Docker container using the command:
bash
Copy code
docker rm <container_id>
Answer:
You can delete a Docker image using the command:
bash
Copy code
docker rmi <image_id>
Answer:
Docker Compose is a tool that allows you to define and run multi-container Docker
applications. You can define your application services, networks, and volumes in a
single docker-compose.yml file, making it easy to start and stop all services with a single
command.
Answer:
A Docker Volume is a storage mechanism for persisting data generated by and used
by Docker containers. Volumes are managed by Docker and can be used to share
data between containers or between a container and the host system.
Answer:
CMD: Specifies the default command to run when a container starts. It can be
overridden by specifying a command in docker run.
ENTRYPOINT: Specifies the command that will always run when the container
starts. You can still pass arguments to it.