Skip to content

Instantly share code, notes, and snippets.

@YounesRahimi
Last active February 24, 2026 20:01
Show Gist options
  • Select an option

  • Save YounesRahimi/a0d20470be05773e0bd8adc490b91884 to your computer and use it in GitHub Desktop.

Select an option

Save YounesRahimi/a0d20470be05773e0bd8adc490b91884 to your computer and use it in GitHub Desktop.
Build & Publish a Docker Image with OpenJDK 17 + 7-Zip

Build & Publish a Docker Image with OpenJDK 17 + 7-Zip

1. Project Structure

my-java-app/
├── Dockerfile
├── pom.xml
└── src/

2. Dockerfile

# Base image
FROM openjdk:17.0.2-jdk-slim

# Metadata
LABEL maintainer="your-dockerhub-username"
LABEL description="OpenJDK 17 with 7-Zip installed"

# Install 7-Zip
# openjdk:17.0.2-jdk-slim is Debian-based, so we use apt
RUN apt-get update && \
    apt-get install -y --no-install-recommends p7zip-full && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Verify 7z is available
RUN 7z i

# Set working directory
WORKDIR /app

# Copy your jar (adjust the jar name to match yours)
# COPY target/my-app.jar app.jar

# Default command
CMD ["java", "-version"]

Remove the COPY comment and CMD line and replace with your own app entrypoint once you're ready to include your JAR.


3. Build the Image Locally

# Format: docker build -t <dockerhub-username>/<image-name>:<tag> <context-dir>
docker build -t johndoe/openjdk17-7zip:latest .

# Optionally tag a versioned release too
docker tag johndoe/openjdk17-7zip:latest johndoe/openjdk17-7zip:1.0.0

4. Login to Docker Hub

docker login
# Enter your Docker Hub username and password/token when prompted

# Recommended: use an Access Token instead of your password
# Generate one at: https://hub.docker.com/settings/security
docker login -u johndoe --password-stdin <<< "your_access_token"

5. Push to Docker Hub

# Push latest
docker push johndoe/openjdk17-7zip:latest

# Push versioned tag
docker push johndoe/openjdk17-7zip:1.0.0

Your image will be publicly available at: https://hub.docker.com/r/johndoe/openjdk17-7zip


6. Verify — Pull & Run From Anywhere

# Pull and run on any machine
docker pull johndoe/openjdk17-7zip:latest

# Test 7z is working inside the container
docker run --rm johndoe/openjdk17-7zip:latest 7z i

# Test Java is working
docker run --rm johndoe/openjdk17-7zip:latest java -version

# Mount a local folder and extract a RAR file
docker run --rm \
  -v "C:/temp:/data" \
  johndoe/openjdk17-7zip:latest \
  7z x /data/sample.rar -o/data/extracted -y

7. Full Flow Cheat Sheet

# 1. Build
docker build -t johndoe/openjdk17-7zip:latest .

# 2. Test locally
docker run --rm johndoe/openjdk17-7zip:latest 7z i

# 3. Login
docker login

# 4. Push
docker push johndoe/openjdk17-7zip:latest

# 5. Done — image is live on Docker Hub

Optional: Automate with GitHub Actions

If your Dockerfile lives in a GitHub repo, you can auto-build and push on every commit:

# .github/workflows/docker-publish.yml
name: Publish Docker Image

on:
  push:
    branches: [ "main" ]

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: |
            johndoe/openjdk17-7zip:latest
            johndoe/openjdk17-7zip:1.0.0

Add DOCKERHUB_USERNAME and DOCKERHUB_TOKEN in your GitHub repo under Settings → Secrets and variables → Actions.

# Base image
FROM openjdk:17.0.2-jdk-slim
# Metadata
LABEL maintainer="yooresh"
LABEL description="OpenJDK 17 with 7-Zip installed"
# Install 7-Zip
# openjdk:17.0.2-jdk-slim is Debian-based, so we use apt
RUN apt-get update && \
apt-get install -y --no-install-recommends p7zip-full && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Verify 7z is available
RUN 7z i
# Set working directory
RUN mkdir /opt/app
WORKDIR /opt/app
# Default command
CMD ["java", "-version"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment