Skip to content

Instantly share code, notes, and snippets.

@Andreluizfc
Last active October 18, 2024 16:06
Show Gist options
  • Save Andreluizfc/e203e71286e81e01006754f9e3b8a9f8 to your computer and use it in GitHub Desktop.
Save Andreluizfc/e203e71286e81e01006754f9e3b8a9f8 to your computer and use it in GitHub Desktop.
Docker Commands

DOCKER Cheat Sheet

Build and run

docker build -t project/my-app .
docker run --name name-of-container -p 8080:8080 --detach project/my-app

Run with ENV VARS

docker run -p 8000:8000 \
    -e VAR_1="VALUE" \
    -e VAR_2='["value_1", "value_2"]' \
    -e CLIENT_SECRET_FILE="/app/client_credentials.json" \
    -e GOOGLE_APPLICATION_CREDENTIALS="/app/application_default_credentials.json" \
    -v /Users/andreluizfcastro/.config/gcloud/application_default_credentials.json:/app/application_default_credentials.json \
    fastapi-gunicorn-app

Run container in GCP console

docker run -it -e DEVSHELL_PROJECT_ID=$DEVSHELL_PROJECT_ID python:3.9 /bin/bash

List containers

docker ps
docker ps -a

Stop containers and remove:

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images -q)

Remove Volumes

docker volume rm $(docker volume ls -q)

Prune System and Cache

docker system prune -a
docker builder prune --all

Observe image bash:

docker exec -it [container_id] /bin/bash
docker exec -u root -it [container_id] /bin/bash

Inspect

You can examine a container's metadata in Docker by using Docker inspect:

docker inspect [container_id]

Docker Compose

Build with no cache

docker-compose build --no-cache

Build and up

docker-compose up -d --build

If cache stays in memory

docker-compose up --force-recreate

Where to find Volumes

Windows + WSL 2 (Ubuntu 18.04).

Type in the Windows file explorer :

For Docker version 20.10.+ : \\wsl$\docker-desktop-data\data\docker\volumes
For Docker Engine v19.03: \\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment