Introduction

In this section, we will explore how PowerShell can be used to manage Docker containers. Docker is a platform that allows you to automate the deployment, scaling, and management of applications inside lightweight containers. PowerShell provides a set of cmdlets that make it easy to interact with Docker, allowing you to manage containers, images, networks, and volumes.

Key Concepts

  1. Docker Containers: Lightweight, standalone, and executable packages that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
  2. Docker Images: Read-only templates used to create containers. Images are built from a set of instructions written in a Dockerfile.
  3. Dockerfile: A text document that contains all the commands a user could call on the command line to assemble an image.
  4. Docker Hub: A cloud-based repository where Docker users and partners create, test, store, and distribute container images.

Setting Up Docker with PowerShell

Prerequisites

  • Docker must be installed on your system. You can download Docker from Docker's official website.
  • PowerShell should be installed and updated to the latest version.

Installing Docker Module for PowerShell

To interact with Docker from PowerShell, you need to install the Docker module. Open PowerShell and run the following command:

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force

Importing the Docker Module

After installing the module, import it into your PowerShell session:

Import-Module -Name DockerMsftProvider

Basic Docker Commands in PowerShell

Pulling an Image

To pull an image from Docker Hub, use the docker pull command. For example, to pull the latest Ubuntu image:

docker pull ubuntu:latest

Listing Images

To list all the Docker images on your system, use the docker images command:

docker images

Running a Container

To run a container from an image, use the docker run command. For example, to run an Ubuntu container interactively:

docker run -it ubuntu:latest /bin/bash

Listing Containers

To list all running containers, use the docker ps command:

docker ps

To list all containers, including stopped ones, use:

docker ps -a

Stopping a Container

To stop a running container, use the docker stop command followed by the container ID or name:

docker stop <container_id>

Removing a Container

To remove a stopped container, use the docker rm command:

docker rm <container_id>

Removing an Image

To remove an image, use the docker rmi command:

docker rmi <image_id>

Practical Example: Creating and Running a Custom Docker Image

Step 1: Create a Dockerfile

Create a file named Dockerfile with the following content:

# Use the official Ubuntu image as a base
FROM ubuntu:latest

# Install basic utilities
RUN apt-get update && apt-get install -y \
    curl \
    vim \
    git

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Run a command to verify the setup
CMD ["echo", "Hello, PowerShell and Docker!"]

Step 2: Build the Docker Image

Navigate to the directory containing the Dockerfile and run the following command to build the image:

docker build -t my-custom-image .

Step 3: Run the Docker Container

Run a container from the newly created image:

docker run my-custom-image

You should see the output: Hello, PowerShell and Docker!

Exercises

Exercise 1: Pull and Run a Docker Image

  1. Pull the nginx image from Docker Hub.
  2. Run a container from the nginx image.
  3. List all running containers to verify that the nginx container is running.

Solution

# Pull the nginx image
docker pull nginx:latest

# Run a container from the nginx image
docker run -d -p 8080:80 nginx:latest

# List all running containers
docker ps

Exercise 2: Create and Run a Custom Docker Image

  1. Create a Dockerfile that installs nodejs and copies a simple Node.js application into the container.
  2. Build the Docker image.
  3. Run a container from the custom image and verify that the Node.js application runs correctly.

Solution

  1. Create a Dockerfile:
# Use the official Node.js image as a base
FROM node:latest

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install dependencies
RUN npm install

# Expose the application port
EXPOSE 3000

# Run the application
CMD ["node", "app.js"]
  1. Build the Docker image:
docker build -t my-node-app .
  1. Run a container from the custom image:
docker run -d -p 3000:3000 my-node-app

Conclusion

In this section, we covered the basics of using PowerShell to manage Docker containers. We learned how to install the Docker module, pull images, run containers, and create custom Docker images. These skills are essential for automating and managing containerized applications using PowerShell. In the next module, we will explore best practices and advanced tips for writing readable and maintainable PowerShell code.

PowerShell Course

Module 1: Introduction to PowerShell

Module 2: Basic Scripting

Module 3: Working with Objects

Module 4: Advanced Scripting Techniques

Module 5: Automation and Task Scheduling

Module 6: PowerShell Remoting

Module 7: Advanced PowerShell Features

Module 8: PowerShell and DevOps

Module 9: Best Practices and Advanced Tips

© Copyright 2024. All rights reserved