- Define your app environment with a Dockerfile
- Build a container image
- Push the image to a registry (e.g., Docker Hub)
- Run the image locally
1. Writing a Dockerfile
Start by creating aDockerfile in your project root. This example shows a Spring Boot application running on OpenJDK 8 (Alpine):
FROMsets the base image.EXPOSEdocuments which port the app listens on.ARGdefines a build-time variable for your JAR.ADDcopies your artifact into the image.ENTRYPOINTspecifies the startup command.
2. Building the Docker Image
Run this command from the directory containing yourDockerfile. Replace <docker-hub-username> and <image-name> as needed:
-ttags your image in the formatrepository/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:You must be logged in to Docker Hub before pushing images.
Use
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:-druns the container in detached mode.-p host_port:container_portpublishes container ports to the host.
Quick Reference: Docker CLI Commands
| Command | Purpose | Example |
|---|---|---|
| docker build | Build an image from a Dockerfile | docker build -t user/app:latest . |
| docker push | Push an image to a registry | docker push user/app:latest |
| docker login | Authenticate with a Docker registry | docker login |
| docker run | Create and start a container from an image | docker run -d -p 8080:8080 user/app:latest |
| docker images | List all local images | docker images |
| docker ps | List running containers | docker ps |