How to Manage Docker Volumes on Windows

This blog post was created from a snip created by Matt McElreath. You can check out the video Managing Docker Volumes on Windows if you’re more into video format.

Docker volumes are the preferred way for handling persistent data created by and used by Docker containers. Let’s take a look at how this works.

If you want to store persistent data for containers, there are a couple of options. First, I’ll show you how to use a bind mount. I’m currently in a folder called data on my C-drive. If I list the contents of this folder, you can see that I have five text files.

If I want to make this folder available to a container, I can mount it when starting the container. Let’s go ahead and run a container using docker run. I’m going to run this container in interactive mode, then specify -V. Here, I’m going to put the path to my data folder, followed by a colon, then I will specify the path inside the container where I would like this folder to be mounted.

For this, I’m going to specify the shareddata folder on the C-drive. Then I’ll specify the Windows server core image and finally, I’ll specify that I want to run PowerShell once I’m inside the container.

Now that I’m inside the new container, if I list the contents of the C-drive, you can see that I have a shareddata folder.

Let’s go into that folder and list the contents. Here are my five test files that are located on my container host.

I can also create files in this folder, which will be available to other containers or my container host. Let’s go ahead and run new item to create a file called containertest.

We can see above that the new file has been created from within the container. Now I’ll exit this container which will shut it down by running exit.

If I run docker ps, you can see that there are currently no running containers.

Now let’s list the contents of the data folder again from my container host.

We can see the new file that was created from inside the container called containertest. Bind mounts have some limited functionality, however, so volumes are the preferred way to accomplish what we are trying to do. To get started with volumes, we can run the same command to start up a container, but this time with a couple of small differences. Where we specified the volume, instead of using the path on the container hosts’ file system, I’m going to use the word hostdata as the name of a volume I want to create and use.

From inside the new container, if I list the contents of the C-drive, you can see again that I have a folder called shareddata.

If I list the contents of that folder, it is currently empty because we created a blank volume. Now let’s run Ctrl-P-Q which will take us out of the running container, but keep it running in the background.

From the container host, let’s run docker volume ls. This will list the current volumes on this container host. I have a volume called hostdata, which was created when I specified it in the docker run command.

If I run docker ps we can see our running container.

Let’s stop that container using docker stop. Now we have no running containers.

Let’s remove the stopped containers by running docker rm. If I list the volumes again, you can see that the hostdata volume is still available and can be mounted to new containers.

Another way to create a volume is to use the docker volume create command. If you don’t specify a name, docker will give it a name which is a long list of random characters. Otherwise, you can specify a name here. I’m going to call this volume logdata. Now we can see it is in the list when we list the volumes again.

Now let’s go ahead and mount that to a new container. I’m going to use docker run again and for the volume I’m going to specify the volume that I just created and mount it to c:\logdata.

From inside the container, I’m going to go into the logdata folder and create a couple of files. Right now, there are no files in this directory, so let’s go ahead and create some.

Now I have two log files in this directory.

Let’s run Ctrl-P-Q again to exit this container while it is still running. While that container’s running, let’s start up a new container with the same volume mounted.

If we run a listing on the logdata folder in the new container we can see the two log files being shared.

Now let’s exit this container. I currently still have one running container and two exited containers.

I’m going to go ahead and stop all running containers, then run docker rm to remove all exited containers.

Let’s go ahead and list the volumes again. The logdata volume is still available to be mounted to future containers.

If I just run docker volume, I’ll get some usage help for the command.

We already looked at create, so let’s move on to inspect. If I run docker volume inspect against the logdata volume, it will return the properties for that volume, including the mount point which is the physical path to the volume on the container host.

Let’s open that folder using Invoke-Item and have a look. Under the logdata folder, there’s a folder called _data. If we open that, we can see the files that were created from the container earlier.

To delete a volume, we can run docker volume rm, followed by the name of the volume you want to delete.

Now if I list the volumes, logdata is no longer there.

Finally, we can use prune to remove all unused local volumes. This will delete all volumes that are not mounted to a running or stopped container.

