2024-03-20
Alpine Linux
To install a package: apk add <packagename>
, for example to install maven and openjdk17 we use the command apk add openjdk17 maven
Dockerize Spring Boot Application.
FROM debian:bookworm
RUN apt update -y && apt install default-jdk maven git -y
RUN git clone https://gitlab.com/vinay-keshava/spring-boot-hello-world.git
WORKDIR /spring-boot-hello-world
EXPOSE 9328
RUN mvn clean install -DskipTests
ENTRYPOINT ["java","-jar","target/demo-0.0.1.jar"]
Selecting debian:bookworm
as base image,installing jdk and maven packages, along with this exposing the port 9238
and building the project.
After successfull build of this docker image to create a container use the command docker run -dit -p 8080:9328 <imagename>
where 9328 port number is being binded to port 8080 on the host
FROM mysql:8.0.36-bookworm
ENV MYSQL_PASSWORD=root\
MYSQL_DATABASE=customersurvey\
MYSQL_ALLOW_EMPTY_PASSWORD=false\
MYSQL_ALLOW_RANDOME_PASSWORD=false
RUN apt update -y && apt install git maven default-jdk -y
RUN git clone https://gitlab.com/vinay-keshava/customer-survey.git
WORKDIR /customer-survey
EXPOSE 8088
RUN mvn clean install -DskipTests
ENTRYPOINT ["java","-jar","target/CustomerSurvey-0.0.1.jar"]
Fetches the mysql
image from the docker hub and setting ENV
environment variables, and installing maven
and default-jdk
packages and then changing the working directory to the project which is cloned from gitlab, running mvn install
command and upon starting the container running the jar file of the project
Docker Volumes
What if you want to store information outside the docker container, that can be used as shared storage for other containers. Docker volumes are mounted to filesystem paths onto the container, where the actual data is stored on the host.With the help of drivers data is written in different services.
Example to pull a nginx
docker image and syncing file and directories across containers and docker volume.
-
To create a volume:
docker volume create nginx-data
, where nginx-data is the volume name,where the volume is created at/var/lib/docker/volumes/nginx-data/_data
. -
docker inspect nginx-data
displays information at what date the volume was created,mountpoint,drivers,etc.
$ docker run -itd -v /var/lib/docker/volumes/nginx-data/_data/:/var/www/html nginx
what ever data in /var/www/html
inside the container is present is synced to the host nginx-data
.
To verify the working of docker volume, [inside the container] change to the directory /var/www/html
and create a sample file like hello_world.java
and [on the host] when the files are listed under /var/lib/docker/volumes/nginx-data/_data
hello_world.java shows, there by verifying the shared volume.
Docker Ports
To expose a particular ports we use the -p
flag and in Dockerfile we use the keyword EXPOSE <PORTNUMBER>
to expose a particular port
docker login
command to login to hub.docker.com to push container images.
GnuPG import keyword
To import the private GnuPG key we use gpg --import private.asc
and change the level of trust by editing the key and updating the trust level to ultimate
.