Skip to content

Instantly share code, notes, and snippets.

@diegovarussa
Last active September 16, 2019 12:05
Show Gist options
  • Save diegovarussa/2bf07cf968b8212de4a0c7f118084480 to your computer and use it in GitHub Desktop.
Save diegovarussa/2bf07cf968b8212de4a0c7f118084480 to your computer and use it in GitHub Desktop.
Add docker container as a standard command

Edit file (Mac ~/.bash_profile) (Linux: ~/.bashrc or ~/.zshrc) and add:

your_command_name () {
    tty=
    tty -s && tty=--tty
	docker run \
        $tty \
        --rm \
        your_docker/image "$@"
}

obs: tty makes the output colorful

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):/app \
        composer "$@"
}

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):/app \
        php:7.2-cli php "$@"
}

aws () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
	-e "AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX" \
	-e "AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXX" \
	-e "AWS_DEFAULT_REGION=XXXXXXXXXXXXX" \
        --volume $(pwd):/aws \
        mikesir87/aws-cli aws "$@"
}

sam () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
	-e "AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX" \
	-e "AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXX" \
	-e "AWS_DEFAULT_REGION=XXXXXXXXXXXXX" \
        -v /var/run/docker.sock:/var/run/docker.sock \
        -p 3000:3000 \
        --volume "$PWD":/var/opt \
        -w /var/opt  \
        diegovarussa/aws-cli sam "$@"
}

alias vue='docker run -it --rm -v "$PWD":"$PWD" -w "$PWD" -p 8080:8080 -u "$(id -u)" ebiven/vue-cli vue'
alias npm='docker run -it --rm -v "$PWD":"$PWD" -w "$PWD" -p 8080:8080 -u "$(id -u)" node:slim npm'

For Windows:

  • Create a folder called Aliases your user directory like this C:\Users\<your_user_here>\Aliases
  • Add this folder path to your Environment Variables Path
  • Create a .bat file az.bat inside this that folder
  • Add this content:
@echo off
echo.
docker run -it --rm mcr.microsoft.com/azure-cli az %*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment