Skip to content

Instantly share code, notes, and snippets.

@ngstigator
Last active June 13, 2024 21:59
Show Gist options
  • Save ngstigator/4e55c5e0577aa478a64b847d42a95fbb to your computer and use it in GitHub Desktop.
Save ngstigator/4e55c5e0577aa478a64b847d42a95fbb to your computer and use it in GitHub Desktop.
Docker things

Docker

Housekeeping

List images grep filter

Example: list image name and ID

docker images |grep 224466 |awk '{ print ($1,$3) }'

Stop all containers

docker stop $(docker ps -a -q)

Remove all stopped containers

docker rm $(docker ps -a -q)

Remove all volumes (does not affect bind mounts)

docker volume rm $(docker volume ls -q)

Remove dangling images

docker rmi $(docker images -f dangling=true -q --no-trunc)

docker rmi -f $(docker images -f "dangling=true" -q)

Delete all containers and their volumes

docker rm -vf $(docker ps -a -q)

Remove all images

docker rmi -f $(docker images -a -q)

AWS ECR

Log into AWS ECR

AWSCLI V1

aws ecr get-login --no-include-email --profile portal-prod --region=ca-central-1

AWSCLI V2

aws ecr get-login-password --profile portal-prod --region=ca-central-1 | docker login --username AWS --password-stdin 763499268816.dkr.ecr.ca-central-1.amazonaws.com

Basic

Push image to AWS ECR

docker push <AWS_ECR_URI>:<TAG>

Serve le-blog from custom docroot

php docker.phar serve le/blog /media/Share/Projects/cmg/docker/le/blog/docroot -vvv

Run container using an image

docker run -it <image> sh

Run container regardless of image CMD

docker container run -it <image> /bin/bash

Build image

docker build -t <imagename> . --no-cache

Shell into container

docker exec -it <container> bash
docker attach <container>

Inspect specific keys

docker inspect -f '{{ json .Mounts }}' <containerName|containerId> | python -mjson.tool

Container IP address

docker network inspect -f \
'{{json .Containers}}' <DOCKER_NETWORK_LS_HASH> | \
jq '.[] | .Name + ":" + .IPv4Address' 

Gitlab

docker login registry.gitlab.com
docker build -t registry.gitlab.com/ngstigator73/<projectname>/<imagename>:<version> .
docker push registry.gitlab.com/ngstigator73/<projectname>/<imagename>:<version>

Docker Machine

switch env to remote machine

docker-machine create --driver generic --generic-ip-address=<ip_address> --generic-ssh-key ~/.ssh/<key_file> --generic-ssh-user <ssh-user> <docker_machine_name>

eval $(docker-machine env <docker_machine_name>)

unset env from remote

eval $(docker-machine env -u)

Docker Desktop

Windows

Powershell force quit docker desktop

$isrunning = Get-Process "Docker Desktop" -ErrorAction SilentlyContinue
if ($isrunning) {
  $isrunning.CloseMainWindow()
  $isrunning | Stop-Process -Force
}

Linux

Install Docker Compose plugin

sudo apt-get install docker-compose-plugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment