This guide helps you automate Java app deployment to Docker Hub.
Paste the following Dockerfile configuration:
FROM openjdk:23
WORKDIR /app
COPY src/ /app/
RUN javac *.java
CMD ["java", "HelloWorld"]
Login to Dockerhub
Click on your profile =>Account Settings=> Click Personal Access Token => Generate Token => Save the token on your computer.
Log in to Giithub= >Select your repository => Click on Action => New workflow/Search Simple Workflow => Configure => Commit change
Your repository should have .github/workflow/somefile.yml added to your repository
.github/workflows/docker.yml
Paste the following YAML configuration:
Create .github/workflows/docker.yml folder structure directly inside the repository using intellij
.github/workflows/docker.yml
Paste the following YAML configuration:
name: Build and Push to Docker Hub
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Build Docker Image
run: docker build -t ${{ secrets.DOCKER_USERNAME }}/is147:${{ github.run_number }} .
- name: Push Docker Image
run: docker push ${{ secrets.DOCKER_USERNAME }}/is147:${{ github.run_number }}
Now commit and push your changes to trigger the GitHub Action:
git add .
git commit -m "Automate Docker build and push"
git push origin main
OR
From Intellij git=> commit =>commit and push
Once the workflow completes, you can pull and run the image:
docker pull your-dockerhub-username/hello-world:tagname
docker images
docker run your-dockerhub-username/hello-world:tagname