Using Docker On Ubuntu

Recently, I installed Docker on Ubuntu. It was super easy. But when I tried to run a docker command, it threw this error at me:

To confirm that Docker has been installed, open a WSL distribution (e.g. Ubuntu) and display the version and build number by entering: docker -version. Test that your installation works correctly by running a simple built-in Docker image using: docker run hello-world. Install Docker Engine, changing the path below to the path where you downloaded the Docker package. $ sudo dpkg -i /path/to/package.deb. The Docker daemon starts automatically. Verify that Docker Engine is installed correctly by running the hello-world image. $ sudo docker run hello-world. Use Docker on Ubuntu 20.04. The basic syntax for docker commands is: sudo docker option command argument Run Docker Commands Without Sudo. By default, you can only use the docker commands with root privileges. Ubuntu requires the use of the sudo prefix.

It’s not that I am trying to run something special. It happens for basic docker command like ps as well.

Strange, isn’t it? Let me show you how to get past this annoying error.

Fixing ‘Got permission denied while trying to connect to the Docker daemon socket’ error with Docker in Ubuntu

There are two ways to deal with it.

Fix 1: Run all the docker commands with sudo

If you have sudo access on your system, you may run each docker command with sudo and you won’t see this ‘Got permission denied while trying to connect to the Docker daemon socket’ anymore.

But running each and every docker command with sudo is super inconvenient. You miss adding sudo to the beginning and you’ll get ‘permission denied’ error again.

Fix 2: Running docker commands without sudo

To run the docker commands without sudo, you can add your user account (or the account you are trying to fix this problem for) to the docker group.

First, create the docker group using groupadd command. The group may already exist but running the group creation command won’t hurt.

Now that you have the docker group, add your user to this group with the usermod command. I am assuming that you are trying to do it for your own user account and in that case, you can use the $USER variable.

Verify that your user has been added to docker group by listing the users of the group. You probably have to log out and log in back again.

If you check your groups and docker groups is not listed even after logging out, you may have to restart Ubuntu. To avoid that, you can use the newgrp command liks this:

Now if you try running the docker commands without sudo, it should work just fine.

Further troubleshooting

In some cases, you may need to add additional permissions to some files specially if you have run the docker commands with sudo in the past.

You may try changing the group ownership of the /var/run/docker.sock file.

You may also try changing the group ownership of the ~/.docker directory.

And then try running docker with sudo. It should be fine.

I hope this little tutorial helped you to fix the annoying “Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied” error with Docker in Ubuntu.

Did it fix the problem for you? If yes, I welcome a quick comment of thanks from you. If not, I’ll be happy to help you fix this problem further.

Tutorial

Using

Introduction

Server automation now plays an essential role in systems administration, due to the disposable nature of modern application environments. Configuration management tools such as Ansible are typically used to streamline the process of automating server setup by establishing standard procedures for new servers while also reducing human error associated with manual setups.

Ansible offers a simple architecture that doesn’t require special software to be installed on nodes. It also provides a robust set of features and built-in modules which facilitate writing automation scripts.

This guide explains how to use Ansible to automate the steps contained in our guide on How To Install and Use Docker on Ubuntu 18.04. Docker is an application that simplifies the process of managing containers, resource-isolated processes that behave in a similar way to virtual machines, but are more portable, more resource-friendly, and depend more heavily on the host operating system.

Prerequisites

In order to execute the automated setup provided by the playbook we’re discussing in this guide, you’ll need:

  • One Ansible control node: an Ubuntu 18.04 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions and a firewall enabled, as explained in our Initial Server Setup guide. To set up Ansible, please follow our guide on How to Install and Configure Ansible on Ubuntu 18.04.
  • One or more Ansible Hosts: one or more remote Ubuntu 18.04 servers previously set up following the guide on How to Use Ansible to Automate Initial Server Setup on Ubuntu 18.04.

Before proceeding, you first need to make sure your Ansible control node is able to connect and execute commands on your Ansible host(s). For a connection test, please check step 3 of How to Install and Configure Ansible on Ubuntu 18.04.

What Does this Playbook Do?

This Ansible playbook provides an alternative to manually running through the procedure outlined in our guide on How To Install and Use Docker on Ubuntu 18.04.

