├── LICENSE ├── README.md └── install.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Munis Isazade 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Usefull comands while working with docker containers 2 | 3 | 4 | 5 | - These comands avalible for Linux and MacOS operating systems. 6 | 7 | 8 | 9 | ### Installation ### 10 | 11 | - In order to install comands you require to have sudo privileged. if below comand does not work try with sudo. 12 | 13 | Use [curl](https://curl.haxx.se/) to install comands. 14 | 15 | ``` 16 | $ curl -LO https://raw.githubusercontent.com/munisisazade/docker-helper-commands/master/install.sh && bash install.sh 17 | ``` 18 | 19 | ### List of Usefull Comands ### 20 | ```bash 21 | build-docker 22 | down-docker 23 | connect-docker 24 | dangling-remove-docker 25 | restart-docker 26 | migrate-docker 27 | logs-docker 28 | backup-database-docker 29 | restore-database-docker 30 | ``` 31 | 32 | ### Usage ### 33 | 34 | ```bash 35 | build-docker 36 | Building multi-containers. Comand should given where your docker-compose file exist. 37 | 38 | down-docker 39 | Stoping runnig multi-containers in the directory. 40 | 41 | connect-docker 42 | Executing inside running docker container, with bash. 43 | ex: connect-docker bash container_id 44 | 45 | dangling-remove-docker 46 | This comand removes Inactive containers and stalled images. 47 | 48 | restart-docker 49 | This comand restarting multi-containers in the given directory. 50 | 51 | migrate-docker 52 | The comand useful for django projects in case you have migration file and needs to migrated to DB. 53 | ex: migrate-docker container_name_or_id 54 | 55 | logs-docker 56 | This comand tailing logs of multi-containers in the given directory. 57 | 58 | backup-database-docker 59 | Taking backup file of running Postgres container. 60 | 61 | 62 | restore-database-docker 63 | Restoring runnig postgres container. 64 | 65 | ``` 66 | 67 | 68 | ## Contributing 69 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 70 | 71 | 72 | ## License 73 | [MIT](https://github.com/munisisazade/docker-helper-commands/blob/master/LICENSE/) 74 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Author Munis Isazade Django developer 3 | 4 | VERSION="0.3" 5 | ERROR_STATUS=0 6 | #if [[ "$OSTYPE" == "darwin"* ]]; then 7 | # Detect Operation system is Macbook pro OSX 8 | LOCAL_COMMAND_DIRECTORY=/usr/local/bin/ 9 | ROOT_COMMAND_DIRECTORY=/usr/local/bin/ 10 | #else 11 | # LOCAL_COMMAND_DIRECTORY=~/.local/bin/ 12 | # ROOT_COMMAND_DIRECTORY=/bin/ 13 | #fi 14 | WORKING_DIRECTRY=$(pwd) 15 | ISSUE_URL="https://github.com/munisisazade/docker-helper-commands/issues" 16 | 17 | 18 | #usage: ChangeColor $COLOR text/background 19 | function ChangeColor() 20 | { 21 | TYPE="" 22 | case "$2" in 23 | "text") TYPE="setaf" 24 | ;; 25 | "back") TYPE="setab" 26 | ;; 27 | *) TYPE="setaf" 28 | esac 29 | 30 | 31 | 32 | case "$1" in 33 | "red") tput "$TYPE" 1 34 | ;; 35 | "orange") tput "$TYPE" 3 36 | ;; 37 | "blue") tput "$TYPE" 4 38 | ;; 39 | "green") tput "$TYPE" 2 40 | ;; 41 | "black") tput "$TYPE" 0 42 | ;; 43 | "white") tput "$TYPE" 7 44 | ;; 45 | "magenta") tput "$TYPE" 5 46 | ;; 47 | "cyan") tput "$TYPE" 7 48 | ;; 49 | *) tput "$TYPE" 0 50 | esac 51 | } 52 | 53 | function usage { 54 | echo -e "Thans for using this tool:" 55 | if [ "$EUID" -ne 0 ]; then 56 | echo -e "You install $(whoami) user all shels here $LOCAL_COMMAND_DIRECTORY" 57 | else 58 | echo -e "You install $(whoami) user all shels here $ROOT_COMMAND_DIRECTORY" 59 | fi 60 | echo -e "Usage:" 61 | echo -e "$(ChangeColor blue text) build-docker $(ChangeColor green text)Update and create docker compose up $(ChangeColor white text)" 62 | echo -e "\n" 63 | echo -e "$(ChangeColor blue text) down-docker $(ChangeColor green text) Down all docker compose containers $(ChangeColor white text)" 64 | echo -e "\n" 65 | echo -e "$(ChangeColor blue text) connect-docker $(ChangeColor green text) Connect available docker container with sh or bash $(ChangeColor white text)" 66 | echo -e "\n" 67 | echo -e "$(ChangeColor blue text) dangling-remove-docker $(ChangeColor green text) Remove dangling images and containers $(ChangeColor white text)" 68 | echo -e "\n" 69 | echo -e "Run $(ChangeColor blue text)create-django-app --help$(ChangeColor white text) to see all options." 70 | echo -e "\n" 71 | echo -e " If you have any problems, do not hesitate to file an issue:" 72 | echo -e " $(ChangeColor blue text)$ISSUE_URL$(ChangeColor white text)" 73 | } 74 | 75 | 76 | function build_docker_shell() { 77 | touch build-docker 78 | echo "#!/bin/bash" >> build-docker 79 | echo "" >> build-docker 80 | echo "echo -e \"Command Created by Munis\"" >> build-docker 81 | echo "echo -e \"Building docker-compose containers ...\"" >> build-docker 82 | echo "docker-compose up --build -d" >> build-docker 83 | echo "echo -e \"Successfuly build .... [OK]\"" >> build-docker 84 | } 85 | 86 | function down_docker_shell() { 87 | touch down-docker 88 | echo "#!/bin/bash" >> down-docker 89 | echo "" >> down-docker 90 | echo "echo -e \"Command Created by Munis\"" >> down-docker 91 | echo "echo -e \"Down all containers .. width docker-composefile\"" >> down-docker 92 | echo "" >> down-docker 93 | echo "docker-compose down" >> down-docker 94 | } 95 | 96 | function connect_docker_shell() { 97 | touch connect-docker 98 | echo "#!/bin/bash" >> connect-docker 99 | echo "" >> connect-docker 100 | echo "echo -e \"Command Created by Munis\"" >> connect-docker 101 | echo "echo -e \"Connecting with \$1 inside \$2 container with docker command \"" >> connect-docker 102 | echo "docker exec -it \$2 \$1" >> connect-docker 103 | } 104 | 105 | 106 | function dangling_remove_docker_shell() { 107 | touch dangling-remove-docker 108 | echo "#!/bin/bash" >> dangling-remove-docker 109 | echo "" >> dangling-remove-docker 110 | echo "echo -e \"Remove all exited images\"" >> dangling-remove-docker 111 | echo "docker rmi \$(docker images -f dangling=true -q)" >> dangling-remove-docker 112 | echo "" >> dangling-remove-docker 113 | echo "echo -e \"Remove all exited containers\"" >> dangling-remove-docker 114 | echo "docker rm \$(docker ps -a -f status=exited -q)" >> dangling-remove-docker 115 | } 116 | 117 | function restart_docker_shell() { 118 | touch restart-docker 119 | echo "#!/bin/bash" >> restart-docker 120 | echo "" >> restart-docker 121 | echo "echo -e \"Command Created by Munis\"" >> restart-docker 122 | echo "echo -e \"Restart all containers\"" >> restart-docker 123 | echo "docker-compose restart" >> restart-docker 124 | echo "" >> restart-docker 125 | } 126 | 127 | function migrate_docker_shell() { 128 | touch migrate-docker 129 | echo "#!/bin/bash" >> migrate-docker 130 | echo "" >> migrate-docker 131 | echo "echo -e \"Command Created by Munis\"" >> migrate-docker 132 | echo "echo -e \"Migrate django project inside \$1 container ...\"" >> migrate-docker 133 | echo "" >> migrate-docker 134 | echo "docker exec -it \$1 sh -c \"/venv/bin/python manage.py migrate\"" >> migrate-docker 135 | echo "" >> migrate-docker 136 | } 137 | 138 | function logs_docker_shell() { 139 | touch logs-docker 140 | echo "#!/bin/bash" >> logs-docker 141 | echo "" >> logs-docker 142 | echo "echo -e \"Command Created by Munis\"" >> logs-docker 143 | echo "echo -e \"Tails all logs on docker containers\"" >> logs-docker 144 | echo "" >> logs-docker 145 | echo "docker-compose logs -f" >> logs-docker 146 | echo "" >> logs-docker 147 | } 148 | 149 | 150 | function backup_database_shell() { 151 | touch backup-database-docker 152 | echo "#!/bin/bash" >> backup-database-docker 153 | echo "" >> backup-database-docker 154 | echo "echo -e \"Command Created by Munis\"" >> backup-database-docker 155 | echo "echo -e \"Backup Posgresql Database\"" >> backup-database-docker 156 | echo "" >> backup-database-docker 157 | echo "if [ \"\$1\" ]; then" >> backup-database-docker 158 | echo " docker exec -t \$1 pg_dumpall -c -U postgres > dump_\`date +%d-%m-%Y\"_\"%H_%M_%S\`.sql" >> backup-database-docker 159 | echo " echo -e \"Successfuly created Dump file name: \$(tput setaf 2 && tput bold)dump_\`date +%d-%m-%Y\"_\"%H_%M_%S\`.sql \"" >> backup-database-docker 160 | echo "else" >> backup-database-docker 161 | echo " echo -e \"Please Select Database Container name for example \"" >> backup-database-docker 162 | echo " echo -e \"backup-database-docker postgres-db \"" >> backup-database-docker 163 | echo "fi" >> backup-database-docker 164 | echo "" >> backup-database-docker 165 | } 166 | 167 | function restore_database_shell() { 168 | touch restore-database-docker 169 | echo "#!/bin/bash" >> restore-database-docker 170 | echo "" >> restore-database-docker 171 | echo "echo -e \"Command Created by Munis\"" >> restore-database-docker 172 | echo "echo -e \"Restore Posgresql Database\"" >> restore-database-docker 173 | echo "" >> restore-database-docker 174 | echo "if [[ \"\$1\" && \"\$2\" ]]; then" >> restore-database-docker 175 | echo " cat \$1 | docker exec -i \$2 psql -U postgres" >> restore-database-docker 176 | echo " echo -e \"Successfuly Restored from dump file name: \$(tput setaf 2 && tput bold)\$1 \"" >> restore-database-docker 177 | echo "else" >> restore-database-docker 178 | echo " echo -e \"Please Select Dump Sql file and Container name for example \"" >> restore-database-docker 179 | echo " echo -e \"restore-database-docker dump_07-04-2018_22_29_33.sql postgres-db \"" >> restore-database-docker 180 | echo "fi" >> restore-database-docker 181 | echo "" >> restore-database-docker 182 | } 183 | 184 | 185 | 186 | if [ "$EUID" -ne 0 ]; then 187 | # If not root user 188 | if [ -d $LOCAL_COMMAND_DIRECTORY ]; then 189 | cd $LOCAL_COMMAND_DIRECTORY 190 | rm -rf ./*-docker 191 | build_docker_shell 192 | down_docker_shell 193 | connect_docker_shell 194 | dangling_remove_docker_shell 195 | restart_docker_shell 196 | migrate_docker_shell 197 | logs_docker_shell 198 | backup_database_shell 199 | restore_database_shell 200 | chmod +x * 201 | else 202 | cd ~ 203 | mkdir .local 204 | cd .local/ 205 | mkdir bin/ 206 | cd $LOCAL_COMMAND_DIRECTORY 207 | build_docker_shell 208 | down_docker_shell 209 | connect_docker_shell 210 | dangling_remove_docker_shell 211 | restart_docker_shell 212 | migrate_docker_shell 213 | logs_docker_shell 214 | backup_database_shell 215 | restore_database_shell 216 | chmod +x * 217 | fi 218 | else 219 | # if user is root 220 | cd $ROOT_COMMAND_DIRECTORY 221 | rm -rf ./*-docker 222 | build_docker_shell 223 | down_docker_shell 224 | connect_docker_shell 225 | dangling_remove_docker_shell 226 | restart_docker_shell 227 | migrate_docker_shell 228 | logs_docker_shell 229 | backup_database_shell 230 | restore_database_shell 231 | chmod +x build-docker 232 | chmod +x down-docker 233 | chmod +x connect-docker 234 | chmod +x dangling-remove-docker 235 | chmod +x restart-docker 236 | chmod +x migrate-docker 237 | chmod +x logs-docker 238 | chmod +x backup-database-docker 239 | chmod +x restore-database-docker 240 | 241 | fi 242 | 243 | 244 | cd $WORKING_DIRECTRY 245 | 246 | rm -rf install.sh 247 | 248 | if [[ "$OSTYPE" == "darwin"* ]]; then 249 | # Detect Operation system is Macbook pro OSX 250 | echo -e "Successfuly installed" 251 | else 252 | $SHELL 253 | fi 254 | 255 | 256 | --------------------------------------------------------------------------------