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