You want to be careful with this command, so there’s a warning and a prompt to make sure that you are sure that you want to do this. If I type Y and hit enter, it will show me which volumes were deleted.

And if I list my volumes again you can see that they have all been deleted.

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to Set the DNS Server Search Order on Windows with PowerShell

To follow along, you can find a copy of the code used in the SnipSnips GitHub repo.

Setting your DNS server search order with PowerShell is actually really easy. We’ll start with the Get-DNSClientServerAddress to get a look at our existing settings as you can see below.

So there you can see, we have our existing settings on ethernet interface index seven, and our addresses are 192.168.2.52 and the secondary server is at .51.

So we’ll do a quick nslookup to file01.corp.ad, to verify that our primary is in fact responding.

So there we go, you can see above that a responding DNS server is our primary at .52, and successfully returned .55 is our file server.

Now, let’s change the order of our DNS servers. To do that, we’ll use the Set-DNSClientServerAddress cmdlet. We’ll point it to interface index seven as listed above, and I’ll change our order, so 192, 168.2.51 is our primary, and .52 is now our secondary.

We’ll do a quick verification. I’ll check interface index seven.

There, now you can see above, .51 is now our primary as it’s listed first, and .52 is our secondary.

Do another quick nslookup, and you can see that that now returns from .51, which is our primary DNS server.

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to Undeniably KNOW You’re an Entrepreneur

Have you ever been in a job you hate? Have you ever been in a position you love? Probably. Lots of people get a job, make decent money and are happy with our lives.

We’re content to separate work and personal life and talk about “work/life balance.” But what if work and life felt like one to you?

You’d be done with the “work/life” balance problem but depending on where you live and who your social circles are you’d feel out of place. You’d hear comments like “What are you thinking?”, “Are you crazy?”, “You’re going to give up that security?” when commenting to others about venturing out on your own and pursuing your dream.

Entrepreneurs want to break free…constantly

What others around you may not realize is that you possess this deep-rooted desire to break free of the full-time employment chains. Golden handcuffs or not, your whole being is telling you to rip them off and be free but cultural pressures and your sanity are screaming you’re crazy.

Any “normal” person with a six-figure job that they enjoyed doing with ultimate flexibility would have a screw loose for wanting to throw it away, right?

The traditional way of thinking of a good job is security, comfort and merely that thing you do for 40 hours/week that sometimes gets in the way of the fun stuff.

“Good” jobs are toxic to an entrepreneur

An entrepreneur with a “good” job thinks that’s 40 hours/week of wasted potential she could use to pursue her dreams and build a business. These opposing forces, when coming together, produce a volatile mix of fear, angst, sadness and excitement.

An entrepreneur trapped in a “good” job is like an intelligent frog sitting in boiling water. It’s sure is warm and comfortable in that water, but the smart frog knows that, at some point, they’re going to be boiled alive. But that water feels so nice!

Entrepreneurs are different

An entrepreneur is different than most. As that iconic Apple commerical correctly puts it, entrepreneurs are [sic] “the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes, the ones who see things differently.”

An entrepreneur isn’t like anyone else. He’ll never be happy working for the best boss in the world, working on the absolute coolest projects and making millions of dollars a year if the goal is for someone else. He’ll always yearn to follow his own path.

An entrepreneur is independent. He has a vision in his head and uses every ounce of his being to fulfill that vision.

A “good” job is just a distraction from reaching the ultimate goal. “Good” jobs are toxic to an entrepreneur. They tempt her into entertaining the idea that she may be happy working for someone else but at some point, her true calling will surface again and begin knawing at her to pursue her true passion again – entrepreneurship.

A “good” job temporarily masks an entrepreneur’s spirit. It’s a pill that makes an entrepreneur fill warm and cozy until the effect wears off and he’s back left staring in the mirror questioning himself over and over again.

Entrepreneurs are relentless

An entrepreneur is impatient. He has a vision and will make that vision a reality at whatever cost. He has no time for others that hinder steps to fulfill the vision. He has one goal and one goal only; to build a successful business no matter what the cost.

