Docker Contexts
Using the host on each command that we use is redundant and error-prone. For example, a deployment may fail due to it reaching a different host because we omitted to specify the host when running the command and we executed a different command on our local host.
For this case, Docker Contexts can be of help.
By creating contexts, we can switch our Docker configuration to multiple contexts and pick the right context per case.
So, let’s create a context for our EC2 host:
$ docker context create ec2-remote --docker host=ssh://ec2-user@18.130.80.179
Although we have created the context, we are still in the default context. Let’s switch to the recently created context:
$ docker context use ec2-remote
Run the following command:
$ docker run -it --rm redis
Check the results on the server:
ssh ec2-user@18.130.80.179 docker ps CONTAINER ID IMAGE COMMAND ...