Last active
December 23, 2020 02:13
-
-
Save moracabanas/a901d01eb3ccd2c56ee75a7831c12f56 to your computer and use it in GitHub Desktop.
Run your favorite tools as ephemeral containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This check ensures <drun-network> exist | |
# every <drun> container will be reachable by it service name | |
# https://stackoverflow.com/a/53052379 | |
function docker_network_check { | |
docker network inspect drun-network >/dev/null 2>&1 || \ | |
docker network create --driver bridge drun-network | |
} | |
# You can run containered resources whithout the need of intalling it bloating your system | |
# ensure you find the docker image you want to use on the dockerhub registry. | |
# i.e. drun node npm install | |
alias drun='\ | |
docker_network_check && \ | |
docker run \ | |
-d \ | |
--rm \ | |
-w /app \ | |
-v $(pwd):/app \ | |
--network="drun-network" \ | |
' | |
# -d --> detached mode (no container tty output) | |
# --rm --> remove container once exited | |
# -w /app --> create and/or uses the /app dir | |
# -v $(pwd):/app --> mount ./ on /app inside the container | |
# --network --> create the same network so container name works as hostname for every container on the same docker network. | |
# You can pass flags like -it [drun <flags> <image>] | |
# *this will trigger the entrypoint so you might check the source dockerfile* | |
# So you can also drun interactive commandline | |
# $ drun -it node | |
# >> Welcome to Node.js v14.6.0. | |
# >> Type ".help" for more information. | |
# > | |
# Or you can also navigate the container filesystem of your tool | |
# *This will override the default dockerfile entrypoint preventing it to run it like an application. | |
# $ drun -it node bash | |
# >> root@6aa05052a573:/app# | |
# combined examples | |
alias composer='drun prooph/composer:7.4' | |
alias laravel-create-project='create-project --prefer-dist laravel/laravel' | |
# alias php='docker run --rm -v $(pwd):/var/www -w /var/www --network="drun-network" php' | |
# advanced examples | |
alias myadmin='echo "running on http://localhost:8080" && drun --name myadmin -e PMA_ARBITRARY=1 -p 8080:80 phpmyadmin/phpmyadmin' | |
function new_port { | |
_check="placeholder" | |
START_FROM=$1 | |
PORT=$(( $START_FROM )) | |
_check=$(ss -tulpn | grep ":${PORT}") | |
while [[ ! -z "${_check}" ]]; do | |
((PORT++)) | |
_check=$(ss -tulpn | grep ":${PORT}") | |
done | |
echo $PORT | |
} | |
export -f new_port | |
function fast-mysql { | |
PORT=$(new_port 3306) | |
drun \ | |
--name fast-mysql_"$PORT" \ | |
-p "$PORT:3306" \ | |
-v $(pwd)/mysql:/var/lib/mysql \ | |
-e MYSQL_USER="$1" \ | |
-e MYSQL_PASSWORD="$2" \ | |
-e MYSQL_ROOT_PASSWORD=root \ | |
-e MYSQL_DATABASE="$3" \ | |
mysql:"${4:-5.7}" | |
} # fast-mysql <user> <password> <database> <version>(default 5.7) | |
export -f fast-mysql | |
function fast-mariadb { | |
PORT=$(new_port 3306) | |
drun \ | |
--name fast-mariadb_"$PORT" \ | |
-p "$PORT:3306" \ | |
-v $(pwd)/mariadb-mysql:/var/lib/mysql \ | |
-e MYSQL_USER="$1" \ | |
-e MYSQL_PASSWORD="$2" \ | |
-e MYSQL_ROOT_PASSWORD=root \ | |
-e MYSQL_DATABASE="$3" \ | |
mariadb:"${4:-latest}" | |
} | |
export -f fast-mariadb | |
# if you loose some container like <<myadmin>> opened in the background you can attach the process: | |
# $ docker attach <name>/<ID> | |
# and kill it with Ctrl+c | |
# You can check for unnamed containers in the background and kill them with the first 3 ID letters | |
# $ docker ps --> example container with ID(2daa648046f7) | |
# $ docker stop 2da --> this stops that 2daa648046f7 container running on the background if you missed it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add it to your own
~/.bash_aliases
.then reopen your terminal or type
source ~/.bash_aliases
run <image>
You need docker :D