Created
November 19, 2020 15:15
-
-
Save diegoperini/d15828f4b77dcc1d24fc163120ed0de3 to your computer and use it in GitHub Desktop.
Shell/bash boilerplate for running commands in docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Setup container | |
################################## | |
CONTAINER_NAME="tempcontainer" | |
CONTAINER_BASE_IMAGE="amazonlinux:2" | |
(docker kill $CONTAINER_NAME && docker rm $CONTAINER_NAME) || echo Nothing to kill from previous run, starting a fresh install | |
SELINUX_SUFFIX="" | |
if [ -x "/sbin/sestatus" ]; then | |
/sbin/sestatus | grep -P "SELinux status:\s+enabled" >/dev/null | |
if [ $? -eq 0 ]; then | |
SELINUX_SUFFIX=":z" | |
fi | |
fi | |
docker run -d -it --name $CONTAINER_NAME \ | |
-v `pwd`:/opt${SELINUX_SUFFIX} \ | |
$CONTAINER_BASE_IMAGE bash | |
RUN="docker exec $CONTAINER_NAME" | |
# Run commands in docker with $RUN | |
################################## | |
$RUN echo HELLO WORLD | |
# Kill container | |
################################## | |
(docker kill $CONTAINER_NAME && docker rm $CONTAINER_NAME) || echo Nothing to kill from previous run, starting a fresh install |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment