Docker Basics: Your First Steps into Containerization

Demystifying containers for developers

Introduction to Containers

Welcome to the world of Docker! In this post, we'll cover the fundamental concepts of Docker and get you started with your first containers. Docker has revolutionized how we build, ship, and run applications by introducing a standardized way to package applications and their dependencies into lightweight, portable units called containers.

What are Containers?

Imagine you're building an application. It needs specific libraries, configurations, and maybe even an operating system environment. Without containers, ensuring this environment is consistent across your development machine, testing servers, and production deployments can be a nightmare. Containers solve this by packaging your application and its entire runtime environment together. This includes:

This isolation ensures that your application runs the same way regardless of where it's deployed, eliminating the dreaded "it works on my machine" problem.

Docker vs. Virtual Machines (VMs)

It's common to confuse Docker containers with Virtual Machines. While both provide isolation, they do so differently:

Think of it this way: VMs are like separate houses, each with its own foundation and utilities. Containers are like apartments within a building, sharing the building's core infrastructure.

Getting Started with Docker

First, you'll need to install Docker Desktop on your machine. You can download it from the official Docker website:

Download Docker Desktop

Your First Container: Alpine Linux

Let's pull and run a simple Alpine Linux container. Open your terminal or command prompt and run:

docker run alpine:latest

This command does a few things:

You might notice that the command finishes quickly. By default, a container started without any command to execute will exit immediately after starting. To keep it running, we can use an interactive mode:

docker run -it alpine:latest sh

The -it flags mean:

Now you should see a shell prompt like / #. You're inside the Alpine Linux container!

You can run Linux commands here, such as ls / or pwd. To exit the container and return to your host machine, type exit.

Running a Web Server Container (Nginx)

Let's try running a web server. We'll use the official Nginx image and expose port 80:

docker run -d -p 8080:80 --name my-nginx nginx:latest

Let's break this down:

After running this, open your web browser and navigate to http://localhost:8080. You should see the default Nginx welcome page!

Inspecting and Managing Containers

You can see your running containers with:

docker ps

To see all containers, including stopped ones:

docker ps -a

To stop the Nginx container:

docker stop my-nginx

To remove a stopped container:

docker rm my-nginx

What's Next?

This is just the tip of the iceberg. Docker offers much more, including Dockerfiles for building your own images, Docker Compose for managing multi-container applications, and networking options. We'll explore these in future posts!

Experiment with different images, explore their documentation on Docker Hub, and get comfortable with the basic commands. Happy containerizing!

Posted by: The MSDN Blog Team | Date: October 26, 2023