Skip to content

Commit 1106028

Browse files
committed
make scripts generic and add documentation
1 parent b8dee26 commit 1106028

File tree

6 files changed

+116
-84
lines changed

6 files changed

+116
-84
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gitconfig

Dockerfile

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
FROM mcr.microsoft.com/dotnet/sdk:5.0-bullseye-slim AS base
22
RUN apt-get update
33
RUN apt-get -y install vim-nox tmux git fzf ripgrep curl python3 ssh
4-
RUN useradd -ms /bin/bash -u 1002 sam
5-
WORKDIR /home/sam
4+
RUN useradd -ms /bin/bash -u 1002 devuser
5+
WORKDIR /home/devuser
66
ENV TERM="xterm-256color"
77
COPY dotfiles/tmux.conf .tmux.conf
88
ADD https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash .git-completion.bash
99
ADD https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh .git-prompt.sh
1010
COPY dotfiles/bashrc .bashrc
11-
COPY dotfiles/gitconfig .gitconfig
11+
COPY .gitconfig .gitconfig
1212
RUN git clone https://github.com/christoomey/vim-tmux-navigator.git .vim/pack/plugins/start/vim-tmux-navigator
13-
RUN chown -R sam /home/sam
14-
USER sam
13+
RUN chown -R devuser /home/devuser
14+
USER devuser
1515
RUN mkdir -p -m 0700 ~/.ssh
1616
RUN ssh-keyscan ssh.dev.azure.com >> ~/.ssh/known_hosts
1717
ENTRYPOINT bash
1818

19-
FROM base AS align-services-dev
19+
FROM base AS dotnet-dev
2020
COPY dotfiles/vimrc-omni-install .vimrc
2121
RUN vim +'PlugInstall --sync' +qa
2222
COPY dotfiles/vimrc-omni .vimrc
2323
RUN .vim/plugged/omnisharp-vim/installer/omnisharp-manager.sh -l .cache/omnisharp-vim/omnisharp-roslyn
24-
RUN --mount=type=ssh,uid=1002 git clone [email protected]:v3/MFBTech/Syzygy%20API/align-services
25-
WORKDIR /home/sam/align-services
24+
ARG GIT_REPO
25+
ARG CLONE_DIR
26+
RUN --mount=type=ssh,uid=1002 git clone ${GIT_REPO} ${CLONE_DIR}
27+
WORKDIR /home/devuser/${CLONE_DIR}
2628
RUN dotnet restore
2729

28-
FROM base AS align-ts-dev
30+
FROM base AS ts-dev
2931
SHELL ["/bin/bash", "--login", "-c"]
3032
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
3133
&& . ~/.nvm/nvm.sh \
3234
&& nvm install v16.13.1
3335
COPY dotfiles/vimrc-coc-install .vimrc
3436
RUN vim +'PlugInstall --sync' +qa
3537
COPY dotfiles/vimrc-coc .vimrc
36-
RUN mkdir -pv /home/sam/.config/coc
38+
RUN mkdir -pv /home/devuser/.config/coc
3739
RUN . ~/.nvm/nvm.sh && vim +'CocInstall -sync coc-css coc-eslint coc-html coc-json coc-prettier coc-spell-checker coc-tsserver coc-yaml' +qa
3840
RUN . ~/.nvm/nvm.sh && vim +'CocUpdateSync' +qa
3941
COPY vim/coc-settings.json .vim/coc-settings.json
4042
RUN . ~/.nvm/nvm.sh && npm install -g @microsoft/rush
41-
RUN --mount=type=ssh,uid=1002 git clone [email protected]:v3/MFBTech/Syzygy%20Web%20App/align-ts
42-
WORKDIR /home/sam/align-ts
43+
ARG GIT_REPO
44+
ARG CLONE_DIR
45+
RUN --mount=type=ssh,uid=1002 git clone ${GIT_REPO} ${CLONE_DIR}
46+
WORKDIR /home/devuser/${CLONE_DIR}
4347
RUN rush install
4448
RUN git reset --hard

README.md

+44-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,49 @@
33
## Setup
44

