Skip to content

Instantly share code, notes, and snippets.

@flyingluscas
Last active November 20, 2019 21:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save flyingluscas/a2fc4e637f3d967d427105055f6be8cd to your computer and use it in GitHub Desktop.
Save flyingluscas/a2fc4e637f3d967d427105055f6be8cd to your computer and use it in GitHub Desktop.
Shell functions to run things like NPM, Composer and PHP using Docker.

Shell functions to run things using Docker.

Versions

Control the versions using environment variables, put this in your .bashrc or .zshrc.

PHP_VERSION=7.1
NODE_VERSION=8.0

Shell Functions

The shell functions below will run PHP, Composer, Node, NPM and Yarn using Docker, put these functions inside your .bashrc or .zshrc as well.

PHP

php () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --user $(id -u):$(id -g) \
        --volume /etc/passwd:/etc/passwd:ro \
        --volume /etc/group:/etc/group:ro \
        --volume $(pwd):/code \
        --workdir /code \
        --publish 8000:8000 \
        --publish 8080:8080 \
        php:$PHP_VERSION-cli php "$@"
}

Composer

composer () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --user $(id -u):$(id -g) \
        --volume /etc/passwd:/etc/passwd:ro \
        --volume /etc/group:/etc/group:ro \
        --volume $(pwd):/code \
        --volume $HOME/.config/composer:/composer \
        --workdir /code \
        composer "$@"
}

PHPUnit

phpunit () {
    php vendor/bin/phpunit "$@"
}

NodeJs

node () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --user $(id -u):$(id -g) \
        --volume /etc/passwd:/etc/passwd:ro \
        --volume /etc/group:/etc/group:ro \
        --volume $(pwd):/code \
        --workdir /code \
        --publish 3000:3000 \
        --publish 3030:3030 \
        --publish 3333:3333 \
        --publish 8000:8000 \
        node:$NODE_VERSION node "$@"
}

NPM

npm () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --user $(id -u):$(id -g) \
        --volume /etc/passwd:/etc/passwd:ro \
        --volume /etc/group:/etc/group:ro \
        --volume $HOME/.config:$HOME/.config \
        --volume $HOME/.npm:$HOME/.npm \
        --volume $(pwd):/code \
        --workdir /code \
        --entrypoint /usr/local/bin/npm \
        --publish 3000:3000 \
        --publish 3030:3030 \
        --publish 3333:3333 \
        --publish 8000:8000 \
        node:$NODE_VERSION "$@"
}

YARN

yarn () {
    yarnFile="$HOME/.yarnrc"

    if [ ! -f $yarnFile ]; then
        touch $yarnFile
    fi

    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --user $(id -u):$(id -g) \
        --volume /etc/passwd:/etc/passwd:ro \
        --volume /etc/group:/etc/group:ro \
        --volume $HOME/.config:$HOME/.config \
        --volume $HOME/.cache:$HOME/.cache \
        --volume $yarnFile:$yarnFile \
        --volume $(pwd):/code \
        --workdir /code \
        --entrypoint /usr/local/bin/yarn \
        --publish 3000:3000 \
        --publish 3030:3030 \
        --publish 3333:3333 \
        --publish 8000:8000 \
        node:$NODE_VERSION "$@"
}
@ThamaraHessel
Copy link

😍 good guy =)

@lucassmacedo
Copy link

lucassmacedo commented Jul 29, 2017

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment