Create a new Docker context
In this step, we will learn how to create a new Docker context. A Docker context is a way to connect your Docker CLI to a different Docker daemon. This can be useful when you want to manage Docker on a remote machine or a different environment.
By default, your Docker CLI connects to the local Docker daemon. We can see the current context using the docker context ls
command.
docker context ls
You should see an output similar to this, indicating the default context is active:
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current DOCKER_HOST environment variable unix:///var/run/docker.sock
Now, let's create a new context. We will create a context named my-context
. For this example, we will still point it to the local Docker daemon, but in a real-world scenario, you would point it to a remote daemon.
We use the docker context create
command followed by the context name and the endpoint.
docker context create my-context --docker "host=unix:///var/run/docker.sock"
After creating the context, you should see a confirmation message:
my-context
Successfully created context "my-context"
Now, let's list the contexts again to see the newly created one.
docker context ls
You should now see both the default
and my-context
listed.
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current DOCKER_HOST environment variable unix:///var/run/docker.sock
my-context unix:///var/run/docker.sock
To switch to the new context, we use the docker context use
command followed by the context name.
docker context use my-context
You will see a message confirming the context switch:
my-context
Current context is now "my-context"
Finally, let's list the contexts one more time to confirm that my-context
is now the active context, indicated by the asterisk *
.
docker context ls
The output should show my-context
as the active context:
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default Current DOCKER_HOST environment variable unix:///var/run/docker.sock
my-context * unix:///var/run/docker.sock