---
title: "OpenShift CLI (oc) Cheat Sheet"
canonical: "https://kb.uconn.edu/space/IKB/28728688710/OpenShift%20CLI%20(oc)%20Cheat%20Sheet"
format: markdown
---
A quick reference for the most common `oc` commands. Bookmark this page for daily use.

---

## Login & Context

```shell
# Log in (get token from web console → User Menu → Copy Login Command)
oc login --token=<token> --server=https://api.opp.its.uconn.edu:6443

# Check current user
oc whoami

# Check current project
oc project

# Switch to a different project
oc project my-project

# List all projects you have access to
oc projects
```

---

## Viewing Resources

```shell
# List pods
oc get pods

# List all resources in current project
oc get all

# List deployments
oc get deployments

# List services
oc get services

# List routes
oc get routes

# List persistent volume claims
oc get pvc

# List configmaps and secrets
oc get configmaps
oc get secrets

# Get detailed info about a resource
oc describe pod <pod-name>
oc describe deployment <name>
oc describe route <name>

# Output as YAML (useful for debugging)
oc get deployment my-app -o yaml
```

---

## Creating & Deploying Apps

```shell
# Deploy from a container image
oc new-app --name=my-app --image=registry.redhat.io/rhel8/httpd-24

# Deploy from source code (S2I)
oc new-app python:3.11~https://github.com/org/repo.git --name=my-app

# Deploy from Dockerfile in a repo
oc new-app https://github.com/org/repo.git --strategy=docker --name=my-app

# Expose an app externally
oc expose service/my-app

# Get the public URL
oc get route my-app
```

---

## Logs & Debugging

```shell
# View pod logs
oc logs <pod-name>

# Stream logs in real-time
oc logs -f <pod-name>

# View logs from a crashed pod
oc logs <pod-name> --previous

# View build logs
oc logs -f buildconfig/my-app

# Open a shell inside a pod
oc rsh <pod-name>

# Run a one-off command in a pod
oc exec <pod-name> -- ls /app

# View recent events
oc get events --sort-by='.lastTimestamp'

# Check resource usage
oc adm top pods
```

---

## Configuration

```shell
# Set environment variables
oc set env deployment/my-app KEY=value KEY2=value2

# View environment variables
oc set env deployment/my-app --list

# Remove an environment variable
oc set env deployment/my-app KEY-

# Create a ConfigMap
oc create configmap app-config --from-literal=MODE=production

# Create a Secret
oc create secret generic db-creds --from-literal=PASSWORD=s3cur3

# Inject ConfigMap/Secret as env vars
oc set env deployment/my-app --from=configmap/app-config
oc set env deployment/my-app --from=secret/db-creds
```

---

## Scaling & Updates

```shell
# Scale to N replicas
oc scale deployment/my-app --replicas=3

# Autoscale (min 2, max 10, target 80% CPU)
oc autoscale deployment/my-app --min=2 --max=10 --cpu-percent=80

# Update the container image
oc set image deployment/my-app my-app=my-registry/my-app:v2.0

# Roll back to previous version
oc rollout undo deployment/my-app

# Check rollout status
oc rollout status deployment/my-app

# View rollout history
oc rollout history deployment/my-app

# Restart pods (pick up config changes)
oc rollout restart deployment/my-app
```

---

## Storage

```shell
# Add persistent storage to a deployment
oc set volume deployment/my-app \
  --add --name=data \
  --type=persistentVolumeClaim \
  --claim-name=my-data-pvc \
  --mount-path=/app/data

# View PVCs
oc get pvc

# View storage usage
oc describe pvc my-data-pvc
```

---

## Resource Limits & Health Checks

```shell
# Set CPU and memory limits
oc set resources deployment/my-app \
  --requests=cpu=100m,memory=256Mi \
  --limits=cpu=500m,memory=512Mi

# Add a readiness probe
oc set probe deployment/my-app --readiness \
  --get-url=http://:8080/health \
  --initial-delay-seconds=5

# Add a liveness probe
oc set probe deployment/my-app --liveness \
  --get-url=http://:8080/health \
  --initial-delay-seconds=15
```

---

## Builds

```shell
# Start a new build
oc start-build my-app

# Build from local source
oc start-build my-app --from-dir=.

# View builds
oc get builds

# View build logs
oc logs build/my-app-3

# Cancel a build
oc cancel-build my-app-3
```

---

## Cleanup

```shell
# Delete a specific resource
oc delete pod <pod-name>
oc delete deployment my-app
oc delete route my-app
oc delete service my-app

# Delete all resources for an app
oc delete all -l app=my-app

# Force delete a stuck pod
oc delete pod <pod-name> --force --grace-period=0
```

---

## Permissions

```shell
# Check what you can do
oc auth can-i --list

# Check if you can create pods
oc auth can-i create pods

# View your roles
oc get rolebindings
```

---

## Quick Reference Table

| Task | Command |
| --- | --- |
| See running pods | `oc get pods` |
| View logs | `oc logs <pod>` |
| Shell into pod | `oc rsh <pod>` |
| Deploy an image | `oc new-app --image=<url> --name=<name>` |
| Expose externally | `oc expose svc/<name>` |
| Scale up | `oc scale deployment/<name> --replicas=N` |
| Set env var | `oc set env deployment/<name> KEY=val` |
| Restart pods | `oc rollout restart deployment/<name>` |
| Roll back | `oc rollout undo deployment/<name>` |
| Delete everything | `oc delete all -l app=<name>` |

---

*UConn ITS Infrastructure Services — OpenShift Knowledge Base*