Docker Snippets
Image Management
Section titled “Image Management”# Create a docker setup with the current project# Go through guide based on project configurationdocker init
# Get an image, the latest versiondocker pull redis
# List images on localdocker images
# Compare imagesdocker scout compare ...
# Scan image for vulnerabilitiesdocker scout cves image-name:tag
# Build image, name with my-app and tag 1.0# Use Dockerfile in current directory .docker build -t my-app:1.0 .
# Tag an image with repository for pushed laterdocker tag my-app long-repository-name/my-app:1.0
# Push the imagedocker push long-repository-name/my-app:1.0
# Push an image to Docker Hub, assume logged in alreadydocker push myusername/myapp:latest
# Delete an imagedocker rmi image_id
# Create volumedocker volume create --name app-data1
# List volumesdocker volume ls
# See volume details, like file locationdocker inspect <volume-name>
# See history of, layers of an imagedocker history <image-name>
Container, Run Management, Debugging and Logs
Section titled “Container, Run Management, Debugging and Logs”# Run Container in attached mode with terminal output# Command can also pull image if not present# Ctrl + c to terminatedocker run redis
# Run container in detached modedocker run -d redis
# Run a specific image with environment variablesdocker run -e POSTGRES_PASSWORD=mysecretpassword postgres:14.7
# Run redis with -p to find host port 6000 to container port 6379# Host ports must be available and not bound alreadydocker run -p 6000:6379 redis
# Run detached mode with port mapping and with specified namedocker run -d -p 6000:6379 --name redis-old redis
# Run with a named volume -v and path of data in container, requires volume already createddocker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3
# Check running containers and manage themdocker ps# Start, stop containersdocker stop id_of_containerdocker start id_of_container
# List running or not running containersdocker ps -a
# Check logsdocker logs container_id# Check logs with name, names are randomizeddocker logs container_name
# Run commands in container with exec# -it - interactive terminal# /bin/bash - which shell to usedocker exec -it container_id /bin/bash# Use shell if bash not availabledocker exec -it container_id /bin/sh# Enter container as a user, 0 for rootdocker exec -u 0 -it container_id bash
# Delete a containerdocker rm container_id
# List networkdocker network ls
# Use docker compose to start containersdocker-compose -f mongo-docker-compose.yaml up# Notice default network is created# and logs of both containers are shown
# Stop composed containers, remove networkdocker-compose -f mongo-docker-compose.yaml down
# Run specific commands from different imagesdocker compose run
# Watch changes and reload changes per compose.yml settings# Good for code changes and testingdocker compose watch
# Scan directory for vulnerabilitiesdocker scout quickview fs://
Repositories
Section titled “Repositories”# Log into repositorydocker login ip_address:portdocker login 1.1.1.1:8083
# Log into Docker Hubdocker login