Docker: get hash from image name

Particularly if you are using a static image name like “latest” or “develop”, it is useful to be able to know and verify the exact version via the sha256 hash. To get the value you can use:

docker inspect --format='{{index .RepoDigests 0}}' repo/image-name:tag

To use this in a CI/CD workflow for Docker Swarm, you can do something like this:

SHA=$(docker inspect --format='{{index .RepoDigests 0}}' repo/image-name:tag)
docker service update --image $SHA --detach=true --with-registry-auth service-name

Remove Docker images with no tag

Sometimes, particularly on a development environment, you can end up with a lot of Docker images with no tags, which aren’t much practical use. To get rid of them you can do:

docker rmi $(docker images | grep none | awk '{ print $3 }')

Ones in use or with child images will not be deleted and result in output warning, but the rest will be removed so you can deal with the remaining ones as needed.

Docker for Mac clock drift fix

It’s a pain, not sure if there is a “real” fix, but if your Docker host and Mac host clocks drift (presumably the Mac is correct), you can run:

docker run --privileged --rm alpine date -s "$(date -u "+%Y-%m-%d %H:%M:%S")"

I’ve seen differences over 5 minutes, so worth checking…

Recover deleted file from Git

To recover an old file from git where you don’t know the commit, you can do:

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

Finds the last commit that affected the given path and checks out the file from there. Works on any file still in the history. If the file has not been staged or committed, simply use git checkout $file.

From: SO

Redis database size and key info

You can use INFO to get total memory size and a lot of other useful bits but if you just want to know the size of a single database or other info about database-specific keys, it can be a bit tricky.

see: https://stackoverflow.com/questions/7638542/redis-show-database-size-size-for-keys

tldr; in many cases, all you might need is:

redis-cli --bigkeys

To see what’s going on real-time:

redis-cli MONITOR

Yii2 fix “Malformed UTF-8 characters” error with master/slave db

You need to specify charset in slave config, e.g.,

'slaveConfig' => [
  'charset' => 'utf8',
  ...

« Previous Entries