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…

Docker for Mac fix for max files, transparent hugepages warnings

To avoid Redis and other apps’ “max files limit” and “transparent hugepages” etc errors in Docker containers on Docker for Mac, you can set sysctls options in your docker-compose file by using compose format 2.1 or greater, which should be supported in docker-compose >= 1.10.

in docker-compose.yml file:

version: '2.1'
services:
  redis:
    image: redis:3.2-alpine
    command: redis-server /usr/local/etc/redis/redis.conf
    sysctls:
      - net.core.somaxconn=1024

To update transparent hugepages settings in Docker for Mac’s Alpine hypervisor host system, you can use this hack:

docker run --rm --privileged -ti alpine /bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled && echo never > /sys/kernel/mm/transparent_hugepage/defrag"

In theory you wouldn’t need/want to do this on a production host as you would configure the host properly. If you update Docker, you will need to re-run this.

Docker-in-docker Docker Compose with sshd

Dockerfile:

##
# Docker client with docker-compose && sshd
#
# use on a Docker host to allow you to ssh and access Docker and Compose remotely
# e.g., as part of CI/CD on a private network.
# ** Not for production use on publicly-exposed server **
#
# mount for docker host socket:
#    -v /var/run/docker.sock:/var/run/docker.sock:ro
# mount for docker-compose access (optional):
#    -v /host/compose/root:/opt/compose/alias
# cd or reference -f /alias/to/docker-compose.yml file when using docker-compose ...
#####
 
FROM docker:17
# (uses Alpine)
 
RUN apk add --update py-pip
RUN pip install docker-compose
 
# RUN apk add ca-certificates curl openssl nano
RUN apk add openssh
 
# use fresh keys: (could also do on startup)
RUN rm -rf /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_dsa_key
RUN /usr/bin/ssh-keygen -A
 
# install (or append) to authorized_keys: (optional)
COPY certs/my-deploy.pub /root/.ssh/authorized_keys
 
# cleanup install:
RUN rm  -rf /tmp/* /var/cache/apk/*
 
EXPOSE 22
 
# remove prior entrypoint if there is one:
ENTRYPOINT []
CMD ["/usr/sbin/sshd","-D"]

Override Docker Entrypoint properly

It’s not always so obvious what format the `ENTRYPOINT` override should take in a `docker run` command, so here is an example to clarify if you wanted to get a file list instead of the default entrypoint: first override the default ENTRYPOINT command with `/bin/ls` as a `run` argument then pass any args after the image name.

docker run --entrypoint "/bin/ls" namespace/imagename -al /container/path/tolist

As mentioned in the Docker docs, ENTRYPOINT only specifies the executable to run when the container starts.

« Previous Entries