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