An entrepreneur will fail, but the fire an entrepreneur has in his belly is unrelenting. It’s a fire that is not extinguishable. Entrepreneurship is an addiction to success. It’s an endless pursuit that is so ingrained in his DNA that it leaves no room for escape no matter how many bonuses, raises and stock options are thrown at him. It’s a force unlike no other.

Some questions to ask yourself if you’re an entrepreneur are:

  • Do you have that drive, that unrelenting knawing inside you that can be pushed down temporarily but always seems to come back up?
    – Do you feel delighted and fulfilled when the work that you do directly contributes to your own success?
    – Do you feel like a job merely is 40 hrs/week that’s holding you back from your real potential?
    – Are you willing to pursue a passion that may end up failing miserably?
    – Do you think of failure as inevitable and use it as a learning experience in your next venture?
    – Will you do anything to work for yourself rather than going back to another job?

If you’ve answered yes to most of these questions, you are an entrepreneur.

If you’re still in a job, quit. Quit now. You’re only delaying the inevitable. You’re fooling yourself into thinking you’re happy.

You’re not.

You’re merely being bamboozled by the money, power and prestige a “good” job gives you. You will be much happier making a quarter of the money you’re making now working on your business, not someone else’s.

The fulfillment you’ll get out of life will increase exponentially and regardless if you fail or succeed, you’ll get back up and do it again until you’re so much better off than what that 3% raise every year gave you.

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

Entrepreneur vs. Engineer: A Founder’s Dilemma

 

Us IT folk are a unique breed or so I’m told. We’re logical, black and white thinkers that require strict rules and are prone to outbursts around tabs vs. spaces at any moment.

We love either Windows or Linux but not both, have a major case of imposter syndrome and are socially inept. At least that’s the stereotype. It turns out a lot of this is true of myself.

A lot of engineers are completely happy going to work, solving interesting problems, working with good people and toiling on interesting open-source side projects in their free time. This used to be me and life was a lot simpler.

The Hidden Entrepreneur Inside

You see, I’m just like your typical IT engineer but with one key difference. For whatever reason, I have this insatiable desire to blaze my own path and to build something on my own that benefits other people like me. It’s a blessing and a curse.

I have a deep-rooted entrepreneur inside of my brain that refuses to give into the simpler ways of living as just an engineer. Lucky for people that enjoy free, IT screencasts, the entrepreneur is what brought you this fine TechSnips platform.

For those that either silence their entrepreneur brain or for those completely happy working as an engineer may not completely understand this phenomenon. Let me explain.

Imagine constantly thinking you should be doing something else while working on a task you enjoy more at the time. Consider when you leave the volume at 19 on your TV, see a red M&M in a bowl of green ones or get interrupted when you’re just about finished with an awesome TV show. It’s kinda like that.

Entrepreneur vs. Engineer

I, as an engineer, love solving real, tactical problems. I enjoy spending time seeing the fruits of my labor immediately in the form of a passing test, working code or automation workflow doing its thing. The reward is immediate, same-day and doesn’t require strategizing about content, researching future clients or trying to get the word out about TechSnips.

I, as an entrepreneur, am not supposed to focus on the tactical logistics of building the product. I shouldn’t be spending time neck-deep in code in favor of focusing on the TechSnips marketing strategy, SEO or ensuring that next big opportunity doesn’t pass us by!

My inner entrepreneur and engineer constantly battle. On one hand, I love working for myself, building a business, helping others and fulfilling this vision I’ve had for a long time with TechSnips but on the other hand, I sometimes just want to push the world aside and work on a cool PowerShell script!

Learning Balance

Since TechSnips is only a few months old, I’ve still got some time to find my groove. I’m still allowed to have those weeks where I don’t do the “right” thing focusing on strategic vision, marketing and managerial work in favor of the fun stuff like coding.

I’ve still got time but it’s time to start figuring out what I’m good at, not good at and finding more people to help me in this endeavor.

