Sign inSign up

novinem/dev-env

By novinem

Updated over 1 year ago

This image is used to create a portable development environment.

Image
Developer tools
0

391

novinem/dev-env repository overview

About this repo

This is the repo that contains the code to build the dev-env Docker container. The main purpose of this docker container is to create a portable development environment so that no software needs to be installed on a (new) work computer. Also, the developer can then use an operating system of choise.

Use & installation: Basics

This Docker Image can be found on the Docker Hub at https://hub.docker.com/r/novinem/dev-env. To use this docker image, you'll only need to install Docker (dekstop), and set the following bash alias in: nano ~/.bash_aliases

alias devenv='docker run -it --rm -e GIT_USER_NAME="<your_name_here>" -e GIT_USER_EMAIL="<your_email_here>" -v ~/.ssh:/home/node/.ssh:ro -v $PWD:/app -w /app novinem/dev-env'

Now, we only need to 'save' the changes by entering the following command: source ~/.bash_aliases.

Congratulations! You can now use the dev-env with commands like: devenv npm install @scope/package-name or devenv composer require package/name.

Use & installation: Shortcuts

While the alias above is the only alias you'll need to get started, it's cumbersome to type 'devenv' in front of every command. That's why we suggest to also include the following aliases in: nano ~/.bash_aliases

alias node='devenv node'
alias npm='devenv npm'
alias npx='devenv npx'
alias git='devenv git'
alias php='devenv php'
alias composer='devenv composer'
alias laravel='devenv laravel'

In depth: Advanced Configuration

The simple aliases above work correctly for most use cases. However, sometimes you'll want to pass extra docker options for binding a custom volume or simply to set a environment variable. In those cases, you could just use the 'docker run' command directly to pass in your custom values. However, there is an easier way!

For these use cases, we developed a more advanced version of the 'devenv' alias above. Pay Attention! If you want to use this more advanced version, please make sure to remove the previously created alias first. Then, you can add the advanced 'devenv' function to: nano ~/.bash_aliases

function devenv {
    # Config variables
    local email="<your_email_here>"
    local name="<your_name_here>"

    # Runtime variables
    local runtime_args=()
    local command_args=()
    local image_name="novinem/dev-env"
    local verbose=false        # Flag to control echoing the Docker command

    # Persistent storage file for runtime_args
    local devenv_file=".devenv"

    # Load runtime_args from persistent storage if available
    if [[ -f "$devenv_file" ]]; then
        # Read the entire content of .devenv as a single string and append to runtime_args
        runtime_args+=($(cat "$devenv_file"))
    fi

    # Parse CLI arguments
    while [[ $# -gt 0 ]]; do
        if [[ "$1" == "verbose" ]]; then
            verbose=true
            shift
        else
            command_args+=("$1")
            shift
        fi
    done

    # Construct the Docker command
    local docker_command=(
        docker run -it --rm
        -e GIT_USER_NAME="${name}"
        -e GIT_USER_EMAIL="${email}"
        -v ~/.ssh:/home/node/.ssh:ro
        -v "$PWD:/app"
        -w /app
        "${runtime_args[@]}"
        "$image_name"
        "${command_args[@]}"
    )

    # Optionally echo the Docker command
    if $verbose; then
        echo "-------- COMMAND --------"
        echo "${docker_command[@]}"
        echo "------- DOCKER RUN -------"
    fi

    # Execute the Docker command
    "${docker_command[@]}"
}

In depth: Advanced Configuration -> Example use case

Why would you use the advanced version above? Let's say you want to develop a Laravel Package that is on your local PC. Normally, you would like to include that local package in your main Laravel project by including it in the repositories section of composer.json, like this:

....
"repositories": {
    "dev-package": {
        "type": "path",
        "url": "/home/user/laravel-package-repository",
        "options": {
            "symlink": true
        }
    }
}

However, in this case, you can't just use the regular 'devenv' alias by running: devenv composer require vendor/laravel-package-repository. That's becase the URL (/home/user/laravel-package-repository) will not be not found by composer inside the devenv container. For this, you'll want to add another bind mount in docker (in the exact same location) so that composer could find that URL.

The 'devenv' function will allow you to do just that! It reads the content of a .devenv file in the current working directory and passes those contents to the 'docker run' part of the command.

In depth: Advanced Configuration -> Example #1 (Single argument)

Contents of .devenv:

-v /home/user/laravel-package-repository:/home/user/laravel-package-repository

Command:

devenv composer require vendor/laravel-package-repository

Explanation:

In this example, the folder (/home/user/laravel-package-repository) gets mounted inside the container at runtime.

In depth: Advanced Configuration -> Example #2 (Multiple arguments)

Contents of .devenv:

-e TEST=true
-e REASON=because

Command:

devenv php -v

Explanation:

In this example, both environment variables gets set inside the container at runtime.

In depth: Advanced Configuration -> Example #2 (Debug docker command)

Contents of .devenv:

-e TEST=true -e REASON=because

Command:

devenv verbose php -v

Explanation:

This example is the same as the example above. Only now, we added the 'verbose' argument right after the 'devenv' call. This will print the actual command that gets used in plain text (for debugging purposes). The 'verbose' argument a reserved argument that must be placed before all other arguments!

In depth: what's in this image?

This image works best when setting the aliases (or the advanced function) above. Those make sure to mount the current working directory in the right location inside the container to work with. Also, the alias mounts your own .ssh folder into the .ssh folder inside the container so you can use all you own SSH Keys (with GIT for example). As of now, this image has these features built in that can manipulate your working directory:

GIT

Features include:

  • Alias creation of the following aliasses:
    • git ac = !git add . && git commit -m
    • git amend = git commit --amend
    • git lg = git log --oneline --decorate --all --reverse
    • git s = status -sb
  • Enforcing the [https://www.conventionalcommits.org/en/v1.0.0/](Conventional Commits) guidelines by setting a Git commit hook that uses the built in NPM library commitlint. (this makes it easy to add automatic versioning and automatic generation of the changelog by implementing semantic-release in CI)
NODE (v23.3)

Features include:

PHP (v8.3)

Features include:

  • Composer Package manager (v2.8.3)
  • Preinstalled (Global) Composer packages
    • laravel/installer
  • Preinstalled PHP Libraries
    • php8.3-zip
    • php8.3-mbstring
    • php8.3-curl
    • php8.3-xml
    • php8.3-pdo
    • php8.3-tokenizer
    • php8.3-ctype
    • php8.3-bcmath
    • php8.3-fileinfo

Tag summary

Content type

Image

Digest

sha256:a8ef8c037

Size

216.2 MB

Last updated

over 1 year ago

docker pull novinem/dev-env