Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Docker Tricks

Rodrigo Dlugokenski edited this page May 20, 2019 · 63 revisions

Base directory for mounting

mkdir -p ~/Projects/docker
cd ~/Projects/docker

What this is for?

This is for replacing some system components in my development machine. Since I use ArchLinux, that is a rolling release, sometimes I need to lock components to the app I developing, or use this machines as a radioactive components that I can just rm later. Examples:

  • Rails ES gem does not yet support Elasticsearch 6, so I just use an ES 5.6 docker instance
  • ArchLinux is behind versions in certain components (MariaDB 11 is not available, and I need to test some JSON goodies).
  • If everything goes wrong, just remove the fucking docker instance.
  • I safely trust PostgreSQL as host service, using the nice pacman packages, dont need docker for trustees :)

Why don't you use docker compose?

My dev environment changes a lot. It is easier to manage some pieces individually. If you do develop a single application at a large time span, you're better off creating composer files and running them.

Dockers with Proxy

Considering that $PWD is the base directory

Running Traefik as HTTP Proxy server

mkdir traefik
curl https://raw.githubusercontent.com/containous/traefik/master/traefik.sample.toml > traefik/traefik.toml
docker run -d \
  --name traefik \
  --publish 80:80 \
  --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
  --mount type=bind,source=$PWD/traefik/traefik.toml,target=/traefik.toml \
  --mount type=bind,source=$PWD/traefik/acme.json,target=/acme.json \
  --label traefik.port=8080 \
  --label traefik.frontend.rule=Host:monitor.localhost \
  traefik \
  --docker \
  --docker.domain=localhost \
  --docker.watch \
  --web

You can now use http://monitor.localhost to observe your requests.

Portainer as Docker GUI

mkdir portainer
docker run -d \
  --name portainer \
  --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
  --mount type=bind,src=$PWD/portainer,dst=/data \
  --label traefik.port=9000 \
  portainer/portainer \
  -H unix:///var/run/docker.sock

You can now use http://portainer.localhost to manage your Docker containers.

MySQL

mkdir mysql
docker run -d \
  --name mysql \
  -p 3306:3306 \
  -v $PWD/mysql:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD="MY ROOT PASSWORD" \
  --memory 384000000 \
  --restart unless-stopped \
  mysql:5.7

Memory: Limit memory usage in bytes

It exposes port 3306 to your host computer (and potentially to your network if your host is not firewalled). Now you can install a compatible mysql client in your host OS.

For ArchLinux: pacman -S percona-server-clients or pacman -S mariadb-clients

Ghost using MySQL above

Creating database and privileges

mysql -h 127.0.0.1 -uroot -p
CREATE USER 'ghost'@'%';
GRANT ALL PRIVILEGES ON ghost.* To 'ghost'@'%' IDENTIFIED BY 'ghost123';
FLUSH PRIVILEGES;

CTRL-D to disconnect

Creating the docker instance

mkdir ghost
docker run -d \
          --name ghost \
          --mount type=bind,src=$PWD/ghost,dst=/var/lib/ghost/content \
          --label traefik.port=2368 \
          --link mysql:mysql \
          -e database__client=mysql \
          -e database__connection__host=mysql \
          -e database__connection__user=ghost \
          -e database__connection__password=ghost123 \
          -e database__connection__database=ghost \
          -e url=http://ghost.localhost \
          --restart unless-stopped \
          ghost:alpine

First time setup will be available at http://ghost.localhost/ghost/ and the blog at http://ghost.localhost/

Mautic using MySQL above

mysql -h 127.0.0.1 -uroot -p
CREATE USER 'mautic'@'%';
GRANT ALL PRIVILEGES ON mautic.* To 'mautic'@'%' IDENTIFIED BY 'mautic123';
FLUSH PRIVILEGES;

docker run -d \
          --name mautic \
          --link mysql:mysql \
          -e MAUTIC_DB_USER=mautic \
          -e MAUTIC_DB_PASSWORD=mautic123 \
          --restart unless-stopped \
          --memory 536870900 \
          mautic/mautic:fpm

Elasticsearch 6 with memory restrictions