55
- Run ssh-add on host to add ssh id to ssh-agent for forwarding
6-
- Jest tests take a lot of memory; in host docker assigned 6GB
6+
- Assumes your .ssh directory is at the root of your home directory on your machine
7+
- Copy your .gitconfig file into the root of this repo (will be ignored by .gitignore)
8+
- Use the existing `run-ts.sh` or `run-dotnet` scripts or create your own `run-[x].sh` script based on them
79

8-
## To do
10+
## run-[x].sh scripts
911

10-
- Use docker-compose instead of script once
11-
- support for passing ssh-agent port via compose
12-
- detachKeys releases ctrl-p when run through compose
12+
### Variables
13+
14+
```bash
15+
# Image target in Dockerfile
16+
IMAGE_TARGET="ts-dev"
17+
# Tag for image that will be built on host
18+
IMAGE_TAG="align-ts-dev"
19+
# Name of container that will be run from image
20+
CONTAINER_NAME="align-ts-dev-active"
21+
# Name of non-root user (in container, not host) that will be used in container
22+
DOCKER_USER_HOME="/home/devuser"
23+
# Port or range of ports to be proxied on host
24+
HOST_PORTS="3000"
25+
# Port or range of ports to be proxied from container
26+
CONTAINER_PORTS="3000"
27+
# Container hostname
28+
HOSTNAME="ts-docker"
29+
# Git repository
30+
GIT_REPO="[email protected]:v3/Fabrikam/some-repo"
31+
# Directory to clone into
32+
CLONE_DIR="some-repo"
33+
```
34+
35+
### Usage
36+
37+
`./run-[x].sh`
38+
39+
Builds image and runs the container. If image is already build that is used. If container already exists that is used.
40+
41+
`./run-x.sh -k`
42+
43+
Delete existing container (make sure there's nothing on there you wanted to save!)
44+
45+
`./run-x.sh -r`
46+
47+
Delete existing image
48+
49+
`./run-x.sh -x`
50+
51+
Delete both container and image

run-dotnet.sh

+7-33
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
11
#/bin/sh
22

3-
IMAGE_TARGET="align-services-dev"
3+
IMAGE_TARGET="dotnet-dev"
44
IMAGE_TAG="align-services-dev"
55
CONTAINER_NAME="align-services-dev-active"
6+
DOCKER_USER_HOME="/home/devuser"
67
HOST_PORTS="12000-12050"
78
CONTAINER_PORTS="12000-12050"
9+
HOSTNAME="dnet-docker"
10+
GIT_REPO="[email protected]:v3/MFBTech/Syzygy%20API/align-services"
11+
CLONE_DIR="align-services"
812

9-
# flags to easily delete image and container
10-
while getopts ":krx" option; do
11-
case $option in
12-
k)
13-
echo "Deleting container..."
14-
docker container rm ${CONTAINER_NAME}
15-
exit;;
16-
r)
17-
echo "Deleting image..."
18-
docker rmi ${IMAGE_TAG}
19-
exit;;
20-
x)
21-
echo "Deleting image and container..."
22-
docker container rm ${CONTAINER_NAME}
23-
docker rmi ${IMAGE_TAG}
24-
exit;;
25-
esac
26-
done
13+
source ./run-func.sh
2714

28-
# build image if not built already
29-
if [[ "$(docker images -q ${IMAGE_TAG} 2> /dev/null)" == "" ]]; then
30-
echo "Building image..."
31-
docker build --ssh default --pull --target ${IMAGE_TARGET} -t ${IMAGE_TAG} .
32-
fi
33-
34-
# run container if not created, otherwise attach to existing
35-
if [[ "$(docker container ls -qa --filter name=${CONTAINER_NAME} 2> /dev/null)" == "" ]]; then
36-
echo "Creating and running container..."
37-
docker run -p ${HOST_PORTS}:${CONTAINER_PORTS} --mount type=bind,src=${HOME}/.ssh,target=/home/sam/.ssh --detach-keys='ctrl-z,z' --name ${CONTAINER_NAME} -it ${IMAGE_TAG}
38-
else
39-
echo "Starting and attaching to existing container..."
40-
docker start --detach-keys='ctrl-z,z' -i ${CONTAINER_NAME}
41-
fi
15+
run_func "$@"

