Skip to main content
Docker containers bundle everything an application needs to run—and nothing more—making it simple to move workloads across environments. In this guide, you’ll learn how to:
  1. Define your app environment with a Dockerfile
  2. Build a container image
  3. Push the image to a registry (e.g., Docker Hub)
  4. Run the image locally

1. Writing a Dockerfile

Start by creating a Dockerfile in your project root. This example shows a Spring Boot application running on OpenJDK 8 (Alpine):
Key directives:
  • FROM sets the base image.
  • EXPOSE documents which port the app listens on.
  • ARG defines a build-time variable for your JAR.
  • ADD copies your artifact into the image.
  • ENTRYPOINT specifies the startup command.

2. Building the Docker Image

Run this command from the directory containing your Dockerfile. Replace <docker-hub-username> and <image-name> as needed:
  • -t tags your image in the format repository/name:tag.
Docker caches each layer by default. When you rebuild without changing earlier steps, subsequent builds are faster. To bypass the cache, add --no-cache.

3. Pushing to Docker Hub

First, authenticate using:
Then push your tagged image:
You must be logged in to Docker Hub before pushing images.
Use docker login and enter your credentials when prompted.

4. Running the Container Locally

Map port 9999 on your host to port 9999 in the container:
  • -d runs the container in detached mode.
  • -p host_port:container_port publishes container ports to the host.

Quick Reference: Docker CLI Commands


Watch Video