Working with Docker Images¶
Push and pull Docker images to/from TUKE Harbor Repository.
-
Push
Upload images to repository.
-
Pull
Download images from repository.
-
Tagging
Proper version labeling.
-
CI/CD
Pipeline automation.
Basic Workflow¶
| Step | Command | Description |
|---|---|---|
| 1 | docker build |
Create image |
| 2 | docker tag |
Label for repository |
| 3 | docker login |
Login |
| 4 | docker push |
Upload to repository |
| 5 | docker pull |
Download from repository |
Creating a Docker Image¶
From Dockerfile¶
From running container¶
Tag and Push¶
Tag the image¶
Format
repository.cloud.tuke.sk/<project>/<image-name>:<tag>
| Parameter | Description | Example |
|---|---|---|
| project | Project name in Harbor | my-project |
| image-name | Docker image name | my-application |
| tag | Version or label | v1.0, latest, dev |
Log in¶
Push image¶
Successful upload
Pull and Run¶
Pull image¶
Run container¶
Advanced Usage¶
Multi-platform images¶
Build for multiple architectures (amd64, arm64):
# Create buildx builder
docker buildx create --name multiarch --use
# Build and push for multiple platforms
docker buildx build --platform linux/amd64,linux/arm64 \
-t repository.cloud.tuke.sk/my-project/my-application:v1.0 \
--push .
Multiple tags¶
# Tag image with multiple versions
docker tag my-application:v1.0 repository.cloud.tuke.sk/my-project/my-application:v1.0
docker tag my-application:v1.0 repository.cloud.tuke.sk/my-project/my-application:latest
# Push both versions
docker push repository.cloud.tuke.sk/my-project/my-application:v1.0
docker push repository.cloud.tuke.sk/my-project/my-application:latest
Multi-stage build (size optimization)¶
FROM golang:1.21 AS builder
WORKDIR /app
COPY . .
RUN go build -o app
FROM alpine:latest
COPY --from=builder /app/app /app
CMD ["/app"]
Docker Compose¶
docker-compose.yml¶
version: '3.8'
services:
app:
image: repository.cloud.tuke.sk/my-project/my-application:v1.0
ports:
- "8080:80"
restart: unless-stopped
Running¶
Kubernetes¶
Create docker-registry secret¶
kubectl create secret docker-registry tuke-registry \
--docker-server=repository.cloud.tuke.sk \
--docker-username=ab123cd \
--docker-password=your-password \
--docker-email=ab123cd@tuke.sk
Deployment YAML¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-application
spec:
replicas: 3
selector:
matchLabels:
app: my-application
template:
metadata:
labels:
app: my-application
spec:
imagePullSecrets:
- name: tuke-registry
containers:
- name: app
image: repository.cloud.tuke.sk/my-project/my-application:v1.0
ports:
- containerPort: 80
CI/CD Integration¶
GitLab CI/CD¶
# .gitlab-ci.yml
variables:
IMAGE_NAME: repository.cloud.tuke.sk/$CI_PROJECT_PATH:$CI_COMMIT_SHORT_SHA
build:
stage: build
image: docker:latest
services:
- docker:dind
before_script:
- docker login -u $HARBOR_USER -p $HARBOR_PASSWORD repository.cloud.tuke.sk
script:
- docker build -t $IMAGE_NAME .
- docker push $IMAGE_NAME
GitHub Actions¶
# .github/workflows/docker.yml
name: Build and Push Docker Image
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Login to TUKE Harbor
uses: docker/login-action@v2
with:
registry: repository.cloud.tuke.sk
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: repository.cloud.tuke.sk/my-project/my-application:${{ github.sha }}
Harbor Web UI¶
Viewing images¶
- Log in to Harbor web UI
- Go to your project
- Click Repositories
- Select repository and view all tags
Deleting an image¶
- In the repository list, select a tag
- Click Delete
- Confirm deletion
Warning
Deleted images cannot be recovered!
Command Reference¶
# Login
docker login repository.cloud.tuke.sk
# Tag image
docker tag <local-image> repository.cloud.tuke.sk/<project>/<image>:<tag>
# Push image
docker push repository.cloud.tuke.sk/<project>/<image>:<tag>
# Pull image
docker pull repository.cloud.tuke.sk/<project>/<image>:<tag>
# List local images
docker images | grep repository.cloud.tuke.sk
# Logout
docker logout repository.cloud.tuke.sk
Best Practices¶
Versioning
Instead of latest, use specific versions: v1.0.0, 2024.1, git commit SHA.
Multi-stage builds
Use multi-stage builds to minimize image size.
Automation
Integrate build and push into CI/CD pipeline.
Documentation
Add README to images using LABEL in Dockerfile.
Security
Never store sensitive data (passwords, API keys) directly in Docker images!