Skip to content

Instantly share code, notes, and snippets.

@clcollins
Last active December 19, 2022 09:04

Revisions

  1. clcollins revised this gist Mar 10, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions run-docker-container.sh
    Original file line number Diff line number Diff line change
    @@ -32,9 +32,9 @@ DOCKERCMD="docker"
    SITENAME=${TITLE}
    NAME=${IMAGE}

    declare -a ENVAR_STRING
    for envar in ${ENVARS[@]} ; do
    ENVAR_STRING+=("-e ${envar}")
    declare -a ENVVAR_STRING
    for envvar in ${ENVVARS[@]} ; do
    ENVVAR_STRING+=("-e ${envvar}")
    done

    declare -a PORT_STRING
    @@ -74,7 +74,7 @@ if $DOCKERCMD ps -a | awk "/${NAME}/ {print $NF}" | grep $NAME &>/dev/null ; the
    && $DOCKERCMD rm $NAME 1>/dev/null
    fi

    OPTS="${ENVAR_STRING[@]} ${PORT_STRING[@]} ${VOLUME_STRING[@]} $NAME_STRING $RESTART_STRING $ENTRYPOINT_STRING"
    OPTS="${ENVVAR_STRING[@]} ${PORT_STRING[@]} ${VOLUME_STRING[@]} $NAME_STRING $RESTART_STRING $ENTRYPOINT_STRING"

    THE_COMMAND="$DOCKERCMD run $OPTS $DAEMON_STRING $IMAGE $CMD_STRING"
    if $DRY_RUN ; then
  2. clcollins revised this gist Mar 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion run-docker-container.sh
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ else
    DAEMON_STRING='-it'
    fi

    if $DOCKERCMD ps -a | awk '/${NAME}/ {print $NF}' | grep $NAME &>/dev/null ; then
    if $DOCKERCMD ps -a | awk "/${NAME}/ {print $NF}" | grep $NAME &>/dev/null ; then
    $DOCKERCMD stop $NAME 1>/dev/null \
    && $DOCKERCMD rm $NAME 1>/dev/null
    fi
  3. clcollins revised this gist Mar 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion run-docker-container.sh
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ else
    DAEMON_STRING='-it'
    fi

    if $DOCKERCMD inspect $NAME &>/dev/null ; then
    if $DOCKERCMD ps -a | awk '/${NAME}/ {print $NF}' | grep $NAME &>/dev/null ; then
    $DOCKERCMD stop $NAME 1>/dev/null \
    && $DOCKERCMD rm $NAME 1>/dev/null
    fi
  4. clcollins revised this gist Mar 10, 2016. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion run-docker-container.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/bin/bash

    # Set to true to just see what command
    # would be run
    DRY_RUN=false

    TITLE='MY-TEST-CONTAINER'
    IMAGE='MY-IMAGE'
    # Local volume to be mapped into the container any time you run it
    @@ -72,4 +76,13 @@ fi

    OPTS="${ENVAR_STRING[@]} ${PORT_STRING[@]} ${VOLUME_STRING[@]} $NAME_STRING $RESTART_STRING $ENTRYPOINT_STRING"

    echo "$DOCKERCMD run $OPTS $DAEMON_STRING $IMAGE $CMD_STRING"
    THE_COMMAND="$DOCKERCMD run $OPTS $DAEMON_STRING $IMAGE $CMD_STRING"
    if $DRY_RUN ; then
    echo "$THE_COMMAND"
    exit 0
    else
    echo "$THE_COMMAND"
    exec $THE_COMMAND
    fi


  5. clcollins created this gist Mar 10, 2016.
    75 changes: 75 additions & 0 deletions run-docker-container.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/bash

    TITLE='MY-TEST-CONTAINER'
    IMAGE='MY-IMAGE'
    # Local volume to be mapped into the container any time you run it
    # usually with config files or whatnot
    # Ex MYCONFDIR='/srv/myconfdirs/${TITLE}'
    MYCONFDIR=""

    # An array of envvars
    # Ex. ENVVARS=("SITENAME=my.site.name}" "RUBY_VERSION=2.3.0" "CONTAINER=true")
    ENVVARS=()

    # Array of ports like (80:80 443:443 3000 8080)
    # Can be mapped or unmapped
    PORTS=()

    # Array of volumes like ("${MYCONFDIR}:/conf")
    VOLUMES=()
    ENTRYPOINT=""
    CMD=""
    RESTART=""
    DAEMON=false

    # The Docker command to use.
    # Could be different if including --tlsverify -H "hostname:hostport", etc
    DOCKERCMD="docker"
    SITENAME=${TITLE}
    NAME=${IMAGE}

    declare -a ENVAR_STRING
    for envar in ${ENVARS[@]} ; do
    ENVAR_STRING+=("-e ${envar}")
    done

    declare -a PORT_STRING
    for port in ${PORTS[@]} ; do
    PORT_STRING+=("-p ${port}")
    done

    declare -a VOLUME_STRING
    for volume in ${VOLUMES[@]} ; do
    VOLUME_STRING+=("-v ${volume}")
    done

    if [[ ! -z $NAME ]] ; then
    NAME_STRING="--name ${NAME}"
    fi

    if [[ ! -z $RESTART ]] ; then
    RESTART_STRING="--restart ${RESTART}"
    fi

    if [[ ! -z $ENTRYPOINT ]] ; then
    ENTRYPOINT_STRING="--entrypoint ${ENTRYPOINT}"
    fi

    if [[ ! -z $CMD ]] ; then
    CMD_STRING="${CMD}"
    fi

    if $DAEMON ; then
    DAEMON_STRING='-d'
    else
    DAEMON_STRING='-it'
    fi

    if $DOCKERCMD inspect $NAME &>/dev/null ; then
    $DOCKERCMD stop $NAME 1>/dev/null \
    && $DOCKERCMD rm $NAME 1>/dev/null
    fi

    OPTS="${ENVAR_STRING[@]} ${PORT_STRING[@]} ${VOLUME_STRING[@]} $NAME_STRING $RESTART_STRING $ENTRYPOINT_STRING"

    echo "$DOCKERCMD run $OPTS $DAEMON_STRING $IMAGE $CMD_STRING"