Blame

14246b Tebby Dog 2025-07-06 21:26:59
Init
1
# Docker-Shortcuts
2
3
# Description
4
5
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.
6
7
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>`).
8
9
# Usage**
10
11
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`.
12
13
You can run the script in three different modes:
14
15
1. **No arguments**: The script will use the directory name as both the local image name and Docker image name.
16
```bash
17
dcp
18
```
19
2. **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.
20
```bash
21
dcp <DOCKER_IMAGE_NAME>
22
```
23
3. **Two arguments**: You can specify both the local image name and the Docker image name as separate command-line arguments.
24
```bash
25
dcp <LOCAL_IMAGE_NAME> <DOCKER_IMAGE_NAME>
26
```
27
28
# Example Output
29
30
When you run the script, it will output something like this:
31
```
32
Docker image built with tag: myimage:02.14.21.1
33
Docker image tagged: tebwritescode/myimage:02.14.21.1
34
Docker image tagged: tebwritescode/myimage:latest
35
Docker image pushed: tebwritescode/myimage:02.14.21.1
36
Docker image pushed: tebwritescode/myimage:latest
37
38
Local Image Name: myimage
39
Docker Image Name: myimage
40
```
41
Note that the exact output may vary depending on your directory name and DockerHub credentials.
42
43
# Assumptions
44
45
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.
46
47
# Downloading the Script
48
49
Create folders
50
mkdir /opt/dcp
51
mkdir /opt/dcp/counters
52
53
You can download this script by running:
54
```bash
55
curl -sSO https://github.com/tebwritescode/dcp/blob/14d5d8d8126f94e9867152339f1ee644ee9e1c1e/dcp.sh > /opt/dcp/dcp.sh && chmod +x /opt/dcp/dcp.sh
56
```
57
58
Or copy-pasta
59
60
```bash
61
#!/bin/bash
62
63
# Check how many arguments were passed
64
if [ $# -eq 0 ]; then
65
# No arguments passed
66
IMAGE_NAME=$(basename "$PWD")
67
DOCKER_IMAGE_NAME=$(basename "$PWD")
68
elif [ $# -eq 1 ]; then
69
# One argument passed
70
IMAGE_NAME=$(basename "$PWD")
71
DOCKER_IMAGE_NAME=$1
72
elif [ $# -eq 2 ]; then
73
# Two arguments passed
74
IMAGE_NAME=$1
75
DOCKER_IMAGE_NAME=$2
76
else
77
echo "Error: Too many arguments passed."
78
echo "Usage: $0 - Use parent directory name for both local and docker image name."
79
echo "$0 <DOCKER_IMAGE_NAME> - Use parent directory name for local image name and use <DOCKER_IMAGE_NAME> as docker image name/"
80
echo "$0 <IMAGE_NAME> <DOCKER_IMAGE_NAME> - Use <IMAGE_NAME> for local image name and use <DOCKER_IMAGE_NAME> as docker image name/"
81
exit 1
82
fi
83
84
# Get today's date in the format MM.DD.YY
85
DATE=$(date +"%m.%d.%y")
86
87
# Define the file where the counter will be stored
88
COUNTER_FILE="/opt/dcp/counters/build_counter_${DOCKER_IMAGE_NAME}_${DATE}.txt"
89
90
# Check if the counter file exists, if not, create it and set the counter to 0
91
if [ ! -f "$COUNTER_FILE" ]; then
92
rm -fv build_counter_${DOCKER_IMAGE_NAME}_*.txt
93
echo "0" > "$COUNTER_FILE"
94
fi
95
96
# Read the current counter value
97
COUNTER=$(cat "$COUNTER_FILE")
98
99
# Increment the counter by 1
100
NEW_COUNTER=$((COUNTER + 1))
101
102
# Update the counter in the file
103
echo "$NEW_COUNTER" > "$COUNTER_FILE"
104
105
# Build the Docker image with the version tag in the format MM.DD.YY.<counter>
106
if ! /usr/bin/docker build -t "$IMAGE_NAME":"$DATE"."$NEW_COUNTER" .; then
107
echo "Docker build failed."
108
exit 1
109
fi
110
111
# Print the image tag for reference
112
echo "Docker image built with tag: $IMAGE_NAME:$DATE.$NEW_COUNTER"
113
114
# Tag the image for DockerHub
115
/usr/bin/docker tag "$IMAGE_NAME:$DATE.$NEW_COUNTER" tebwritescode/"$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER"
116
/usr/bin/docker tag "$IMAGE_NAME:$DATE.$NEW_COUNTER" tebwritescode/"$DOCKER_IMAGE_NAME:latest"
117
118
# Print the image tag for reference
119
echo "Docker image tagged: tebwritescode/$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER"
120
echo "Docker image tagged: tebwritescode/$DOCKER_IMAGE_NAME:latest"
121
122
# Push the Docker image to DockerHub
123
if ! /usr/bin/docker push tebwritescode/"$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER"; then
124
echo "Docker push failed for $NEW_COUNTER."
125
exit 1
126
fi
127
128
if ! /usr/bin/docker push tebwritescode/"$DOCKER_IMAGE_NAME:latest"; then
129
echo "Docker push failed for 'latest'."
130
exit 1
131
fi
132
133
# Output the assigned values (optional)
134
echo "Local Image Name: $IMAGE_NAME"
135
echo "Docker Image Name: $DOCKER_IMAGE_NAME"
136
137
# Print the image push for reference
138
echo "Docker image pushed: tebwritescode/$DOCKER_IMAGE_NAME:$DATE.$NEW_COUNTER"
139
echo "Docker image pushed: tebwritescode/$DOCKER_IMAGE_NAME:latest"
140
```
141
142
# Setting up an Alias
143
144
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`):
145
```bash
146
alias dcp='/opt/dcp/dcp.sh'
147
```
148
Once you've added this alias, you can simply run `dcp` instead of typing out the full script name.
149
150
# Using the Alias
151
152
To use the `dcp` alias, simply type:
153
```bash
154
dcp [optional arguments]
155
```
156
For example:
157
```bash
158
dcp myimage latest
159
```
160
This will build and push a Docker image with the tag `tebwritescode/myimage:latest`.