{"id":299678,"date":"2021-07-15T14:00:19","date_gmt":"2021-07-15T11:00:19","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it\/"},"modified":"2021-07-15T14:00:19","modified_gmt":"2021-07-15T11:00:19","slug":"how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it\/","title":{"rendered":"#How Docker Contexts Let You Work with Multiple Hosts \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a297d9e48477\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a297d9e48477\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it\/#Creating_a_Context\" >Creating a Context<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it\/#Selecting_Contexts\" >Selecting Contexts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it\/#Synchronizing_Contexts_between_Machines\" >Synchronizing Contexts between Machines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-docker-contexts-let-you-work-with-multiple-hosts-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How Docker Contexts Let You Work with Multiple Hosts \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9034\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Contexts in the Docker CLI provide a streamlined mechanism to interact with multiple Docker endpoints. You can set up contexts for each of your hosts and switch between them on the fly.<\/p>\n<p>When a context is active, Docker will direct all of your commands to that host. If you mostly use a local Docker install but sometimes need to start containers in production, Docker contexts are one option available to you.<\/p>\n<p>Any valid Docker endpoint <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/context\/working-with-contexts\">can be turned into<\/a> a context. You can wire up regular Docker Engine installs, Docker Swarm clusters, and Kubernetes clusters in the cloud. Each stored context contains all the connection information for the referenced host.<\/p>\n<h2 id=\"creating-a-context\"><span class=\"ez-toc-section\" id=\"Creating_a_Context\"><\/span>Creating a Context<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Contexts are managed with the <code>docker context<\/code> command. You create new contexts using <code>docker context create<\/code>. You must supply a name for your context as well as its endpoint configuration.<\/p>\n<p>Here\u2019s how to create a context that connects to a Docker socket exposed over TCP on a remote host:<\/p>\n<pre><code>docker context create remote-host --docker host=tcp:\/\/\/my-remote-host:2735<\/code><\/pre>\n<p>Contexts use Docker Swarm as their default container orchestrator. You can explicitly set this using a flag:<\/p>\n<pre><code>docker context create remote-host &#13;\n    --default-stack-orchestrator=swarm &#13;\n    --docker host=tcp:\/\/\/my-remote-host:2735<\/code><\/pre>\n<p>To create a connection to Kubernetes, change the orchestrator type. You must also add the <code>--kubernetes<\/code> flag and specify the path to a Kubernetes configuration file:<\/p>\n<pre><code>docker context create kubernetes-host &#13;\n    --default-stack-orchestrator=kubernetes &#13;\n    --kubernetes config-file=\/home\/username\/.kube\/config &#13;\n    --docker host=unix:\/\/\/var\/run\/docker.sock<\/code><\/pre>\n<h2 id=\"selecting-contexts\"><span class=\"ez-toc-section\" id=\"Selecting_Contexts\"><\/span>Selecting Contexts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The active context is switched using <code>docker context use<\/code>. Pass the name of the context that you want to activate.<\/p>\n<pre><code>docker context use remote-host<\/code><\/pre>\n<p>All subsequent CLI commands will be executed using the endpoint given by the new context. The active context will automatically persist until you change it. To switch to a different context, run <code>docker context use<\/code> again. You can revert to the default context with your local Docker socket by passing <code>default<\/code> as the context name.<\/p>\n<p>You can always override the selected context by adding the <code>--context<\/code> flag to any Docker command:<\/p>\n<pre><code>docker run ubuntu:latest --context remote-host<\/code><\/pre>\n<p>The <code>DOCKER_CONTEXT<\/code> environment variable also functions as an alternative to the <code>--context<\/code> flag. Either mechanism facilitates a temporary switch to a different context without making you run and revert a <code>docker context use<\/code> command.<\/p>\n<p>Using the <code>DOCKER_HOST<\/code> environment variable will override the active context, too. This variable forces Docker to use a particular daemon endpoint instead of the one provided by the context.<\/p>\n<p>You can inspect the active context by running <code>docker context ls<\/code>. This command lists all the contexts available in your CLI configuration. The active context is highlighted with an asterisk. To delete a context, run <code>docker context rm<\/code>, supplying the context name. It\u2019s not possible to delete the <code>default<\/code> context.<\/p>\n<h2 id=\"synchronizing-contexts-between-machines\"><span class=\"ez-toc-section\" id=\"Synchronizing_Contexts_between_Machines\"><\/span>Synchronizing Contexts between Machines<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Context files are stored in your Docker CLI\u2019s configuration directory. This is usually <code>$HOME\/.docker<\/code> on Linux. You\u2019ll find your contexts in the <code>contexts<\/code> subdirectory. Each context gets its own folder named with a unique hash. Inside, you\u2019ll find a <code>meta.json<\/code> file that describes the context. Only created contexts have files stored on-disk. The <code>default<\/code> context inherits the settings from your Docker daemon configuration.<\/p>\n<p>If you want to synchronize context configuration, you can back up the <code>contexts<\/code> folder to move it to another machine. You could use an Rsync transfer or a Git repository to simplify regular updates. Symlinking the folder to a network share might also be an option depending on your requirements.<\/p>\n<p>Docker lets you export and import contexts via the CLI, too:<\/p>\n<pre><code>docker context export my-context<\/code><\/pre>\n<p>This will create a <code>my-context.dockercontext<\/code> file in your working directory. The file includes the <code>meta.json<\/code> contents as well as some extra information, such as the context\u2019s name. Transfer this file to another machine and run <code>docker context import my-context.dockercontext<\/code> to load the context configuration.<\/p>\n<p>Alternatively, you can export a standalone Kubernetes configuration file for Kubernetes contexts:<\/p>\n<pre><code>docker context export kubernetes-context --kubeconfig<\/code><\/pre>\n<p>This will produce a regular \u201ckubeconfig\u201d file compatible with Kubernetes ecosystem tools such as <code>kubectl<\/code>. The ability to acquire a kubeconfig file from a Docker context improves toolchain interoperability. Nothing within the file will be specific to the Docker CLI.<\/p>\n<p>If you need to edit a context, use the <code>docker context update<\/code> command. This accepts the same flags as <code>docker context create<\/code>. If you\u2019re making bulk updates, you could edit the <code>meta.json<\/code> files to directly manipulate your contexts. You can inspect a context\u2019s <code>meta.json<\/code> file from the CLI with <code>docker context inspect my-context<\/code>.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker contexts are useful when you need to deploy containers in multiple independent environments. You can set up contexts for your local Docker socket, a shared team staging server, and your production Kubernetes server.<\/p>\n<p>Docker has built-in support for the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/cloud\/aci-integration\">Microsoft Azure<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/cloud\/ecs-integration\">Amazon ECS<\/a> container clouds, which can be added as contexts, too. There\u2019s no limit on the number of contexts you can create, so you have good versatility as you move between your hosts.<\/p>\n<p>Arguably the biggest functional issue with contexts is the possibility of accidentally running a command against the wrong context. If you\u2019ve forgotten that you\u2019re in your <code>production<\/code> context, running <code>docker rm database-container<\/code> could have devastating consequences. If in doubt, run <code>docker context ls<\/code> first to check what you have selected.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/12514\/how-docker-contexts-let-you-work-with-multiple-hosts\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How Docker Contexts Let You Work with Multiple Hosts \u2013 CloudSavvy IT&#8221; Contexts in the Docker CLI provide a streamlined mechanism to interact with multiple Docker endpoints. You can set up contexts for each of your hosts and switch between them on the fly. When a context is active, Docker will direct all of your&#8230;<\/p>\n","protected":false},"author":1,"featured_media":299679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-299678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/299678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=299678"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/299678\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/299679"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=299678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=299678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=299678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}