run-func.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#/bin/bash
2+
3+
run_func () {
4+
# flags to easily delete image and container
5+
local OPTIND o a
6+
while getopts ":krx" option; do
7+
case $option in
8+
k)
9+
echo "Deleting container..."
10+
docker container rm ${CONTAINER_NAME}
11+
exit;;
12+
r)
13+
echo "Deleting image..."
14+
docker rmi ${IMAGE_TAG}
15+
exit;;
16+
x)
17+
echo "Deleting image and container..."
18+
docker container rm ${CONTAINER_NAME}
19+
docker rmi ${IMAGE_TAG}
20+
exit;;
21+
esac
22+
done
23+
24+
# build image if not built already
25+
if [[ "$(docker images -q ${IMAGE_TAG} 2> /dev/null)" == "" ]]; then
26+
echo "Building image..."
27+
docker build --ssh default --pull --build-arg GIT_REPO=${GIT_REPO} --build-arg CLONE_DIR=${CLONE_DIR} --target ${IMAGE_TARGET} -t ${IMAGE_TAG} .
28+
fi
29+
30+
# run container if not created, otherwise attach to existing
31+
if [[ "$(docker container ls -qa --filter name=${CONTAINER_NAME} 2> /dev/null)" == "" ]]; then
32+
echo "Creating and running container..."
33+
docker run -p ${HOST_PORTS}:${CONTAINER_PORTS} --mount type=bind,src=${HOME}/.ssh,target=${DOCKER_USER_HOME}/.ssh --detach-keys='ctrl-z,z' --name ${CONTAINER_NAME} -h ${HOSTNAME} -it ${IMAGE_TAG}
34+
else
35+
echo "Starting and attaching to existing container..."
36+
docker start --detach-keys='ctrl-z,z' -i ${CONTAINER_NAME}
37+
fi
38+
}

run-ts.sh

+10-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
1-
#/bin/sh
1+
#/bin/bash
22

3-
IMAGE_TARGET="align-ts-dev"
3+
IMAGE_TARGET="ts-dev"
44
IMAGE_TAG="align-ts-dev"
55
CONTAINER_NAME="align-ts-dev-active"
6+
DOCKER_USER_HOME="/home/devuser"
7+
HOST_PORTS="3000"
8+
CONTAINER_PORTS="3000"
9+
HOSTNAME="ts-docker"
10+
GIT_REPO="[email protected]:v3/MFBTech/Syzygy%20Web%20App/align-ts"
11+
CLONE_DIR="align-ts"
612

7-
# flags to easily delete image and container
8-
while getopts ":krx" option; do
9-
case $option in
10-
k)
11-
echo "Deleting container..."
12-
docker container rm ${CONTAINER_NAME}
13-
exit;;
14-
r)
15-
echo "Deleting image..."
16-
docker rmi ${IMAGE_TAG}
17-
exit;;
18-
x)
19-
echo "Deleting image and container..."
20-
docker container rm ${CONTAINER_NAME}
21-
docker rmi ${IMAGE_TAG}
22-
exit;;
23-
esac
24-
done
13+
source ./run-func.sh
2514

26-
# build image if not built already
27-
if [[ "$(docker images -q ${IMAGE_TAG} 2> /dev/null)" == "" ]]; then
28-
echo "Building image..."
29-
docker build --ssh default --pull --target ${IMAGE_TARGET} -t ${IMAGE_TAG} .
30-
fi
31-
32-
# run container if not created, otherwise attach to existing
33-
if [[ "$(docker container ls -qa --filter name=${CONTAINER_NAME} 2> /dev/null)" == "" ]]; then
34-
echo "Creating and running container..."
35-
docker run -p 3000:3000 --mount type=bind,src=${HOME}/.ssh,target=/home/sam/.ssh --detach-keys='ctrl-z,z' --name ${CONTAINER_NAME} -it ${IMAGE_TAG}
36-
else
37-
echo "Starting and attaching to existing container..."
38-
docker start --detach-keys='ctrl-z,z' -i ${CONTAINER_NAME}
39-
fi
15+
run_func "$@"

0 commit comments

Comments
 (0)