Switch Docker Contexts Like a Pro

Switch Docker Contexts Like a Pro

Hello, fellow developers! If you’ve ever worked with Docker system services or switched between multiple Docker contexts, you know that it can feel like you’re managing a fleet of ships instead of just running a few containers. Today I’m going to go through the basics of stopping, disabling, switching, and resurrecting your Docker Engine, plus a quick troubleshooting tip for those pesky post-upgrade permission issues. So let’s Switch Docker Contexts Like a Pro.

Stopping and disabling the Docker Engine

Sometimes you need a new beginning. Run the following to stop the Docker Engine and related services:

sudo systemctl stop docker docker.socket containerd

Disable auto-start on boot:

sudo systemctl disable docker docker.socket containerd

Tip: This can be especially useful if you’re switching to Docker Desktop, experimenting with remote Docker contexts, or just trying to conserve some system resources.

Docker Contexts: What, Why and How

The Docker CLI supports multiple engines: local, remote, and cloud. You can read more about how this architecture works in the official Docker Contexts Documentation.

Run the following command to see what contexts you have and which one is active (indicated by the *):

docker context ls

If you have installed Docker Desktop, you will see a new context called desktop-linux. This is how the CLI talks to Docker Desktop instead of your system’s Docker Engine.

Switching Contexts Effortlessly

Need to switch between environments? Use:

docker context use context_name
  • Standard switch (default engine): docker context use default
  • Switch to Docker Desktop: docker context use desktop-linux

Restart Docker service.

Ready to run Docker again? It is as simple as:

sudo systemctl start docker docker.socket containerd

Troubleshooting: Docker daemon socket permission denied

After an upgrade, you might see the infamous error:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

How to fix:

  1. Check the status of the Docker service to ensure that it is running properly.
  2. Assign permissions to the Docker socket if everything else is okay: sudo chmod 666 /var/run/docker.sock

Note: Depending on your current Docker context, the socket URI might be different, so double-check your context before changing permissions.

Conclusion

Managing Docker services and contexts can be a hassle, but it doesn’t have to be. With these commands in your toolbox, you’ll be swapping engines, troubleshooting like a pro, and making your dev environment work for you, not the other way around.

More deep dives and practical tips coming up in our next edition. Happy shipping; until then!

Leave a Reply

Your email address will not be published. Required fields are marked *