From what I can tell, as a founder running a successful business is about choosing your battles. You need customers to survive, a motivated team behind you, a great product and a vision for the future. I’m getting there. You also need to know when and when not to spend time on frivolous activities in favor of the greater good.

TechSnips continues to grow month after month and I’m excited about the future. I’m looking forward to growing as an entrepreneur and business owner and learning how to adapt my engineer tendendencies to business development!

 

 

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

Making all Objects in an AWS S3 Bucket Always Public

At TechSnips, we use Amazon S3 to store all of the stuff required to operate. One ability we need is to provide a publicly accessible repository of files. Luckily, S3 has this ability to set objects to public read-access.

To set an object to public read-access, you can click on Make Public via right-clicking on the object inside of the S3 Management Console.

S3 Make Public

This is all well and good but if you’ve got tons of files constantly being uploaded to S3, I’m not about to manually make all of my objects public like this!

After a bit of digging, I was able to figure out how to make all objects be public the moment they are added to a particular bucket. To do this requires creating a bucket policy. This bucket policy applies to all GetObject actions. You can see the bucket policy I used below for our techsnips-public S3 bucket.

This bucket policy can be assigned to the bucket via the Management Console

Once you have the bucket policy set, you’ll then need to also assign Public Access to the Everyone group as well via the Access Control List.

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to Create/Restore an Azure Virtual Machine Snapshot with PowerShell

Azure Virtual Machine Snapshot

I’ve learned that creating an Azure virtual machine snapshot isn’t quite as easy as creating checkpoints in my on-prem Hyper-V lab. Frustrated that I had to build my own tool for this, I set out to do it anyway.

As you may have found out already creating an Azure virtual machine snapshot isn’t quite as right-click and click as you’d expect. Sure, Azure has Azure Backup now but it seems overkill for the constant snapshots and reverts I’d like to do in my development environment. Instead, Azure has a Snapshots feature that appears to have been neglected by Microsoft. There’s little to no documentation on creating and restoring Azure virtual machine snapshots.

So I dug in and automated it!

I wanted two PowerShell functions so I can easily create and restore snapshots from OS disk attached to Azure virtual machines called New-AzureRmVmSnapshot and Restore-AzureRmVmSnapshot so I got to work.

Creating Azure Snapshots

First, I need to figure out how to create an Azure virtual machine snapshot in the first place. To do that, I first needed to shut down the VM that I wanted to create a snapshot for.

Stopping the Azure VM

Creating the snapshot

Once the VM was stopped, I then learned how I could create a snapshot using the New-AzureRmSnapshot cmdlet but it, unfortunately, wasn’t that easy. To gather up all of the information I needed required first finding the OS disk that was attached to the VM, creating a config and then I could create the snapshot.

Create a Disk from the Snapshot

Once the snapshot was created, you actually have to create a new Azure disk from the snapshot which requires creating another disk config and creating the disk with the New-AzureRmDisk cmdlet.

Yay! We have created our first Azure virtual machine snapshot!

Restoring an Azure virtual machine Snapshot

Now let’s say you’ve been messing around in the VM and want to restore the snapshot. This is the step that was a challenge because I could find no great step-by-step tutorial to do this. I eventually found out how to detach the old Azure OS disk from the VM and attach the new one.

Detach the Old OS Disk and Reattach new one

At this point, the snapshot should be restored but you’ve still got that old OS disk hanging around. Go ahead and clean that up.

Delete the Old OS Disk

Does all of this sound like a major pain? It did me too! Luckily, my pain is your gain! Here are two functions to take a snapshot of an OS disk attached to a VM and one to restore it. They are nothing fancy and could be improved greatly

Functions

 

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to “Rename” Amazon S3 “Folder” Objects with Python

 

To rename a folder on a traditional file system is a piece of cake but what if that file system wasn’t really a file system at all? In that case, it gets a little trickier! Amazon’s S3 service consists of objects with key values. There are no folders or files to speak of but we still need to perform typical filesystem-like actions like renaming folders.

