<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Siddharth Pradhan</title>
    <description>The latest articles on DEV Community by Siddharth Pradhan (@lamsiddharth).</description>
    <link>https://dev.to/lamsiddharth</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1654750%2F83687e96-919a-4b26-9178-6645f19bf83d.png</url>
      <title>DEV Community: Siddharth Pradhan</title>
      <link>https://dev.to/lamsiddharth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lamsiddharth"/>
    <language>en</language>
    <item>
      <title>Understanding Root vs. Non-Root Users, Docker, and Fixing ECR Authentication Issues</title>
      <dc:creator>Siddharth Pradhan</dc:creator>
      <pubDate>Sun, 09 Mar 2025 10:07:29 +0000</pubDate>
      <link>https://dev.to/lamsiddharth/understanding-root-vs-non-root-users-docker-and-fixing-ecr-authentication-issues-8jh</link>
      <guid>https://dev.to/lamsiddharth/understanding-root-vs-non-root-users-docker-and-fixing-ecr-authentication-issues-8jh</guid>
      <description>&lt;p&gt;When working with Docker and cloud services like Amazon Elastic Container Registry (ECR), you may encounter issues related to user permissions and authentication. One common problem is the "authorization token has expired" error when pushing images to ECR. This error often arises due to differences between how root and non-root users interact with Docker and AWS ECR.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Root vs. Non-Root Users in Linux and Docker&lt;/li&gt;
&lt;li&gt;Why the ECR Authentication Token Expires&lt;/li&gt;
&lt;li&gt;How to Fix the "Authorization Token Expired" Error&lt;/li&gt;
&lt;li&gt;Best Practices for Managing Docker and ECR Authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Root vs. Non-Root Users in Linux and Docker
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is the Root User?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The root user is the superuser in Linux-based systems. It has unrestricted access to all commands, files, and system resources. When you run a command with sudo, you’re executing it as the root user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unlimited Privileges: The root user can perform any action on the system, including installing software, modifying system files, and changing permissions.&lt;/li&gt;
&lt;li&gt;Dangerous Power: A single incorrect command as root can delete critical system files or render the system unusable.&lt;/li&gt;
&lt;li&gt;Home Directory: The root user’s home directory is /root.&lt;/li&gt;
&lt;li&gt;Configuration Files: Root-specific configurations are stored in /root/.docker/config.json (for Docker)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is a Non-Root User?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A non-root user is a regular user with limited permissions. These users are created for everyday tasks and are restricted from performing administrative actions unless explicitly granted permission.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited Privileges: Non-root users can only access files and directories they own or have been granted permission to use.&lt;/li&gt;
&lt;li&gt;Home Directory: Each non-root user has a home directory, typically located at /home/username.&lt;/li&gt;
&lt;li&gt;Configuration Files: User-specific configurations are stored in /home/username/.docker/config.json (for Docker).&lt;/li&gt;
&lt;li&gt;Security: Non-root users cannot modify system files or install software globally, which reduces the risk of accidental damage or security breaches.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why Does Linux Distinguish Between Root and Non-Root Users?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The distinction between root and non-root users is a core part of Linux’s security model. Here’s why it’s done:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Security:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minimize damage if a user’s account is compromised.&lt;/li&gt;
&lt;li&gt;Follow the Principle of Least Privilege: Users are given only the permissions they need to perform their tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Stability:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevent accidental changes to critical system files.&lt;/li&gt;
&lt;li&gt;Isolate user files and configurations to avoid conflicts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Accountability:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Actions performed by non-root users can be logged and audited.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Why the ECR Authentication Token Expires
&lt;/h2&gt;

&lt;p&gt;Amazon ECR requires users to authenticate Docker before pushing or pulling images. This authentication is done using a temporary token generated by the AWS CLI command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
`aws ecr get-login-password --region &amp;lt;region&amp;gt; | docker login --username AWS --password-stdin &amp;lt;aws_account_id&amp;gt;.dkr.ecr.&amp;lt;region&amp;gt;.amazonaws.com`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points About the Token:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The token is valid for 12 hours by default.&lt;/li&gt;
&lt;li&gt;The token is stored in the Docker configuration file (~/.docker/config.json) of the user who ran the docker login command.&lt;/li&gt;
&lt;li&gt;If you switch users (e.g., use sudo to run Docker commands), the token from the non-root user’s configuration is not accessible, leading to the "authorization token expired" error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. How to Fix the "Authorization Token Expired" Error
&lt;/h2&gt;