mkdir elastic
docker run --name elastic -d \
    -p 9200:9200 -p 9300:9300 \
    -e discovery.type=single-node \
    -e xpack.security.enabled=false \
    --mount type=bind,src=$PWD/elastic,dst=/usr/share/elasticsearch/data \
    -e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \
    --label traefik.port=9200 \
    --memory 536870900 \
    docker.elastic.co/elasticsearch/elasticsearch:6.4.0

TimescaleDB

mkdir -p timescale/data
docker run -d --name timescaledb \
    -p 5442:5432 \
    -e POSTGRES_PASSWORD=password \
    --mount type=bind,src=$PWD/timescale/data,dst=/var/lib/postgresql/data/ \
    timescale/timescaledb

Other useful dockers

Redis

docker run --name redis -d \
          -p 6379:6379 \
          --memory 384000000 \
          --restart unless-stopped \
          redis:alpine 

Kibana with proxy

docker run --name kibana \
    --link elastic:elasticsearch \
    -e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \
    --memory 536870900 \
    docker.elastic.co/kibana/kibana:6.4.0

Kibana external

docker run --name some-kibana -d \
   -e ELASTICSEARCH_URL=http://some-elasticsearch:9200 \ 
   docker.elastic.co/kibana/kibana:5.5.3

Dejavu (from appbase.io)

docker run -d \
    --link elastic5:elasticsearch \
    --label traefik.port=1358 \
    appbaseio/dejavu

Metabase

Using PSQL as metabase db, getting some data from our app mysql db

docker run -d \
    -p 3500:3000 \
    --link mysql:mysql \
    -e "JAVA_TIMEZONE=Brazil/East" \
    -e "MB_DB_TYPE=postgres" \
    -e "MB_DB_DBNAME=metabase" \
    -e "MB_DB_PORT=5432" \
    -e "MB_DB_USER=metabase" \
    -e "MB_DB_PASS=<password>" \
    -e "MB_DB_HOST=my-database-host" \
    --name metabase metabase/metabase

Apache Zeppelin

docker run -d \
   -p 8080:8080 \
   --mount type=bind,src=$PWD/zeppelin,dst=/opt/zeppelin/notebook \
   --name zeppelin xemuliam/zeppelin

Zendesk Maxwell

Testing: I think it must be run from EC2 to get correct IAM permissions and send msgs to Kinesis. From localhost use stdout as producer to test.

mkdir maxwell
docker run -it --rm \
   -v $PWD/maxwell:/root/.aws \
   --name maxwell \
   -e AWS_DEFAULT_REGION=us-east-1 \
   -e KINESIS_STREAM=Kinesis_Stream \
   -e MYSQL_USERNAME=MySQL_USER \
   -e MYSQL_PASSWORD=MySQL_PW \
   -e MYSQL_HOST=MySQL_Host \
   zendesk/maxwell \
   sh -c 'cp /app/kinesis-producer-library.properties.example /app/kinesis-producer-library.properties && echo "Region=$AWS_DEFAULT_REGION" >> /app/kinesis-producer-library.properties && bin/maxwell --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD --host=$MYSQL_HOST --producer=kinesis --kinesis_stream=$KINESIS_STREAM'

Testing locally

docker run -it --rm \
   -v $PWD/maxwell:/root/.aws \
   --name maxwell \
   -e MYSQL_USERNAME=root \
   -e MYSQL_PASSWORD=LOCALHOST_PW \
   -e MYSQL_HOST=127.0.0.1 \
   zendesk/maxwell \
   sh -c 'bin/maxwell --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD --host=$MYSQL_HOST --producer=stdout'

Dremio

docker run -d \
   -p 9047:9047 -p 31010:31010 -p 45678:45678 \
   --name dremio \
   --label traefik.port=9047 \
   --mount type=bind,src=$PWD/dremio/data,dst=/opt/dremio/data/ \
   dremio/dremio-oss

Apache Airflow

mkdir airflow
docker run -d \
  --name airflow \
  --mount type=bind,src=$PWD/dags,dst=/usr/local/airflow/dags \
  --label traefik.port=8080 \
  apache/airflow