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