Skip to content

Instantly share code, notes, and snippets.

@Thermatix
Created March 16, 2023 17:38
Show Gist options
  • Save Thermatix/8ccdc7b64477cb38320f81ba1473ff38 to your computer and use it in GitHub Desktop.
Save Thermatix/8ccdc7b64477cb38320f81ba1473ff38 to your computer and use it in GitHub Desktop.
A set of files that can be used for the purposes of setting up a Rust based dev environment inside of a docker container.

TODO

Nothing at the mokment :)

Dependencies

Task running

This project useses just as a task runner, for a full list of available tasks do:

$ just -l

current tasks available are:

    build                   # build debug binary
    container               # bring up dev environment container
    make_image              # force a rebuild of the image
    release                 # build release binary
    run *COMMAND            # execute a command within the conainter
    start_container_runtime # helper for starting container runtime
    stop_container          # bring down dev environment container
    test TEST               # run a specific test
    test-all                # run all tests

To run a task do:

& just TASK_NAME ANY ARGS
version: '3'
services:
rust-dev:
build:
context: .
args:
- HTTP_PROXY=$HTTP_PROXY
- NO_PROXY=$NO_PROXY
- HTTPS_PROXY=$HTTP_PROXY
image: ${CONTAINER_NAME}:dev
container_name: ${CONTAINER_NAME}
environment:
http_proxy: ${HTTP_PROXY}
https_proxy: ${HTTP_PROXY}
volumes:
- ${PWD}:/workspace:rw
- target:/workspace/target:rw
tty: true
volumes:
target:
external: false
FROM rust:latest AS build
WORKDIR /workspace
FROM build AS dev
set shell := ["bash", "-c"]
container := "rust-dev"
notfound_regex := "[^n][^o][^t] [^f][^o][^u][^n][^d]" # I don't like this but "just" doesn't support negative look ahead
container_runtime := if `type podman` =~ notfound_regex {
"podman"
} else if `type docker` =~ notfound_regex {
"docker"
} else {
error("podman nor docker executables found")
}
container_exec := container_runtime + " exec -it " + container
# helper for starting container runtime
start_container_runtime:
#!/usr/bin/env bash
if [ "{{ container_runtime }}" == "podman" ]; then
podman machine init
podman machine start
else
echo "start docker however it's started"
exit 1
fi
[private]
docker_check:
#!/usr/bin/env bash
if ! {{ container_runtime }} info > /dev/null 2>&1; then
echo "This script uses docker or podman, and neither of them running - please start one of them and try again!"
exit 1
fi
[private]
image : docker_check
#!/usr/bin/env bash
if ! {{ container_runtime }} image inspect {{ container }}:dev > /dev/null 2>&1; then
$DOCKER_BUILDKIT=1 {{ container_runtime }} build -t "{{ container }}:dev" \
--target dev \
--build-arg HTTP_PROXY=$HTTP_PROXY \
--build-arg HTTPS_PROXY=$HTTPS_PROXY \
--build-arg NO_PROXY=$NO_PROXY \
.
fi
# force a rebuild of the image
make_image: docker_check
$DOCKER_BUILDKIT=1 {{ container_runtime }} build -t "{{ container }}:dev" \
--no-cache \
--target dev \
--build-arg HTTP_PROXY=$HTTP_PROXY \
--build-arg HTTPS_PROXY=$HTTPS_PROXY \
--build-arg NO_PROXY=$NO_PROXY \
.
# bring up dev environment container
container: image
#!/usr/bin/env bash
if [ ! "$( {{ container_runtime }} container inspect -f '{{{{.State.Status}}' {{ container }} )" == "running" ]; then
CONTAINER_NAME="{{ container }}" docker-compose up -d
fi
# build debug binary
build: container
{{ container_exec }} cargo build
# run all tests
test-all: container
{{ container_exec }} cargo test
# run a specific test
test TEST: container
{{ container_exec }} cargo test {{ TEST }}
# build release binary
release: container
{{ container_exec }} cargo build --release
# execute a command within the conainter
run *COMMAND: container
{{ container_exec }} {{ COMMAND }}
# bring down dev environment container
stop_container: container
docker-compose stop && docker-compose down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment