How to fix the dpkg lock file error in Packer?

Today everyone is switching to cloud platforms. To this, we need to install basic software or modify certain Configurations in each server and to all this, we call prerequisites. So we have a concept of creating a BASE IMAGE and GOLDEN IMAGE. We were creating Golden Server Image for one of our clients using Packer to fulfill the prerequisites, and it is when I faced this issue.

‘E: Could not get lock /var/lib/dpkg/lock’

Firstly, we will talk about Packer.

What is Packer? 

Packer is an open-source DevOps tool made by Hashicorp to create identical machine images for multiple platforms from a single JSON config file. This tool really works when it comes to creating immutable architecture. Which helps us to create instances with predefined requirements of the client and requesters.

How to use Packer?

You can use Packer for AWS, GCP, Azure, etc.

Packer is a template-driven tool and templates are written in JSON format.

{

  "variables": {

    "region": ""

  },

  "builders": [

    {

      "type": "amazon-ebs",

      "profile": "default",

      "region": "{{user `region`}}",

      "instance_type": "t2.micro",

      "source_ami": "base_AMI",

      "ssh_username": "ssh-user"

    }

  ],

  "provisioners": [

    {

      "type": "shell",

      "script": "./script.sh"

    },

    {

      "type": "shell",

      "inline": [

        "sudo apt update",

        "sleep 20"

      ]

    },

    {

      "type": "shell",

      "script": "install.sh",

      "max_retries": "15"

    }

  ]

}

oftentimes, while running this packer script we all see an error like

<strong>“ /var/lib/dpkg/lock-frontend ”</strong>

It happened to me as well. When we were creating an Image for one of our applications. It was being created on Amazon Machine Image and while running one of the bash scripts, the Packer pipeline broke down repeatedly. That got me confused and I was clueless about how to fix it. So, I started exploring this error and I found the cause below.

  • It could occur in our local system. OR
  • It could occur while running the packer script.

Usually, we see this error because some other program is trying to update Ubuntu. When a command or application is updating the system or installing new software, it locks the dpkg file (Debian package manager). 

This locking is done so that two processes don’t change the content at the same time as it may lead to an unwarranted situation and possibly a broken system. 

We have 2 ways to solve this problem. One is for the local system and another one is for the packer. The solution for the error occurring on the local system can be easily found on the internet. But for packer script, it was pretty hard to find.

So I figured out a solution to the very problem.

Let’s see what steps can you take to fix this issue of ‘unable to lock administration directory’. 

While running the shell script if we are facing this issue, we can use the “WHILE LOOP” for this and make it SLEEP till this gets unlocked. 

We can add This command to our SCRIPT 

while sudo lsof /var/lib/dpkg/lock-frontend ; do sleep 10; done;

Conclusion

It was the solution we used to skip the error. Thankfully, it resolved our issue. Now, this got us thinking – have you ever come across an issue like this? We totally invite your suggestions.
Do let us know in the comments below.

Blog Pundit: Deepak Gupta and Sandeep Rawat

Opstree is an End to End DevOps solution provider

Connect Us

Author: Rishabh Sharma

DevOps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: