How to connect to Host from Docker Container?

Where there are many solutions as well as applications or a dedicated docker container which you can run, but all of these set up will require a lot of extra configuration. The solution I have found is really simple.


Idea is to find the docker host location IP address and send it as environment variable. For one system, the host IP address doesn’t change so it is restart proof. We generally use file or similar to send UID and GID along with this, you can add host IP as following:


export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v | awk '{ print $2 }' | cut -f2 -d: | head -n1)
export UID=${UID}
export GID=${GID}
docker-compose up -d


Now you can pass $DOCKERHOST as environment variable in docker-compose.yml file as folloing:


Sample output

➜ ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v | awk '{ print $2 }' | cut -f2 -d: | head -n1

Use Case

My personal observation is that when I use MySQL inside the docker, it is three times slower than when I have it in native machine. Using MySQL on host machine gives me a great performance as well as it has full support of security related layers integrated solutions developed specially for host machines which is still not available inside docker.

Worth Sharing?

Dockerfile for latest WordPress and Latest PHP

I am using Docker to run WordPress. Official image doesn’t contain imagick as well as tidy. These two are necessary extensions for cache plugins and image manipulations including resize and compression to work.

Follwing Dockerfile will have latest PHP as well as WordPress. As long as you are at PHP7.4.X as well as wordpress 5.4.X we are good to go.


FROM wordpress:5.4.2-php7.4-apache
# Better WordPress Health-Check
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
#RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick
RUN docker-php-ext-install bcmath exif
# W3Cache tweaks
RUN apt-get update && apt-get install -y tidy libtidy-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN a2enmod headers ext_filter 

RUN set -eux; \
	a2enmod rewrite expires; \
	a2enmod remoteip; \
	{ \
		echo 'RemoteIPHeader X-Forwarded-For'; \
# these IP ranges are reserved for "private" use and should thus *usually* be safe inside Docker
		echo 'RemoteIPTrustedProxy'; \
	} > /etc/apache2/conf-available/remoteip.conf; \
	a2enconf remoteip; \
# (replace all instances of "%h" with "%a" in LogFormat)
	find /etc/apache2 -type f -name '*.conf' -exec sed -ri 's/([[:space:]]*LogFormat[[:space:]]+"[^"]*)%h([^"]*")/\1%a\2/g' '{}' +

RUN docker-php-ext-install tidy

Worth Sharing?

How to have MySQL backup with Docker?

Dockerized Ruby Backup Gem

I created a dockerized image for ruby gem backup/backup It was unfortunately older ruby where it worked flawlessly. Image can be found here varunbatrait/backup

If you are coming from Ruby, backup is a popular ruby gem which can be used to backup the database. Unfortunately it is maintenance only but it works flawlessly with current features. Since, it is not compatible with latest ruby, it makes sense to make a docker image of it.


I am assuming that you have configured the models inside models folder where you are right now. Then you can perform a trigger like this. Config can be an empty ruby file if you have everything mentioned in your model.

docker run --rm -v $(pwd):/usr/src/app  --user $(id -u):$(id -g) varunbatrait/backup:latest perform -t database_name --config-file config.rb --root-path . 


As usual, you can use alias and invoke it with just backup by using following:

alias backup='docker run --rm -it -v $(pwd):/usr/src/app  --user $(id -u):$(id -g) varunbatrait/backup:latest'

Provided you have added above alias to your .zshrc or .bashrc, You can generate a model – you can generate using:

backup generate:model --trigger=demo --config-file config.rb 

This will generate model file demo.rb under models/demo.rb

# encoding: utf-8

# Backup Generated: demo
# Once configured, you can run the backup with the following command:
# $ backup perform -t demo [-c <path_to_configuration_file>]
# For more information about Backup's components, see the documentation at:
#, 'Description for demo') do


For information related to usage of backup. Please visit official documentation

Worth Sharing?