&lt;p&gt;The error occurs because the Docker client is unable to find a valid authentication token for ECR. Here’s how to fix it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1: Run Docker Commands Without sudo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your user is part of the docker group, you can run Docker commands without sudo. This ensures that the Docker client uses the correct configuration and authentication token.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add Your User to the docker Group:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`sudo usermod -aG docker $USER`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Log out and log back in, or run newgrp docker to apply the changes.&lt;/p&gt;

&lt;p&gt;2.Authenticate Docker with ECR:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-east-1.amazonaws.com`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Push the Image:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`docker push &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-east-1.amazonaws.com/siddharth/test-lambda:latest
`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option 2: Authenticate as Root&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you must use sudo, you need to authenticate Docker with ECR as the root user.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Switch to the Root User:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`sudo su`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Authenticate Docker with ECR:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-east-1.amazonaws.com`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Push the Image:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`
docker push &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-east-1.amazonaws.com/siddharth/test-lambda:latest`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Exit the Root Shell:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`exit`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option 3: Use a Docker Credential Helper&lt;/strong&gt;&lt;br&gt;
To avoid manually logging in repeatedly, you can configure a Docker credential helper for ECR. This automatically manages your ECR credentials.&lt;/p&gt;

&lt;p&gt;1.Install the Docker Credential Helper for ECR:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`sudo apt-get install docker-credential-helper-ecr`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Update Your Docker Configuration:
Edit ~/.docker/config.json and add:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
`{
  "credsStore": "ecr-login"
}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Reauthenticate with ECR:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-east-1.amazonaws.com`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Push the Image:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`docker push &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-east-1.amazonaws.com/siddharth/test-lambda:latest`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Best Practices for Managing Docker and ECR Authentication
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Avoid Using sudo with Docker:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Add your user to the docker group and run Docker commands as a non-root user.&lt;/li&gt;
&lt;li&gt;This reduces security risks and avoids configuration mismatches.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Use a Credential Helper:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Configure the Docker credential helper for ECR to automate authentication and avoid token expiration issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Rotate AWS Credentials Regularly:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Ensure your AWS access keys are rotated periodically for security.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Monitor Token Expiry:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If you’re running long-running scripts, ensure the ECR token is refreshed before it expires.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Use IAM Roles for EC2 Instances:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If you’re working on an EC2 instance, assign an IAM role with ECR permissions to avoid managing access keys.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts: The Philosophy of Root and Non-Root
&lt;/h2&gt;

&lt;p&gt;In the world of Linux, the root user is like a god—it can create, destroy, and reshape the system at will. But with great power comes great responsibility. One wrong move, and the entire system can come crashing down. On the other hand, the non-root user is like a wanderer—limited in scope but free to explore within boundaries. It’s a reminder that constraints aren’t always limitations; they’re often protections.&lt;/p&gt;

&lt;p&gt;When you work with tools like Docker and ECR, you’re not just managing containers or pushing images—you’re navigating a system built on layers of trust, permissions, and balance. It’s a microcosm of how we should approach life: be bold, but not reckless; be free, but not careless.&lt;/p&gt;

&lt;p&gt;So, as you dive deeper into the world of DevOps, remember this: The root user may have all the power, but the non-root user has all the wisdom. Use both wisely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay Hungry, Stay Rooted&lt;/strong&gt;&lt;br&gt;
Keep breaking barriers, but always know your limits. Keep building, but always respect the system. And most importantly, keep learning—because in the end, the only thing that truly roots us is our curiosity.&lt;/p&gt;

&lt;p&gt;Stay dope. Stay grounded. 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>aws</category>
      <category>linux</category>
      <category>docker</category>
    </item>
    <item>
      <title>Syncing Prisma Migrations: Aligning Your Schema Drift and Manual SQL Changes in PostgreSQL</title>
      <dc:creator>Siddharth Pradhan</dc:creator>
      <pubDate>Fri, 28 Feb 2025 14:01:20 +0000</pubDate>
      <link>https://dev.to/lamsiddharth/syncing-prisma-migrations-aligning-your-schema-drift-and-manual-sql-changes-in-postgresql-18fd</link>
      <guid>https://dev.to/lamsiddharth/syncing-prisma-migrations-aligning-your-schema-drift-and-manual-sql-changes-in-postgresql-18fd</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;disclaimer: newbie here 1st time ever ran into this problem anyone reading this pls suggest some better methods&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you’ve been working with Prisma and PostgreSQL, you might run into a situation where your database schema and Prisma migration history are out of sync. This often happens when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You make direct manual SQL changes instead of using Prisma migrations.&lt;/li&gt;
