├── .eslintignore ├── .eslintrc ├── .github ├── FUNDING.yml ├── stale.yml └── workflows │ └── main.yml ├── .gitignore ├── .npmignore ├── .travis.yml ├── .vscode └── launch.json ├── LICENSE ├── README.md ├── Vagrantfile ├── examples ├── build │ ├── Dockerfile │ ├── Dockerfile.tar │ ├── build_img_cwd.js │ ├── build_img_files.js │ └── run.js ├── duplexstreams.js ├── exec_running_container.js ├── external_volume.js ├── listContainers.js ├── logs.js ├── run.js ├── run_stdin.js ├── timeout.js └── top.js ├── lib ├── config.js ├── container.js ├── docker.js ├── exec.js ├── image.js ├── network.js ├── node.js ├── plugin.js ├── proto │ └── auth.proto ├── secret.js ├── service.js ├── session.js ├── task.js ├── util.js └── volume.js ├── package-lock.json ├── package.json └── test ├── .dockerignore ├── Dockerfile ├── buildkit.Dockerfile ├── container.js ├── docker.js ├── empty.tar ├── fixtures └── dockerignore │ ├── .dockerignore │ ├── Dockerfile │ ├── MC-hammer.txt │ ├── empty.txt │ └── ignore-dir │ ├── .gitkeep │ └── empty.txt ├── image.js ├── networks.js ├── plugin.js ├── promises.js ├── spec_helper.js ├── swarm.js ├── test-load.tar ├── test.tar └── util.js /.eslintignore: -------------------------------------------------------------------------------- 1 | # 3rd-party files 2 | node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "mocha": true, 4 | "node": true 5 | }, 6 | "rules": { 7 | /* Possible Errors */ 8 | "no-extra-parens": 2, 9 | "valid-jsdoc": 2, 10 | /* Best Practices */ 11 | "block-scoped-var": 2, 12 | "complexity": [1, 3], 13 | "default-case": 2, 14 | "dot-notation": [2], 15 | "guard-for-in": 2, 16 | "no-div-regex": 2, 17 | "no-else-return": 2, 18 | "no-eq-null": 2, 19 | "no-floating-decimal": 2, 20 | "no-process-env": 2, 21 | "no-self-compare": 2, 22 | "no-void": 2, 23 | "no-warning-comments": 2, 24 | "radix": 2, 25 | "vars-on-top": 2, 26 | "wrap-iife": 2, 27 | /* Variables */ 28 | "no-catch-shadow": 2, 29 | /* Node.JS */ 30 | "no-sync": 2, 31 | /* Stylistic Issues */ 32 | "no-mixed-spaces-and-tabs": 2, 33 | "no-underscore-dangle": 2, 34 | "quotes": [1, "single", "avoid-escape"] 35 | }} 36 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: apocas 2 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 550 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - question 8 | - bug 9 | - enhancement 10 | - "needs love" 11 | - "needs inspection" 12 | 13 | # Label to use when marking an issue as stale 14 | staleLabel: stale 15 | # Comment to post when marking an issue as stale. Set to `false` to disable 16 | markComment: > 17 | This issue has been automatically marked as stale because it has not had 18 | recent activity. It will be closed if no further activity occurs. Open a new issue if needed. 19 | # Comment to post when closing a stale issue. Set to `false` to disable 20 | closeComment: false -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | workflow_dispatch: 7 | 8 | jobs: 9 | build_nodejs: 10 | runs-on: ubuntu-latest 11 | 12 | strategy: 13 | matrix: 14 | node-version: [14.x, 16.x, 18.x, 20.x, 22.x] 15 | 16 | steps: 17 | - uses: actions/checkout@v3 18 | 19 | - name: Use Node.js ${{ matrix.node-version }} 20 | uses: actions/setup-node@v3 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | 24 | - name: Provisioning 25 | run: | 26 | docker --version 27 | node -v 28 | docker pull ubuntu 29 | 30 | - name: NPM install 31 | run: npm install 32 | 33 | - name: NPM test 34 | run: npm test 35 | 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | node_modules 3 | npm-debug.log 4 | *~ 5 | *.log 6 | .idea 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | Vagrantfile 2 | cookbooks 3 | .vagrant 4 | test 5 | tools 6 | examples 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | language: node_js 4 | 5 | node_js: 6 | - "8" 7 | 8 | before_script: 9 | - docker pull ubuntu 10 | 11 | services: 12 | - docker 13 | 14 | before_install: 15 | - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 16 | - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" 17 | - sudo apt-get update 18 | - sudo apt-get -y install docker-ce 19 | 20 | install: 21 | - npm install 22 | 23 | script: 24 | - npm test 25 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "args": [ 9 | "--colors", 10 | "${workspaceFolder}/test", 11 | "-R", 12 | "spec", 13 | "--exit" 14 | ], 15 | "internalConsoleOptions": "openOnSessionStart", 16 | "name": "Mocha Tests", 17 | "program": "./node_modules/mocha/bin/mocha.js", 18 | "request": "launch", 19 | "skipFiles": [ 20 | "/**" 21 | ], 22 | "type": "node" 23 | } 24 | ] 25 | } -------------------------------------------------------------------------------- /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 | # dockerode 2 | 3 | Not another Node.js Docker Remote API module. 4 | 5 | `dockerode` objectives: 6 | 7 | * **streams** - `dockerode` does NOT break any stream, it passes them to you allowing for some stream voodoo. 8 | * **stream demux** - Supports optional [stream demultiplexing](https://github.com/apocas/dockerode#helper-functions). 9 | * **entities** - containers, images and execs are defined entities and not random static methods. 10 | * **run** - `dockerode` allow you to seamless run commands in a container aka `docker run`. 11 | * **tests** - `dockerode` really aims to have a good test set, allowing to follow `Docker` changes easily, quickly and painlessly. 12 | * **feature-rich** - There's a real effort in keeping **All** `Docker` Remote API features implemented and tested. 13 | * **interfaces** - Features **callback** and **promise** based interfaces, making everyone happy :) 14 | 15 | ## Ecosystem 16 | 17 | * docker-modem [https://github.com/apocas/docker-modem](https://github.com/apocas/docker-modem) - Docker's API network stack 18 | * dockerode-compose [https://github.com/apocas/dockerode-compose](https://github.com/apocas/dockerode-compose) - docker-compose in Node.js 19 | 20 | ## Installation 21 | 22 | `npm install dockerode` 23 | 24 | ## Usage 25 | 26 | * Input options are directly passed to Docker. Check [Docker API documentation](https://docs.docker.com/engine/api/latest/) for more details. 27 | * Return values are unchanged from Docker, official Docker documentation will also apply to them. 28 | * Check the tests and examples folder for more examples. 29 | 30 | ### Getting started 31 | 32 | To use `dockerode` first you need to instantiate it: 33 | 34 | ``` js 35 | var Docker = require('dockerode'); 36 | var docker = new Docker({socketPath: '/var/run/docker.sock'}); 37 | var docker1 = new Docker(); //defaults to above if env variables are not used 38 | var docker2 = new Docker({host: 'http://192.168.1.10', port: 3000}); 39 | var docker3 = new Docker({protocol:'http', host: '127.0.0.1', port: 3000}); 40 | var docker4 = new Docker({host: '127.0.0.1', port: 3000}); //defaults to http 41 | 42 | //protocol http vs https is automatically detected 43 | var docker5 = new Docker({ 44 | host: '192.168.1.10', 45 | port: process.env.DOCKER_PORT || 2375, 46 | ca: fs.readFileSync('ca.pem'), 47 | cert: fs.readFileSync('cert.pem'), 48 | key: fs.readFileSync('key.pem'), 49 | version: 'v1.25' // required when Docker >= v1.13, https://docs.docker.com/engine/api/version-history/ 50 | }); 51 | 52 | var docker6 = new Docker({ 53 | protocol: 'https', //you can enforce a protocol 54 | host: '192.168.1.10', 55 | port: process.env.DOCKER_PORT || 2375, 56 | ca: fs.readFileSync('ca.pem'), 57 | cert: fs.readFileSync('cert.pem'), 58 | key: fs.readFileSync('key.pem') 59 | }); 60 | 61 | //using a different promise library (default is the native one) 62 | var docker7 = new Docker({ 63 | Promise: require('bluebird') 64 | //... 65 | }); 66 | //... 67 | ``` 68 | 69 | ### Manipulating a container: 70 | 71 | ``` js 72 | // create a container entity. does not query API 73 | var container = docker.getContainer('71501a8ab0f8'); 74 | 75 | // query API for container info 76 | container.inspect(function (err, data) { 77 | console.log(data); 78 | }); 79 | 80 | container.start(function (err, data) { 81 | console.log(data); 82 | }); 83 | 84 | container.remove(function (err, data) { 85 | console.log(data); 86 | }); 87 | 88 | // promises are supported 89 | var auxContainer; 90 | docker.createContainer({ 91 | Image: 'ubuntu', 92 | AttachStdin: false, 93 | AttachStdout: true, 94 | AttachStderr: true, 95 | Tty: true, 96 | Cmd: ['/bin/bash', '-c', 'tail -f /var/log/dmesg'], 97 | OpenStdin: false, 98 | StdinOnce: false 99 | }).then(function(container) { 100 | auxContainer = container; 101 | return auxContainer.start(); 102 | }).then(function(data) { 103 | return auxContainer.resize({ 104 | h: process.stdout.rows, 105 | w: process.stdout.columns 106 | }); 107 | }).then(function(data) { 108 | return auxContainer.stop(); 109 | }).then(function(data) { 110 | return auxContainer.remove(); 111 | }).then(function(data) { 112 | console.log('container removed'); 113 | }).catch(function(err) { 114 | console.log(err); 115 | }); 116 | ``` 117 | 118 | You may also specify default options for each container's operations, which will always be used for the specified container and operation. 119 | 120 | ``` js 121 | container.defaultOptions.start.Binds = ["/tmp:/tmp:rw"]; 122 | ``` 123 | 124 | ### Stopping all containers on a host 125 | 126 | ``` js 127 | docker.listContainers(function (err, containers) { 128 | containers.forEach(function (containerInfo) { 129 | docker.getContainer(containerInfo.Id).stop(cb); 130 | }); 131 | }); 132 | ``` 133 | 134 | ### Building an Image 135 | Context: provides the path to the Dockerfile. Additionaly files that are involved in the build *must* be explicitly mentioned in src array, since they are sent to a temp env to build. Example: file for COPY command are extracted from that temporary environment. 136 | 137 | ``` js 138 | docker.buildImage('archive.tar', {t: imageName}, function (err, response){ 139 | //... 140 | }); 141 | 142 | docker.buildImage({ 143 | context: __dirname, 144 | src: ['Dockerfile', 'file1', 'file2'] 145 | }, {t: imageName}, function (err, response) { 146 | //... 147 | }); 148 | ``` 149 | 150 | `buildImage` returns a Promise of NodeJS stream. In case you want to find out when the build has finished, you must follow the progress of the build with the `modem` instance in dockerode: 151 | 152 | ``` js 153 | let dockerode = new Dockerode(); 154 | let stream = await dockerode.buildImage(...); 155 | await new Promise((resolve, reject) => { 156 | dockerode.modem.followProgress(stream, (err, res) => err ? reject(err) : resolve(res)); 157 | }); 158 | // Build has finished 159 | ``` 160 | 161 | 162 | ### Creating a container: 163 | 164 | ``` js 165 | docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash'], name: 'ubuntu-test'}, function (err, container) { 166 | container.start(function (err, data) { 167 | //... 168 | }); 169 | }); 170 | //... 171 | ``` 172 | 173 | ### Streams goodness: 174 | 175 | ``` js 176 | //tty:true 177 | docker.createContainer({ /*...*/ Tty: true /*...*/ }, function(err, container) { 178 | 179 | /* ... */ 180 | 181 | container.attach({stream: true, stdout: true, stderr: true}, function (err, stream) { 182 | stream.pipe(process.stdout); 183 | }); 184 | 185 | /* ... */ 186 | }); 187 | 188 | //tty:false 189 | docker.createContainer({ /*...*/ Tty: false /*...*/ }, function(err, container) { 190 | 191 | /* ... */ 192 | 193 | container.attach({stream: true, stdout: true, stderr: true}, function (err, stream) { 194 | //dockerode may demultiplex attach streams for you :) 195 | container.modem.demuxStream(stream, process.stdout, process.stderr); 196 | }); 197 | 198 | /* ... */ 199 | }); 200 | 201 | docker.createImage({fromImage: 'ubuntu'}, function (err, stream) { 202 | stream.pipe(process.stdout); 203 | }); 204 | 205 | //... 206 | ``` 207 | 208 | There is also support for [HTTP connection hijacking](https://docs.docker.com/engine/api/v1.45/#tag/Container/operation/ContainerAttach), 209 | which allows for cleaner interactions with commands that work with stdin and stdout separately. 210 | 211 | ```js 212 | docker.createContainer({Tty: false, /*... other options */}, function(err, container) { 213 | container.start(function(err) { 214 | container.exec({Cmd: ['shasum', '-'], AttachStdin: true, AttachStdout: true}, function(err, exec) { 215 | exec.start({hijack: true, stdin: true}, function(err, stream) { 216 | // shasum can't finish until after its stdin has been closed, telling it that it has 217 | // read all the bytes it needs to sum. Without a socket upgrade, there is no way to 218 | // close the write-side of the stream without also closing the read-side! 219 | fs.createReadStream('node-v5.1.0.tgz', 'binary').pipe(stream); 220 | 221 | // Fortunately, we have a regular TCP socket now, so when the readstream finishes and closes our 222 | // stream, it is still open for reading and we will still get our results :-) 223 | docker.modem.demuxStream(stream, process.stdout, process.stderr); 224 | }); 225 | }); 226 | }); 227 | }); 228 | ``` 229 | 230 | ### Equivalent of `docker run` in `dockerode`: 231 | 232 | * `image` - container image 233 | * `cmd` - command to be executed 234 | * `stream` - stream(s) which will be used for execution output. 235 | * `create_options` - (optional) Options used for container creation. Refer to the [DockerEngine ContainerCreate documentation](https://docs.docker.com/engine/api/v1.37/#operation/ContainerCreate) for the possible values 236 | * `start_options` - (optional) Options used for container start. Refer to the [DockerEngine ContainerStart documentation](https://docs.docker.com/engine/api/v1.37/#operation/ContainerStart) for the possible values 237 | * `callback` - callback called when execution ends (optional, promise will be returned if not used). 238 | 239 | ``` js 240 | //callback 241 | docker.run('ubuntu', ['bash', '-c', 'uname -a'], process.stdout, function (err, data, container) { 242 | console.log(data.StatusCode); 243 | }); 244 | 245 | //promise 246 | docker.run(testImage, ['bash', '-c', 'uname -a'], process.stdout).then(function(data) { 247 | var output = data[0]; 248 | var container = data[1]; 249 | console.log(output.StatusCode); 250 | return container.remove(); 251 | }).then(function(data) { 252 | console.log('container removed'); 253 | }).catch(function(err) { 254 | console.log(err); 255 | }); 256 | ``` 257 | 258 | or, if you want to split stdout and stderr (you must to pass `Tty:false` as an option for this to work) 259 | 260 | ``` js 261 | docker.run('ubuntu', ['bash', '-c', 'uname -a'], [process.stdout, process.stderr], {Tty:false}, function (err, data, container) { 262 | console.log(data.StatusCode); 263 | }); 264 | ``` 265 | 266 | If you provide a callback, `run` will return an EventEmitter supporting the following events: container, stream, data. 267 | If a callback isn't provided a promise will be returned. 268 | 269 | ``` js 270 | docker.run('ubuntu', ['bash', '-c', 'uname -a'], [process.stdout, process.stderr], {Tty:false}, function (err, data, container) { 271 | //... 272 | }).on('container', function (container) { 273 | //... 274 | }); 275 | ``` 276 | 277 | And here is one more complex example using auto-remove and Docker network. 278 | 279 | ``` js 280 | docker.run('some-python-image', ['python', 'main.py', arg], process.stdout, {name: 'my-python-container', HostConfig: { AutoRemove: true, NetworkMode: 'my_network'}}, function(err, data, container) { 281 | // Do stuff 282 | }); 283 | ``` 284 | 285 | ### Equivalent of `docker pull` in `dockerode`: 286 | 287 | * `repoTag` - container image name (optionally with tag) 288 | `myrepo/myname:withtag` 289 | * `options` - extra options passed to create image. 290 | * `callback` - callback called when execution ends. 291 | 292 | ``` js 293 | docker.pull('myrepo/myname:tag', function (err, stream) { 294 | // streaming output from pull... 295 | }); 296 | ``` 297 | 298 | #### Pull from private repos 299 | 300 | `docker-modem` already base64 encodes the necessary auth object for you. 301 | 302 | ``` js 303 | var auth = { 304 | username: 'username', 305 | password: 'password', 306 | auth: '', 307 | email: 'your@email.email', 308 | serveraddress: 'https://index.docker.io/v1' 309 | }; 310 | 311 | docker.pull('tag', {'authconfig': auth}, function (err, stream) { 312 | //... 313 | }); 314 | ``` 315 | 316 | If you already have a base64 encoded auth object, you can use it directly: 317 | 318 | ```js 319 | var auth = { key: 'yJ1J2ZXJhZGRyZXNzIjoitZSI6Im4OCIsImF1dGgiOiIiLCJlbWFpbCI6ImZvbGllLmFkcmc2VybmF0iLCJzZX5jb2aHR0cHM6Ly9pbmRleC5kb2NrZXIuaW8vdZvbGllYSIsInBhc3N3b3JkIjoiRGVjZW1icmUjEvIn0=' } 320 | ``` 321 | 322 | 323 | ## Helper functions 324 | 325 | * `followProgress` - allows to fire a callback only in the end of a stream based process. (build, pull, ...) 326 | 327 | ``` js 328 | //followProgress(stream, onFinished, [onProgress]) 329 | docker.pull(repoTag, function(err, stream) { 330 | //... 331 | docker.modem.followProgress(stream, onFinished, onProgress); 332 | 333 | function onFinished(err, output) { 334 | //output is an array with output json parsed objects 335 | //... 336 | } 337 | function onProgress(event) { 338 | //... 339 | } 340 | }); 341 | ``` 342 | 343 | * `demuxStream` - demux stdout and stderr 344 | 345 | ``` js 346 | //demuxStream(stream, stdout, stderr) 347 | container.attach({ 348 | stream: true, 349 | stdout: true, 350 | stderr: true 351 | }, function handler(err, stream) { 352 | //... 353 | container.modem.demuxStream(stream, process.stdout, process.stderr); 354 | //... 355 | }); 356 | ``` 357 | 358 | ## Sponsors 359 | 360 | Amazing entities that [sponsor](https://github.com/sponsors/apocas) my open-source work. Check them out! 361 | 362 | [![HTTP Toolkit](https://avatars.githubusercontent.com/u/39777515?s=100)](https://github.com/httptoolkit) 363 | 364 | ## Documentation 365 | 366 | ### Docker 367 | 368 | - docker.createContainer(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerCreate) 369 | - docker.createImage([auth], options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageCreate) 370 | - docker.loadImage(file, options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageLoad) 371 | - docker.importImage(file, options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageCreate) 372 | - docker.buildImage(file, options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageBuild) 373 | - docker.checkAuth(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SystemAuth) 374 | - docker.getContainer(id) - Returns a Container object. 375 | - docker.getImage(name) - Returns an Image object. 376 | - docker.getVolume(name) - Returns a Volume object. 377 | - docker.getPlugin(name) - Returns a Plugin object. 378 | - docker.getService(id) - Returns a Service object. 379 | - docker.getTask(id) - Returns a Task object. 380 | - docker.getNode(id) - Returns a Node object. 381 | - docker.getNetwork(id) - Returns a Network object. 382 | - docker.getSecret(id) - Returns a Secret object. 383 | - docker.getConfig(id) - Returns a Config object. 384 | - docker.getExec(id) - Returns a Exec object. 385 | - docker.listContainers(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerList) 386 | - docker.listImages(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageList) 387 | - docker.listServices(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ServiceList) 388 | - docker.listNodes(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NodeList) 389 | - docker.listTasks(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/TaskList) 390 | - docker.listSecrets(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SecretList) 391 | - docker.listConfigs(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ConfigList) 392 | - docker.listPlugins(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginList) 393 | - docker.listVolumes(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/VolumeList) 394 | - docker.listNetworks(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkList) 395 | - docker.createSecret(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SecretCreate) 396 | - docker.createConfig(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ConfigCreate) 397 | - docker.createPlugin(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginCreate) 398 | - docker.createVolume(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/VolumeCreate) 399 | - docker.createService(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ServiceCreate) 400 | - docker.createNetwork(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkCreate) 401 | - docker.pruneImages(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImagePrune) 402 | - docker.pruneBuilder() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/BuildPrune) 403 | - docker.pruneContainers(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerPrune) 404 | - docker.pruneVolumes(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/VolumePrune) 405 | - docker.pruneNetworks(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkPrune) 406 | - docker.searchImages(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageSearch) 407 | - docker.info() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SystemInfo) 408 | - docker.version() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SystemVersion) 409 | - docker.ping() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SystemPing) 410 | - docker.df() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SystemDataUsage) 411 | - docker.getEvents(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SystemEvents) 412 | - docker.swarmInit(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SwarmInit) 413 | - docker.swarmJoin(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SwarmJoin) 414 | - docker.swarmLeave(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SwarmLeave) 415 | - docker.swarmUpdate(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SwarmUpdate) 416 | - docker.swarmInspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SwarmInspect) 417 | - docker.pull(repoTag, options, callback, auth) - Like Docker's CLI pull 418 | - docker.pullAll(repoTag, options, callback, auth) - Like Docker's CLI pull with "-a" 419 | - docker.run(image, cmd, stream, createOptions, startOptions) - Like Docker's CLI run 420 | 421 | 422 | ### Container 423 | 424 | - container.inspect(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerInspect) 425 | - container.rename(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerRename) 426 | - container.update(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerUpdate) 427 | - container.top(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerTop) 428 | - container.changes() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerChanges) 429 | - container.export() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerExport) 430 | - container.start(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerStart) 431 | - container.stop(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerStop) 432 | - container.pause(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerPause) 433 | - container.unpause(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerUnpause) 434 | - container.exec(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerExec) 435 | - container.commit(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageCommit) 436 | - container.restart(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerRestart) 437 | - container.kill(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerKill) 438 | - container.resize(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerResize) 439 | - container.attach(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerAttach) 440 | - container.wait(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerWait) 441 | - container.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerDelete) 442 | - container.getArchive(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerArchive) 443 | - container.infoArchive(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerArchiveInfo) 444 | - container.putArchive(file, options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PutContainerArchive) 445 | - container.logs(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerLogs) 446 | - container.stats(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ContainerStats) 447 | 448 | ### Exec 449 | 450 | - exec.start(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ExecStart) 451 | - exec.resize(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ExecResize) 452 | - exec.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ExecInspect) 453 | 454 | ### Image 455 | 456 | - image.inspect(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.48/#tag/Image/operation/ImageInspect) 457 | - image.history() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageHistory) 458 | - image.push(options, callback, auth) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImagePush) 459 | - image.tag(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageTag) 460 | - image.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageDelete) 461 | - image.get() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ImageGet) 462 | 463 | ### Network 464 | 465 | - network.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkInspect) 466 | - network.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkDelete) 467 | - network.connect(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkConnect) 468 | - network.disconnect(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NetworkDisconnect) 469 | 470 | ### Node 471 | 472 | - node.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NodeInspect) 473 | - node.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NodeDelete) 474 | - node.update(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/NodeUpdate) 475 | 476 | ### Plugin 477 | 478 | - plugin.privileges() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/GetPluginPrivileges) 479 | - plugin.pull(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginPull) 480 | - plugin.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginInspect) 481 | - plugin.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginDelete) 482 | - plugin.enable(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginEnable) 483 | - plugin.disable(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginDisable) 484 | - plugin.update([auth], options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginUpgrade) 485 | - plugin.push(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginPush) 486 | - plugin.configure(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/PluginSet) 487 | 488 | ### Secret 489 | 490 | - secret.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SecretInspect) 491 | - secret.remove() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SecretDelete) 492 | - secret.update(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/SecretUpdate) 493 | 494 | ### Service 495 | 496 | - service.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ServiceInspect) 497 | - service.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ServiceDelete) 498 | - service.update(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ServiceUpdate) 499 | - service.logs(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/ServiceLogs) 500 | 501 | ### Task 502 | 503 | - task.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/TaskInspect) 504 | - task.logs(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/Session) 505 | 506 | ### Volume 507 | 508 | - volume.inspect() - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/VolumeInspect) 509 | - volume.remove(options) - [Docker API Endpoint](https://docs.docker.com/engine/api/v1.37/#operation/VolumeDelete) 510 | 511 | 512 | ## Tests 513 | 514 | * `docker pull ubuntu:latest` to prepare your system for the tests. 515 | * Tests are implemented using `mocha` and `chai`. Run them with `npm test`. 516 | 517 | ## Examples 518 | 519 | Check the examples folder for more specific use cases examples. 520 | 521 | ## License 522 | 523 | Pedro Dias - [@pedromdias](https://twitter.com/pedromdias) 524 | 525 | Licensed under the Apache license, version 2.0 (the "license"); You may not use this file except in compliance with the license. You may obtain a copy of the license at: 526 | 527 | http://www.apache.org/licenses/LICENSE-2.0.html 528 | 529 | Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. See the license for the specific language governing permissions and limitations under the license. 530 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | BOX_NAME = ENV['BOX_NAME'] || "ubuntu/focal64" 5 | SSH_PRIVKEY_PATH = ENV["SSH_PRIVKEY_PATH"] 6 | NODE_MAJOR_VERSION = "14" 7 | 8 | $script = <