top of page
Newspapers

AUTHENTISE NEWS

Find all of Authentise's press releases, dev blogs and additive manufacturing thought pieces right here.

Docker: 4 useful tips, you may not know about

Updated: Apr 20, 2020


Tidy Docker environment


By adding --rm argument when starting interactive containers, Docker will delete the containers as soon as they are stopped. Unfortunately, --rm argument is not available for detached containers. To remove stopped detached containers, add this to your .bash_profile file:

clean_docker() {
  # $ docker help ps
  # -a, --all=false       Show all containers (default shows just running)
  # --before=             Show only container created before Id or Name
  # -f, --filter=[]       Filter output based on conditions provided
  # --format=             Pretty-print containers using a Go template
  # --help=false          Print usage
  # -l, --latest=false    Show the latest created container, include non-running
  # -n=-1                 Show n last created containers, include non-running
  # --no-trunc=false      Don't truncate output
  # -q, --quiet=false     Only display numeric IDs
  # -s, --size=false      Display total file sizes
  # --since=              Show created since Id or Name, include non-running

  # query all containers that have status = (exited|dead)
  CONTAINERS=$(docker ps \
    --all \
    --filter "status=exited" \
    --filter "status=dead" \
    --format="{{.ID}}")

  for CONTAINER in $CONTAINERS; do
    # get the container name and label
    NAME=$(docker ps \
      --all \
      --filter "id=$CONTAINER" \
      --format "{{.Names}} {{.Labels}}")

    read -p "You are about to delete $NAME Are you sure? (y/n)" -r
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      echo -n "Deleting $NAME..."

      docker rm $CONTAINER 2&>1 /dev/null

      echo " DELETED"
    fi
  done
}

Make sure to source your profile with. ~/.bash_profileand then you can execute: $ clean_docker


Small Docker images


The general rule of the thumb is to run just one process per container.


When installing packages, make sure to pass --no-install-recommends to apt-get to exclude everything that is not real dependency to the package.


Fast Ubuntu mirror


Add this to your Dockerfile before installing any packages to select Ubuntu mirror that is closest to your location:

RUN echo "deb mirror://mirrors.ubuntu.com/mirrors.txt trusty main restricted universe multiverse" > /etc/apt/sources.list && \
    echo "deb mirror://mirrors.ubuntu.com/mirrors.txt trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb mirror://mirrors.ubuntu.com/mirrors.txt trusty-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    DEBIAN_FRONTEND=noninteractive apt-get update

Docker build system


I keep my Dockerfiles on GitHub. Locally, I am storing Docker repositories under /repos. My nginx image can be found under/repos/yani-.nginx. I have this function in my.bash_profile file to build docker images for me:

docker_build() {
  local DIR=$(basename $(pwd))
  local USERNAME=${DIR%.*}
  local IMAGE=${DIR#*.}

  echo -n "Building $USERNAME/$IMAGE"
  LOG=$((docker build --tag $USERNAME/$IMAGE .) 2>&1)
  if [ $? -eq 0 ]; then
    echo " DONE"
  else
      echo " FAILED"
      echo "$LOG" | less
  fi
}

Here is how I build my nginx image:

$ cd /repos/yani-.nginx
$ docker_build
Building yani-/nginx DONE

Now, I can run a container:

$ docker run -it --rm yani-/nginx

Hope you find my tips useful. Do you have other tips you would like to share? Tweet to@Authentise. and we will post them here!

1,273 views0 comments

Recent Posts

See All

ES6 features to start with

Arrows => Arrows are a shorthand for function. Old way: users.forEach(function(user) { // do something with the user }); New way: users.forEach(user => { // do something with the user }) Impor

bottom of page