Renaming S3 “folders” isn’t possible; not even in the S3 management console but we can perform a workaround. We can create a new “folder” in S3 and then move all of the files from that “folder” to the new “folder”. Once all of the files are moved, we can then remove the source “folder”.

To do this, I’ll be using Python and the boto3 module. If you’re working with S3 and Python and not using the boto3 module, you’re missing out. It makes things much easier to work with.

Prerequisites

For the demonstration I’ll be showing you to work, you’ll need to meet a few prereqs ahead of time:

  • MacOS/Linux
  • Python 3+
  • The boto3 module (pip install boto3 to get it)
  • An Amazon S3 Bucket
  • An AWS IAM user access key and secret access key with access to S3
  • An existing “folder” with “files” inside in your S3 bucket

Renaming an Amazon S3 Key

To rename our S3 folder, we’ll need to import the boto3 module and I’ve chosen to assign some of the values I’ll be working with as variables.

Once I’ve done that, I’ll need to authenticate to S3 by providing my access key ID and secret key for the IAM user I’ll be using. In this case, I’ve chosen to use a boto3 session. I’ll be using a boto3 resource to work with S3.

Once I’ve done that, I then need to find all of the files matching my key prefix. You can see below that I’m using a Python for loop to read all of the objects in my S3 bucket. I’m using the optional filter action and filtering all of the S3 objects in the bucket down to only the key prefix for the folder I want to rename.

Once I’ve started the for loop iterating over the “folder” key and all of the “file” keys inside of it, I’ll then need to exclude the “folder” key itself since I won’t be copying that. I just need the file keys. I’m excluding that by an if statement that matches all key values that don’t end with a forward slash.

After I’m in the block that will only contain file key values, I’m now assigning the file name and destination key names to make it easier to reference.

Once I have all of that setup, I then finally do the actual copy using the copy_from action. You can see below that I’m creating an S3 object using the bucket name and destination file key. I’m then passing the source key to the copy_from action.

Once the loop has finished and all of the files have been copied to the new key, I’ll then need to use the delete action to clean all of the files including the “folder” key since it is not inside of the if condition.

At this point, we’re done! You should now see all of the files that were previously in the source key under the destination key with no sign of the source key!

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to Write to a Text File with PowerShell

A common need amongst IT professionals is to create and append to text files. The ubiquitous nature of a plain-text file lends itself to lots of uses. In a PowerShell script, we can quickly create and append to plain-text files using a couple of built-in cmdlets called Add-Content and Set-Content. In this article, let’s go over how we can use both of these cmdlets to handle all of our text file needs.

Both the Add-Content and Set-Content cmdlet both perform similar functions; creating and writing to text files but they behave a little differently. The biggest difference is one always appends text to a file while the other always overwrites it. This is important to know when you’re writing to an important log file!

Continue reading “How to Write to a Text File with PowerShell”

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to Create a PSCredential Object Without Using Get-Credential in PowerShell

PSCredential objects are commonplace in PowerShell. They are a creative way to store and pass credentials to various services securely. Many built-in and third-party cmdlets require PSCredential objects on many different commands.

Typically, to create a PSCredential object, we’d use the Get-Credential cmdlet. The Get-Credential cmdlet is the most common way that PowerShell receives input to create the PSCredential object like the username and password.

Continue reading “How to Create a PSCredential Object Without Using Get-Credential in PowerShell”

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

How to Reference an Object Property in a String in PowerShell

Strings by themselves are a simple data type in PowerShell. Merely surrounding a set of characters with single or double quotes creates a string. But whenever you need to insert an expression inside of that string is when things get a little more complicated. This process is typically known as string interpolation.
By default, PowerShell interprets a string as a literal. For example, just typing 'This is a string' in PowerShell creates a string just fine. However, when you need to insert some expression like a variable or an object property in our case, you have to use double quotes.
String Interpolation
In this case, we used a single variable. To get an expression to expand inside of a string, we just need to insert the variable inside of double quotes but happens when we try to expand an object property?

Continue reading “How to Reference an Object Property in a String in PowerShell”

Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.