Blame

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