Running this playbook will perform the following actions on your Ansible hosts:

  1. Install aptitude, which is preferred by Ansible as an alternative to the apt package manager.
  2. Install the required system packages.
  3. Install the Docker GPG APT key.
  4. Add the official Docker repository to the apt sources.
  5. Install Docker.
  6. Install the Python Docker module via pip.
  7. Pull the default image specified by default_container_image from Docker Hub.
  8. Create the number of containers defined by the create_containers variable, each using the image defined by default_container_image, and execute the command defined in default_container_command in each new container.

Once the playbook has finished running, you will have a number of containers created based on the options you defined within your configuration variables.

How to Use this Playbook

The first thing we need to do is obtain the Docker playbook and its dependencies from the do-community/ansible-playbooks repository. We need to clone this repository to a local folder inside the Ansible Control Node.

In case you have cloned this repository before while following a different guide, access your existing ansible-playbooks copy and run a git pull command to make sure you have updated contents:

If this is your first time using the do-community/ansible-playbooks repository, you should start by cloning the repository to your home folder with:

The files we’re interested in are located inside the docker_ubuntu1804 folder, which has the following structure:

Here is what each of these files are:

  • vars/default.yml: Variable file for customizing playbook settings.
  • playbook.yml: The playbook file, containing the tasks to be executed on the remote server(s).
  • readme.md: A text file containing information about this playbook.

We’ll edit the playbook’s variable file to customize our Docker setup. Access the docker_ubuntu1804 directory and open the vars/default.yml file using your command line editor of choice:

This file contains a few variables that require your attention:

The following list contains a brief explanation of each of these variables and how you might want to change them:

  • create_containers: The number of containers to create.
  • default_container_name: Default container name.
  • default_container_image: Default Docker image to be used when creating containers.
  • default_container_command: Default command to run on new containers.

Install Docker On Ubuntu Wsl

Once you’re done updating the variables inside vars/default.yml, save and close this file. If you used nano, do so by pressing CTRL + X, Y, then ENTER.

You’re now ready to run this playbook on one or more servers. Most playbooks are configured to be executed on every server in your inventory, by default. We can use the -l flag to make sure that only a subset of servers, or a single server, is affected by the playbook. We can also use the -u flag to specify which user on the remote server we’re using to connect and execute the playbook commands on the remote hosts.

To execute the playbook only on server1, connecting as sammy, you can use the following command:

You will get output similar to this:

Note: For more information on how to run Ansible playbooks, check our Ansible Cheat Sheet Guide.

When the playbook is finished running, log in via SSH to the server provisioned by Ansible and run docker ps -a to check if the containers were successfully created:

You should see output similar to this:

This means the containers defined in the playbook were created successfully. Since this was the last task in the playbook, it also confirms that the playbook was fully executed on this server.

The Playbook Contents

You can find the Docker server setup featured in this tutorial in the docker_ubuntu1804 folder inside the DigitalOcean Community Playbooks repository. To copy or download the script contents directly, click the Raw button towards the top of each script.

The full contents of the playbook as well as its associated files are also included here for your convenience.

vars/default.yml

Using Docker On Ubuntu

The default.yml variable file contains values that will be used when setting up Docker on your server.

Using Docker On Ubuntu Operating System

vars/default.yml

playbook.yml

The playbook.yml file is where all tasks from this setup are defined. It starts by defining the group of servers that should be the target of this setup (all), after which it uses become: true to define that tasks should be executed with privilege escalation (sudo) by default. Then, it includes the vars/default.yml variable file to load configuration options.

Using Docker On Ubuntu Operating System

Feel free to modify this playbook to best suit your individual needs within your own workflow. For example, you could use the docker_image module to push images to Docker Hub or the docker_container module to set up container networks.

Conclusion

Automating your infrastructure setup can not only save you time, but it also helps to ensure that your servers will follow a standard configuration that can be customized to your needs. With the distributed nature of modern applications and the need for consistency between different staging environments, automation like this has become a central component in many teams’ development processes.

Using Docker In Ubuntu Server

In this guide, we demonstrated how to use Ansible to automate the process of installing and setting up Docker on a remote server. Because each individual typically has different needs when working with containers, we encourage you to check out the official Ansible documentation for more information and use cases of the docker_container Ansible module.

If you’d like to include other tasks in this playbook to further customize your initial server setup, please refer to our introductory Ansible guide Configuration Management 101: Writing Ansible Playbooks.