Skip to content

Instantly share code, notes, and snippets.

@helenhash
Last active November 6, 2024 05:18
Show Gist options
  • Save helenhash/22e9c3340193dc97c40c8a0b907a6521 to your computer and use it in GitHub Desktop.
Save helenhash/22e9c3340193dc97c40c8a0b907a6521 to your computer and use it in GitHub Desktop.

Docker Learning Note

Install

Docker Desktop WSL

https://docs.docker.com/get-docker/

Basic concepts

Docker

https://docs.docker.com/get-started/overview/

With Docker, you can manage your infrastructure in the same ways you manage your applications. shipping, testing, and deploying code quickly

Docker provides the ability to package and run an application in a loosely isolated environment called a container.

Image

An image is a read-only template with instructions for creating a Docker container.

Container image : package (all code, tools)

Often, an image is based on another image, with some additional customization.

To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it.

makes images so lightweight, small, and fast, when compared to other virtualization technologies.

Container

https://www.docker.com/resources/what-container/

Container: runtime - ex Docker Engine

Container >< vitual machine

A container is a runnable instance of an image.

A container is defined by its image as well as any configuration options you provide to it when you create or start it.

You can create, start, stop, move, or delete a container using the Docker API or CLI.

Docker registries

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.

Docker client

Desktop

Practice

Dockerfile

Pre (Optional): install docker plugin IDE

https://docs.docker.com/engine/reference/builder/

https://docs.docker.com/engine/reference/commandline/run/

Example:

FROM maven:3.6.3-jdk-11-openj9 AS build
USER root
WORKDIR /app
COPY . .
RUN mvn clean package

FROM adoptopenjdk/openjdk11
USER root
WORKDIR /app
COPY --from=build /app/target/simple-demo-0.0.1-SNAPSHOT.jar ./simple-demo.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "simple-demo.jar"]

.dockerignore file

If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY

Build

Build an image from a Dockerfile

docker build . -t <image name>

Run and map port

docker run -p 8080:8080 <image name>

You also use the -p flag to create a mapping between the host’s port 3000 to the container’s port 3000. Without the port mapping, you wouldn’t be able to access the application.

@helenhash
Copy link
Author

helenhash commented Mar 20, 2023

jib-maven-plugin

What

Jib is an open-source Java tool maintained by Google for building Docker images of Java applications. It simplifies containerization since with it, we don't need to write a dockerfile.

How

Config in pom file

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.8.0</version>
    <configuration>
      <from>
        <image>gcr.io/PROJECT/BASE_IMAGE</image>
      </from>
      <to>
        <image>gcr.io/PROJECT/IMAGE_NAME</image>
      </to>
    </configuration>
  </plugin>

Build and push the image to a container registry

mvn compile jib:build

https://techmaster.vn/posts/36412/dockerize-spring-boot-app-bang-google-jib-phan-2

https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/README.md

https://cloud.google.com/java/getting-started/jib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment