Docker for everything


I just love Docker. 😍

Not the company, but the underlying technology and what I can do with it, especially when you want to test a project you found on GitHub on your machine but want to avoid installing all the required dependencies, especially pip packages that suggest you should install them globally.

Do you want to test something that requires a PostgreSQL database but don’t want to install it? Want to do some data science and try the ELK stack but don’t have a Kubernetes cluster handy? Or maybe you just want to start a few services on your internal server and have them exposed to your team in a few minutes? Well, containers are here to solve all of this!

Here is a little cheat sheet.

docker images -a # list images
docker ps -a --size # process list
docker system df --verbose # space usage
docker system prune -a # cleanup

Debug mode

docker run -it --rm \
-v {host_path}:{container_path} \ # Volume mount.
-e {key}:{value} \ # Environement variable.
-p {host_ip?}:{host_port}:{container_port} \ # Publish a port.
--name {name} \ # Container name.
{image}:{tag?} {cmd?} {args?} # Image name, tag, command and arguments.

-it: Interactive Terminal.
--rm: Automatically remove the container when it exits.

Detach mode (daemon)

docker run -d \
-v {host_path}:{container_path} \ # Volume mount.
-e {key}:{value} \ # Environement variable.
-p {host_ip?}:{host_port}:{container_port} \ # Publish a port.
--restart always \ # Restart policy.
--name {name} \ # Container name.
{image}:{tag?} {cmd?} {args?} # Image name, tag, command and arguments.

-d: Background mode (detach).
--restart: Restart policy to apply when a container exits.

Here is a few docker scripts I made to get started, feel free to use them.

ZNC

# mkdir /opt/znc

docker kill znc
docker rm znc

docker run -d \
-v /opt/znc:/znc-data \
-p 6666:6666 -p 6668:6668 \
--restart always \
--name znc \
znc

Tor

docker run -d \
-p 127.0.0.1:9050:9050 \
-p 127.0.0.1:9051:9051 \
--restart always \
--name tor \
osminogin/tor-simple

MariaDB

docker run -it --rm \
-v "$PWD"/data:/var/lib/mysql \
-v "$PWD"/socket:/var/run/mysqld \
-e MYSQL_ROOT_PASSWORD=12345 \
-e MYSQL_USER=www \
-e MYSQL_PASSWORD=12345 \
-e MYSQL_DATABASE=testdb \
-e TZ=America/Montreal \
-p 3306:3306 \
--name mariadb102 \
mariadb:10.2

MongoDB

docker run -it --rm \
-v "$PWD"/data:/data/db \
-v "$PWD"/socket:/tmp \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
-e MONGO_INITDB_DATABASE=dbname \
-e TZ=America/Montreal \
-p 27017:27017 \
--name mongo \
mongo

PostgreSQL

docker run -it --rm \
-v "$PWD"/data:/var/lib/postgresql/data \
-v "$PWD"/socket:/var/run/postgresql \
-e POSTGRES_USER=www \
-e POSTGRES_PASSWORD=12345 \
-e POSTGRES_DB=test \
-e TZ=America/Montreal \
-p 5432:5432 \
--name postgres \
postgres

Redis

docker run -it --rm \
-v "$PWD"/data:/data \
-e TZ=America/Montreal \
-p 6379:6379 \
--name redis \
redis redis-server --appendonly yes

Memcached

docker run -it --rm \
-e TZ=America/Montreal \
-p 11211:11211 \
--name memcached \
memcached

Elasticsearch

docker run -it --rm \
-v "$PWD"/data:/usr/share/elasticsearch/data \
-e discovery.type=single-node \
-e TZ=America/Montreal \
-p 9200:9200 \
-p 9300:9300 \
--name elasticsearch \
elasticsearch:7.5.2

http://127.0.0.1:9200

Kibana

docker run -it --rm \
-e SERVER_NAME=kibana.local.e-alpha.ca \
-e ELASTICSEARCH_HOSTS=http://docker.for.mac.localhost:9200 \
-e TZ=America/Montreal \
-p 5601:5601 \
--name kibana \
kibana:7.5.2

http://127.0.0.1:5601

YouTube-DL

Here is an example of an alias you can place on your bash/zsh profile.

alias yt-dl='docker run --rm -i -e PGID=$(id -g) -e PUID=$(id -u) -v "$(pwd)":/workdir:rw mikenye/youtube-dl'

Example usage:

yt-dl "https://www.youtube.com/watch?v=jNQXAC9IVRw"