&lt;li&gt;You create an enum and use it in the same migration (which PostgreSQL doesn’t allow).&lt;/li&gt;
&lt;li&gt;You delete or modify migration files after they’ve been applied.&lt;/li&gt;
&lt;li&gt;Prisma tries to reset the database because it detects drift.&lt;/li&gt;
&lt;li&gt;Instead of resetting your database and losing data, I’ll walk you through how to sync your Prisma migrations with your actual PostgreSQL schema safely.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Schema Drift in Prisma?
&lt;/h2&gt;

&lt;p&gt;Prisma tracks all applied migrations in a special table called _prisma_migrations. When you make direct SQL changes, Prisma isn’t aware of them, so it assumes your database is different from what it expects. This is called schema drift.&lt;/p&gt;

&lt;p&gt;To check if your database is out of sync, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma migrate status

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see something like "The database schema is not in sync with the migrations history", that means there’s a drift.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Check Your Current Database Schema
&lt;/h2&gt;

&lt;p&gt;First, you need to verify what your database schema looks like compared to your Prisma schema. Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma db pull

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command updates your schema.prisma file to reflect the current state of your PostgreSQL database. If there are changes that Prisma wasn’t tracking, you’ll see them here.&lt;/p&gt;

&lt;p&gt;Now, compare your prisma/migrations folder with your schema.prisma. If you see differences, that means Prisma thinks some migrations haven’t been applied, or the database was changed manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Identify and Remove Problematic Migrations
&lt;/h2&gt;

&lt;p&gt;f Prisma thinks a migration is missing but you already applied the changes manually, you need to remove that migration from the _prisma_migrations table so Prisma stops trying to reapply it.&lt;/p&gt;

&lt;p&gt;To list all applied migrations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql your_database -c "SELECT * FROM _prisma_migrations;"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find the problematic migration name and delete it from the history.&lt;/p&gt;

&lt;p&gt;To remove a specific migration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM _prisma_migrations WHERE migration_name = '20240228000100_add_enum_and_table';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠️ Warning: Be very careful when deleting migrations! Always take a backup before making changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create and Apply a New Migration Without Resetting
&lt;/h2&gt;

&lt;p&gt;Now that you’ve removed the problematic migration from Prisma’s history, you need to generate a new migration file that matches the current schema.&lt;/p&gt;

&lt;p&gt;1️⃣ Run this command to create a new migration without applying it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma migrate dev --create-only --name sync_schema

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generates a migration file inside prisma/migrations/, but it won’t run it yet.&lt;/p&gt;

&lt;p&gt;2️⃣ Manually edit the migration file to make sure it accurately reflects your current schema.&lt;/p&gt;

&lt;p&gt;3️⃣ Mark this migration as applied so Prisma won’t try to reapply changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma migrate resolve --applied "sync_schema"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, Prisma knows this migration is already applied and won’t try to reset your database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Verify That Everything is in Sync
&lt;/h2&gt;

&lt;p&gt;Finally, check that Prisma recognizes your schema as up-to-date:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma migrate status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is correct, you should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pgsql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Database schema is up to date!&lt;br&gt;
To be extra sure, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma db pull
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures your schema.prisma is fully synced with the live database.&lt;/p&gt;

&lt;p&gt;Best Practices to Avoid Schema Drift in Prisma&lt;br&gt;
✅ Always use Prisma migrations instead of direct SQL changes.&lt;br&gt;
✅ If you must make manual changes, immediately run prisma db pull to sync.&lt;br&gt;
✅ Never modify an already applied migration file. Instead, create a new migration.&lt;br&gt;
✅ Regularly check npx prisma migrate status to catch drift early.&lt;br&gt;
✅ Use npx prisma migrate resolve instead of forcing resets to preserve data.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>prisma</category>
      <category>database</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
