Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jul 24, 2017

😍 good guy =)

@lucassmacedo

This comment has been minimized.

Copy link

commented Jul 29, 2017

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.