Docker-Shortcuts
Description
This script automates the process of building and pushing a Docker image to DockerHub, while incrementally tracking the version number. It uses the directory name as the default local image name, but allows for customization through command-line arguments.
The script builds the Docker image with a version tag in the format MM.DD.YY.<counter>
, where <counter>
is an incremental value stored in a file. The image is then tagged with additional tags on DockerHub (latest
and tebwritescode/<image_name>:<date>.<counter>
).
Usage**
To use this script, save it to a file (e.g., build_and_push_docker_image.sh
) and make it executable by running chmod +x build_and_push_docker_image.sh
.
You can run the script in three different modes:
- No arguments: The script will use the directory name as both the local image name and Docker image name.
dcp
- One argument: The script will use the parent directory name for the local image name, but allow you to specify a custom Docker image name using the first command-line argument.
dcp <DOCKER_IMAGE_NAME>
- Two arguments: You can specify both the local image name and the Docker image name as separate command-line arguments.
dcp <LOCAL_IMAGE_NAME> <DOCKER_IMAGE_NAME>
Example Output
When you run the script, it will output something like this:
Docker image built with tag: myimage:02.14.21.1 Docker image tagged: tebwritescode/myimage:02.14.21.1 Docker image tagged: tebwritescode/myimage:latest Docker image pushed: tebwritescode/myimage:02.14.21.1 Docker image pushed: tebwritescode/myimage:latest Local Image Name: myimage Docker Image Name: myimage
Note that the exact output may vary depending on your directory name and DockerHub credentials.
Assumptions
This script assumes you have Docker installed on your system, as well as the docker
command in your PATH. Additionally, it requires a valid DockerHub account with access to the repository where you want to push your image.
Downloading the Script
Create folders
mkdir /opt/dcp mkdir /opt/dcp/counters
You can download this script by running:
curl -sSO https://github.com/tebwritescode/dcp/blob/14d5d8d8126f94e9867152339f1ee644ee9e1c1e/dcp.sh > /opt/dcp/dcp.sh && chmod +x /opt/dcp/dcp.sh
Or copy-pasta
#!/bin/bash # Check how many arguments were passed if [ $# -eq 0 ]; then # No arguments passed IMAGE_NAME=$(basename "$PWD") DOCKER_IMAGE_NAME=$(basename "$PWD") elif [ $# -eq 1 ]; then # One argument passed IMAGE_NAME=$(basename "$PWD") DOCKER_IMAGE_NAME=$1 elif [ $# -eq 2 ]; then # Two arguments passed IMAGE_NAME=$1 DOCKER_IMAGE_NAME=$2 else echo "Error: Too many arguments passed." echo "Usage: $0 - Use parent directory name for both local and docker image name." echo "$0 <DOCKER_IMAGE_NAME> - Use parent directory name for local image name and use <DOCKER_IMAGE_NAME> as docker image name/" echo "$0 <IMAGE_NAME> <DOCKER_IMAGE_NAME> - Use <IMAGE_NAME> for local image name and use <DOCKER_IMAGE_NAME> as docker image name/" exit 1 fi # Get today's date in the format MM.DD.YY DATE=$(date +"%m.%d.%y") # Define the file where the counter will be stored COUNTER_FILE="/opt/dcp/counters/build_counter_${DOCKER_IMAGE_NAME}_${DATE}.txt" # Check if the counter file exists, if not, create it and set the counter to 0 if [ ! -f "$COUNTER_FILE" ]; then rm -fv build_counter_${DOCKER_IMAGE_NAME}_*.txt echo "0" > "$COUNTER_FILE" fi # Read the current counter value COUNTER=$(cat "$COUNTER_FILE") # Increment the counter by 1 NEW_COUNTER=$((COUNTER + 1)) # Update the counter in the file echo "$NEW_COUNTER" > "$COUNTER_FILE" # Build the Docker image with the version tag in the format MM.DD.YY.<counter> if ! /usr/bin/docker build -t "$IMAGE_NAME":"$DATE"."$NEW_COUNTER" .; then echo "Docker build failed." exit 1 fi # Print the image tag for reference echo "Docker image built with tag: $IMAGE_NAME:$DATE.$NEW_COUNTER" # Tag the image for DockerHub /usr/bin/docker tag "$IMAGE_NAME:$DATE.$NEW_COUNTER" tebwritescode/"$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER" /usr/bin/docker tag "$IMAGE_NAME:$DATE.$NEW_COUNTER" tebwritescode/"$DOCKER_IMAGE_NAME:latest" # Print the image tag for reference echo "Docker image tagged: tebwritescode/$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER" echo "Docker image tagged: tebwritescode/$DOCKER_IMAGE_NAME:latest" # Push the Docker image to DockerHub if ! /usr/bin/docker push tebwritescode/"$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER"; then echo "Docker push failed for $NEW_COUNTER." exit 1 fi if ! /usr/bin/docker push tebwritescode/"$DOCKER_IMAGE_NAME:latest"; then echo "Docker push failed for 'latest'." exit 1 fi # Output the assigned values (optional) echo "Local Image Name: $IMAGE_NAME" echo "Docker Image Name: $DOCKER_IMAGE_NAME" # Print the image push for reference echo "Docker image pushed: tebwritescode/$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER" echo "Docker image pushed: tebwritescode/$DOCKER_IMAGE_NAME:latest"
Setting up an Alias
To make it easier to use this script, you can set up an alias by adding the following line to your shell configuration file (e.g., ~/.bashrc
or ~/.zshrc
):
alias dcp='/opt/dcp/dcp.sh'
Once you've added this alias, you can simply run dcp
instead of typing out the full script name.
Using the Alias
To use the dcp
alias, simply type:
dcp [optional arguments]
For example:
dcp myimage latest
This will build and push a Docker image with the tag tebwritescode/myimage:latest
.