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 manage connections to different Docker daemons. By default, Docker uses the default
context, which connects to the local Docker daemon. However, you can create new contexts to connect to remote Docker daemons or other container orchestration platforms.
To create a new Docker context, we use the docker context create
command. This command requires a name for the new context and the endpoint of the Docker daemon it should connect to. For this lab, we will create a context named my-context
that connects to the local Docker daemon.
Open your terminal and run the following command:
docker context create my-context --docker host=unix:///var/run/docker.sock
This command creates a new context named my-context
. The --docker host=unix:///var/run/docker.sock
option specifies that this context should connect to the Docker daemon via the Unix socket at /var/run/docker.sock
. This is the default location for the Docker socket on most Linux systems.
After running the command, you should see output similar to this, indicating that the context was created successfully:
my-context
Successfully created context "my-context"
Now that we have created the context, we can verify its existence by listing the available contexts. Use the docker context ls
command:
docker context ls
You should see a list of contexts, including the default
context and the newly created my-context
. The output will look something like this:
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current DOCKER_HOST unix:///var/run/docker.sock swarm
my-context unix:///var/run/docker.sock
The asterisk (*) next to default
indicates that it is currently the active context. We will learn how to switch contexts in a later step.