├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bin └── docker-push-ssh ├── deploy.sh ├── docker_push_ssh ├── __init__.py ├── cli.py └── command.py ├── setup.py └── test.sh /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | .idea/ 3 | build/ 4 | 5 | .DS_Store 6 | *.pyc 7 | *.egg-info 8 | 9 | dist/ 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | language: python 3 | python: 4 | - "2.7" 5 | script: 6 | - bash ./test.sh 7 | services: 8 | - docker -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-push-ssh 2 | [![PyPI version](https://badge.fury.io/py/docker-push-ssh.svg)](https://pypi.org/project/docker-push-ssh) 3 | [![Build Status](https://travis-ci.org/brthor/docker-push-ssh.svg?branch=master)](https://travis-ci.org/brthor/docker-push-ssh) 4 | 5 | Push docker images from your local machine to remote servers without the hassle. 6 | 7 | ## Overview 8 | `docker-push-ssh` is a command line utility to push docker images from your local machine to your remote machine via ssh. 9 | 10 | It creates a private docker registry on your server, establishes a ssh tunnel (so the registry is never exposed to the public), 11 | and uploads your docker image over this ssh tunnel. 12 | 13 | Tested on OS X with "Docker for Mac". 14 | 15 | ## Install 16 | 17 | 1. Install via pip: 18 | `pip install docker-push-ssh` 19 | 20 | 2. Add `localhost:5000` to your docker client's insecure registries (requires restart of docker): 21 | 22 | [[OS X] How to Add Insecure Registry](https://stackoverflow.com/questions/32808215/where-to-set-the-insecure-registry-flag-on-mac-os) 23 | 24 | [[Linux] How to Add Insecure Registry](https://stackoverflow.com/questions/42211380/add-insecure-registry-to-docker) 25 | 26 | 27 | Adding `localhost:5000` to your client's insecure registries is inconvenient but a side-effect of docker's design. 28 | It only needs to be done once from each machine using `docker-push-ssh`. This allows the tool to push through the ssh 29 | tunnel at `localhost:5000` to the temporary registry on your remote host, without needing ssl certificates for your server. 30 | 31 | ## Usage: 32 | 33 | ```bash 34 | $ docker-push-ssh --help 35 | usage: cli.py [-h] [-i SSH_IDENTITY_FILE] [-p SSH_PORT] [-r REGISTRY_PORT] 36 | [--prime-image PRIME_IMAGE] 37 | ssh_host docker_image [docker_image ...] 38 | 39 | A utility to securely push a docker image from your local host to a remote 40 | host over ssh without using docker save/load or needing to setup a private 41 | registry. 42 | 43 | positional arguments: 44 | ssh_host Host to push docker image to. (ex. 45 | username@myhost.com) 46 | docker_image Docker image tag(s) to push. Specify one or more 47 | separated by spaces. 48 | 49 | optional arguments: 50 | -h, --help show this help message and exit 51 | -i SSH_IDENTITY_FILE, --ssh-identity-file SSH_IDENTITY_FILE 52 | [required] Path to the ssh identity file on your local 53 | host. Required, password auth not supported. 54 | -p SSH_PORT, --ssh-port SSH_PORT 55 | [optional] Port on ssh host to connect to. (Default is 56 | 22) 57 | -r REGISTRY_PORT, --registry-port REGISTRY_PORT 58 | [optional] Remote registry port on ssh host to forward 59 | to. (Default is 5000) 60 | --prime-image PRIME_IMAGE 61 | [optional] [list] Base images with which to prime the 62 | registry from the remote host. Docker pull is 63 | performed on the remote host. 64 | ``` 65 | 66 | ## Examples 67 | 68 | First create a test image we can use: 69 | ```bash 70 | $ mkdir /tmp/testimage && cd /tmp/testimage 71 | $ echo "FROM alpine" >> ./Dockerfile 72 | $ echo "RUN touch /etc/testimage" >> ./Dockerfile 73 | $ docker build -t testimage . 74 | ``` 75 | 76 | Now push that test image to our remote server: 77 | ```bash 78 | $ docker-push-ssh -i ~/my_identity_file root@myserver.com testimage 79 | ... 80 | ``` 81 | 82 | Now the `testimage` will be present on your server. 83 | 84 | ## Caveats 85 | 86 | 1. SSH password authentication is not supported. Only key files. 87 | 2. Password-protected SSH keys may not be supported. You can generate a temporary, non-protected key using `ssh-keygen -P "" -f /tmp/my-tmpkey` 88 | -------------------------------------------------------------------------------- /bin/docker-push-ssh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from docker_push_ssh import cli 4 | cli.main() 5 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | pypiUsername="$1" 3 | pypiPassword="$2" 4 | 5 | rm -r dist 6 | 7 | . ./venv/bin/activate 8 | 9 | #pip install twine wheel 10 | python setup.py sdist bdist_wheel 11 | twine upload -u "$1" -p "$2" dist/* -------------------------------------------------------------------------------- /docker_push_ssh/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brthor/docker-push-ssh/30922ddc8dfa2c97558bbf713503601eb65b9670/docker_push_ssh/__init__.py -------------------------------------------------------------------------------- /docker_push_ssh/cli.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, Bryan Thornbury 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import argparse 15 | import os 16 | import socket 17 | import sys 18 | import time 19 | import urllib2 20 | import httplib 21 | 22 | from command import Command 23 | 24 | 25 | def getLocalIp(): 26 | import socket 27 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 28 | s.connect(("8.8.8.8", 80)) 29 | localIp = s.getsockname()[0] 30 | s.close() 31 | 32 | print("[Local IP] " + localIp) 33 | 34 | return localIp 35 | 36 | 37 | def waitForSshTunnelInit(retries=20, delay=1.0): 38 | for _ in range(retries): 39 | time.sleep(delay) 40 | 41 | try: 42 | response = urllib2.urlopen("http://localhost:5000/v2/", timeout=5) 43 | except (socket.error, urllib2.URLError, httplib.BadStatusLine): 44 | continue 45 | 46 | if response.getcode() == 200: 47 | return True 48 | 49 | return False 50 | 51 | 52 | def pushImage(dockerImageTagList, sshHost, sshIdentityFile, sshPort, primeImages, registryPort): 53 | # Setup remote docker registry 54 | print("Setting up secure private registry... ") 55 | registryCommandResult = Command("ssh", [ 56 | "-i", sshIdentityFile, 57 | "-p", sshPort, 58 | "-o", "StrictHostKeyChecking=no", 59 | "-o", "UserKnownHostsFile=/dev/null", 60 | sshHost, 61 | "sh -l -c \"docker run -d -v /etc/docker-push-ssh/registry:/var/lib/registry " + 62 | "--name docker-push-ssh-registry -p 127.0.0.1:{0}:5000 registry\"".format(registryPort) 63 | ]).execute() 64 | 65 | if registryCommandResult.failed(): 66 | print("ERROR") 67 | print(registryCommandResult.stdout) 68 | print(registryCommandResult.stderr) 69 | return False 70 | 71 | try: 72 | # Establish ssh tunnel 73 | print("Establishing SSH Tunnel...") 74 | 75 | sshTunnelCommandResult = Command("docker", [ 76 | "run", "-d", 77 | "--name", "docker-push-ssh-tunnel", 78 | "-p", "127.0.0.1:5000:5000", 79 | "-v", "{0}:/etc/ssh_key_file".format(sshIdentityFile), 80 | "brthornbury/docker-alpine-ssh", 81 | "ssh", 82 | "-N", 83 | "-L", "*:5000:localhost:{0}".format(registryPort), 84 | "-i", "/etc/ssh_key_file", 85 | "-o", "StrictHostKeyChecking=no", 86 | "-o", "UserKnownHostsFile=/dev/null", 87 | "-p", sshPort, 88 | sshHost 89 | ]).environment_dict(os.environ).execute() 90 | 91 | if sshTunnelCommandResult.failed(): 92 | print("ERROR") 93 | print(sshTunnelCommandResult.stdout) 94 | print(sshTunnelCommandResult.stderr) 95 | return False 96 | 97 | print("Waiting for SSH Tunnel Initialization...") 98 | 99 | if not waitForSshTunnelInit(): 100 | print("ERROR") 101 | print("SSH Tunnel failed to initialize.") 102 | 103 | logsCmd = Command("docker", ["logs", "docker-push-ssh-tunnel"]).environment_dict(os.environ).execute() 104 | print(logsCmd.stdout, logsCmd.stderr) 105 | return False 106 | 107 | if sshTunnelCommandResult.failed(): 108 | print("ERROR") 109 | print(sshTunnelCommandResult.stdout) 110 | print(sshTunnelCommandResult.stderr) 111 | return False 112 | 113 | print("Priming Registry with base images...") 114 | for primeImage in (primeImages or []): 115 | 116 | print("Priming base image ({0})".format(primeImage)) 117 | 118 | primingCommand = Command("ssh", [ 119 | "-i", sshIdentityFile, 120 | "-p", sshPort, 121 | "-o", "StrictHostKeyChecking=no", 122 | "-o", "UserKnownHostsFile=/dev/null", 123 | sshHost, 124 | "sh -l -c \"docker pull {0}".format(primeImage) + 125 | " && docker tag {0} localhost:{1}/{0} && docker push localhost:{1}/{0}\"".format(primeImage, registryPort) 126 | ]).execute() 127 | 128 | if primingCommand.failed(): 129 | print("ERROR") 130 | print(primingCommand.stdout) 131 | print(primingCommand.stderr) 132 | return False 133 | 134 | print("Tagging image(s) for push...") 135 | for dockerImageTag in dockerImageTagList: 136 | tagCommandResult = Command("docker", [ 137 | "tag", 138 | dockerImageTag, 139 | "localhost:5000/{0}".format(dockerImageTag) 140 | ]).environment_dict(os.environ).execute() 141 | 142 | if tagCommandResult.failed(): 143 | print("ERROR") 144 | print(tagCommandResult.stdout) 145 | print(tagCommandResult.stderr) 146 | return False 147 | 148 | print("Pushing Image(s) from local host...") 149 | for dockerImageTag in dockerImageTagList: 150 | pushDockerImageCommandResult = Command("docker", [ 151 | "push", 152 | "localhost:5000/{0}".format(dockerImageTag) 153 | ]).environment_dict(os.environ).execute() 154 | 155 | if pushDockerImageCommandResult.failed(): 156 | print("ERROR") 157 | 158 | print(pushDockerImageCommandResult.stdout) 159 | print(pushDockerImageCommandResult.stderr) 160 | 161 | print("Error Pushing Image: Ensure localhost:5000 is added to your insecure registries.") 162 | print("More Details (OS X): " 163 | "https://stackoverflow.com/questions/32808215/where-to-set-the-insecure-registry-flag-on-mac-os") 164 | return False 165 | 166 | print("Pushed Image {0} Successfully...".format(dockerImageTag)) 167 | 168 | print("Pulling and Retagging Image on remote host...") 169 | for dockerImageTag in dockerImageTagList: 170 | pullDockerImageCommandResult = Command("ssh", [ 171 | "-i", sshIdentityFile, 172 | "-p", sshPort, 173 | "-o", "StrictHostKeyChecking=no", 174 | "-o", "UserKnownHostsFile=/dev/null", 175 | sshHost, 176 | "sh -l -c \"docker pull " + "localhost:{1}/{0}".format(dockerImageTag, registryPort) + 177 | " && docker tag localhost:{1}/{0} {0}\"".format(dockerImageTag, registryPort) 178 | ]).execute() 179 | 180 | if pullDockerImageCommandResult.failed(): 181 | print("ERROR") 182 | print(pullDockerImageCommandResult.stdout) 183 | print(pullDockerImageCommandResult.stderr) 184 | return False 185 | 186 | print("Pulled Image {0} Successfully...".format(dockerImageTag)) 187 | 188 | finally: 189 | print("Cleaning up...") 190 | Command("ssh", [ 191 | "-i", sshIdentityFile, 192 | "-p", sshPort, 193 | "-o", "StrictHostKeyChecking=no", 194 | "-o", "UserKnownHostsFile=/dev/null", 195 | sshHost, 196 | "sh -l -c \"docker rm -f docker-push-ssh-registry\"" 197 | ]).execute() 198 | 199 | Command("docker", [ 200 | "rm", "-f", "docker-push-ssh-tunnel" 201 | ]).environment_dict(os.environ).execute() 202 | 203 | for dockerImageTag in dockerImageTagList: 204 | Command("docker", [ 205 | "image", "rm", 206 | "localhost:5000/{0}".format(dockerImageTag) 207 | ]).environment_dict(os.environ).execute() 208 | 209 | return True 210 | 211 | 212 | def main(): 213 | parser = argparse.ArgumentParser(description="A utility to securely push a docker image from your local host to a " 214 | "remote host over ssh without using docker save/load or needing to " 215 | "setup a private registry.") 216 | 217 | parser.add_argument("ssh_host", help="Host to push docker image to. (ex. username@myhost.com)") 218 | 219 | parser.add_argument("docker_image", nargs='+', 220 | help="Docker image tag(s) to push. Specify one or more separated by spaces.") 221 | 222 | parser.add_argument("-i", "--ssh-identity-file", type=str, 223 | help="[required] Path to the ssh identity file on your local host. " 224 | "Required, password auth not supported. (ex. ~/.ssh/id_rsa)") 225 | 226 | parser.add_argument("-p", "--ssh-port", type=str, help="[optional] Port on ssh host to connect to. (Default is 22)", default="22") 227 | 228 | parser.add_argument("-r", "--registry-port", type=str, 229 | help="[optional] Remote registry port on ssh host to forward to. (Default is 5000)", default="5000") 230 | 231 | parser.add_argument("--prime-image", help="[optional] [list] Base images with which to prime the registry from the remote host. Docker pull is performed on the remote host.", action="append") 232 | 233 | args = parser.parse_args() 234 | 235 | assert args.ssh_identity_file is not None 236 | 237 | sshIdentityFileAbsolutePath = os.path.abspath(os.path.expanduser(args.ssh_identity_file)) 238 | 239 | print("[REQUIRED] Ensure localhost:5000 is added to your insecure registries.") 240 | 241 | success = pushImage(args.docker_image, args.ssh_host, sshIdentityFileAbsolutePath, 242 | args.ssh_port, args.prime_image, args.registry_port) 243 | 244 | if not success: 245 | sys.exit(1) 246 | 247 | 248 | if __name__ == "__main__": 249 | main() 250 | -------------------------------------------------------------------------------- /docker_push_ssh/command.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, Bryan Thornbury 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import subprocess 16 | 17 | 18 | class Command(object): 19 | executable = None 20 | arguments = None 21 | 22 | def __init__(self, executable, arguments): 23 | """ 24 | :type executable: str 25 | :type arguments: list[str] 26 | :param executable: 27 | :param arguments: 28 | :return: 29 | """ 30 | 31 | self.executable = executable 32 | self.arguments = arguments 33 | 34 | self.environment_variables = dict() 35 | 36 | def environment(self, key, value): 37 | self.environment_variables[key] = value 38 | return self 39 | 40 | def environment_dict(self, env_dict): 41 | for key, value in env_dict.iteritems(): 42 | self.environment_variables[key] = value 43 | return self 44 | 45 | def execute(self, waitForExit=True): 46 | s_process_args = [self.executable] 47 | s_process_args.extend(self.arguments) 48 | 49 | s_process = subprocess.Popen( 50 | s_process_args, 51 | stdout=subprocess.PIPE, 52 | stderr=subprocess.PIPE, 53 | env=self.environment_variables) 54 | 55 | if waitForExit: 56 | out, err = s_process.communicate() 57 | exitcode = s_process.returncode 58 | else: 59 | out, err, exitcode = None, None, None 60 | 61 | # print out 62 | # print err 63 | 64 | return ProcessResult(self, out, err, exitcode, s_process.pid) 65 | 66 | 67 | class ProcessResult(object): 68 | """ 69 | :type process: Command 70 | :type s_process: subprocess.Popen 71 | :type stdout: str 72 | :type std 73 | """ 74 | 75 | def __init__(self, process, stdout, stderr, exitcode, pid): 76 | self.process = process 77 | self.stdout = stdout 78 | self.stderr = stderr 79 | self.exitcode = exitcode 80 | self.pid = pid 81 | 82 | def failed(self): 83 | return self.exitcode != 0 84 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md", "r") as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name="docker-push-ssh", 8 | version="0.1.8", 9 | author="Bryan Thornbury", 10 | author_email="", 11 | description="Push local docker images to your remote servers via ssh without the hassle.", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/brthor/docker-push-ssh", 15 | packages=setuptools.find_packages(), 16 | classifiers=[ 17 | "Programming Language :: Python :: 2.7", 18 | "License :: OSI Approved :: Apache Software License", 19 | "Operating System :: OS Independent", 20 | "Intended Audience :: Developers", 21 | "Intended Audience :: System Administrators", 22 | "Topic :: Software Development :: Build Tools", 23 | "Topic :: Utilities" 24 | ], 25 | scripts=[ 26 | "bin/docker-push-ssh" 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | set -x 5 | 6 | echo '{"insecure-registries": ["localhost:5000"]}' | sudo tee /etc/docker/daemon.json 7 | sudo service docker stop 8 | sudo service docker start 9 | 10 | docker run --name docker-dind-sshd --privileged -d brthornbury/docker-dind-sshd --storage-driver=overlay 11 | hostIp=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' docker-dind-sshd) 12 | echo $hostIp 13 | 14 | python setup.py sdist 15 | pip install dist/*.tar.gz 16 | 17 | # Create Test Images 18 | image1="a84b42fbe17b4a87b032c45f3c8c74e3" 19 | mkdir /tmp/${image1} 20 | (cd /tmp/${image1} \ 21 | && echo "FROM alpine" >> ./Dockerfile \ 22 | && echo "RUN touch /etc/${image1}" >> ./Dockerfile \ 23 | && echo "CMD echo out-${image1}" >> ./Dockerfile \ 24 | && docker build -t ${image1} .) 25 | 26 | image2="e294fca67f674bda84013057fd48fb62" 27 | mkdir /tmp/${image2} 28 | (cd /tmp/${image2} \ 29 | && echo "FROM alpine" >> ./Dockerfile \ 30 | && echo "RUN touch /etc/${image2}" >> ./Dockerfile \ 31 | && echo "CMD echo out-${image2}" >> ./Dockerfile \ 32 | && docker build -t ${image2} .) 33 | 34 | echo "" > ./emptykey 35 | 36 | set +e 37 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 22 root@${hostIp} 'sh -l -c "docker run "'"${image1}" 38 | failResult1="$?" 39 | 40 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 22 root@${hostIp} 'sh -l -c "docker run "'"${image2}" 41 | failResult2="$?" 42 | set -e 43 | 44 | [ "$failResult1" != "0" ] 45 | [ "$failResult2" != "0" ] 46 | 47 | docker-push-ssh --prime-image alpine -i ./emptykey -p 22 root@${hostIp} ${image1} 48 | docker-push-ssh --prime-image alpine -r 5002 -i ./emptykey -p 22 root@${hostIp} ${image2} 49 | 50 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 22 root@${hostIp} 'sh -l -c "docker run "'"${image1}" 51 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 22 root@${hostIp} 'sh -l -c "docker run "'"${image2}" 52 | --------------------------------------------------------------------------------