Sunday, May 20, 2018

How to run two container websites on a single port in Docker?


There is no out of  the box solution for this because it is not a valid design using dockers. However you can go about installing & configuring a reverse proxy which sits in front of your docker containers.

See this article for details on configuring ngnix as a reverse proxy for docker containers https://www.thepolyglotdeveloper.com/2017/03/nginx-reverse-proxy-containerized-docker-applications/

How to manage files & space in Docker


There are three main ways docker stores files:

  • By default, everything you save to disk inside the container is saved in the aufs layer. This doesn’t create problems if you clean up unused containers and images.
  • If you mount a file or directory from the host (using docker run -v /host/path:/container/path …) the files are stored in the host filesystem, so it’s easy to track them and there is no problem also.
  • The third way are docker volumes. Those are special paths that are mapped to a special directory in /var/lib/docker/volumes/ path on the host. A lot of images use volumes to share files between containers (using the volumes-from option) or persist data so you won’t lose them after the process exits (the data-only containers pattern).

Since there is no tool to list volumes and their state, it’s easy to leave them on disk even after all processes exited and all containers are removed.

You can use below script to cleanup all the things: 

#!/bin/bash


# remove exited containers:

docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v


# remove unused images:

docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi


# remove unused volumes:

find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(

docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'

) | xargs -r rm -fr