sh 自动化Docker工作流程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 自动化Docker工作流程相关的知识,希望对你有一定的参考价值。

########## Default Values #########################

# GitHub Repo URL (Both SSH and HTTPS work)
REPO_URL="<YOUR_REPO_URL>"

# GitHub branch
BRANCH="master"

# Docker Image Tag
TAG="latest"

# DockerHub User Credentials
USER="abc"              # DockerHub username
EMAIL="xyz"             # DockerHub account email

# PASSWORD="xyz"                # Current User's password
# DOCKERHUB_PWD="xyz"           # DockerHub account password

####################################################

###### Parsing command line options ################
while [[ $# > 1 ]]
do
key="$1"

case $key in
    
    -r|--repo)
    REPO_URL="$2"
    shift # past argument
    ;;

    -b|--branch)
    BRANCH="$2"
    shift # past argument
    ;;

    -i|--image)
    IMAGE="$2"
    shift # past argument
    ;;
    
    -t|--tag)
    TAG="$2"
    shift # past argument
    ;;
    
    -u|--user)
    USER="$2"
    shift # past argument
    ;;

    -e|--email)
    EMAIL="$2"
    shift # past argument
    ;;

    *)
esac
shift # past argument or value
done
####################################################

printf "\n"

TEMP="$(echo $REPO_URL | grep / | rev | cut -d '/' -f 1 | rev)"
DIRNAME=(${TEMP//./ })  # Eg.: lens

CloneRepo () {
    # Check if local copy of repo exists. If not, clone repo.
    
    if [ -d "$DIRNAME" ]; then
        echo Repo exists.
    else
        git clone $REPO_URL
    fi

    cd $DIRNAME

    printf "\n"    
}

SetupBranch () {
    # Set up specified branch
    
    echo "Setting up branch $BRANCH..."
    git fetch && git checkout $BRANCH

    printf "\n"    
}


BuildImage () {
    # Build image from Docker File in current directory

    echo "Building docker image..."
    sudo docker build -t $IMAGE .

    printf "\n"
}


RunImage () {
    # Run docker image
    
    echo "Running docker image..."
    sudo docker run $IMAGE

    printf "\n"
}

TagImage () {
    # Tag docker image
    
    echo "Tagging docker image..."
    details="$(sudo docker images | grep $IMAGE)"
    details_array=($details})
    sudo docker tag ${details_array[2]} $USER/$IMAGE:$TAG
    # echo $PASSWORD | sudo -kS docker tag ${details_array[2]} $USER/$IMAGE:$TAG

    printf "\n"    
}


DockerHubLogin () {
    # Login to DockerHub account
    
    echo "Logging into DockerHub..."
    sudo docker login --username=$USER --email=$EMAIL
    # sudo docker login --username=$USER --password=$DOCKERHUB_PWD --email=$EMAIL
    
    printf "\n"       
}

PushImage () {
    # Push Docker image to our DockerHub account
    
    echo "Pushing docker image to DockerHub account..."
    sudo docker push $USER/$IMAGE
    # echo $PASSWORD | sudo -kS docker push $USER/$IMAGE
    
    printf "\n"
}


# Sequence of actions
CloneRepo
SetupBranch
BuildImage
# RunImage
TagImage
DockerHubLogin
PushImage


echo "Hurray!"

以上是关于sh 自动化Docker工作流程的主要内容,如果未能解决你的问题,请参考以下文章

sh 系统重启时自动重启Docker镜像。

sh jupyterHub + docker的半自动安装脚本

Coding使用构建计划,实现局域网Linux中docker的自动部署

Docker 环境下搭建nexus私服

docker 安装

Shell脚本自动编译打包Gradle项目并docker部署