Docker for Dummies: A Simple Introduction for Beginners

Docker for Dummies: A Simple Introduction for Beginners

Hey there! Today, I want to talk about one of my favorite tools, something that's become really important in the world of software development and operations: Docker. If you've been hearing a lot about it but aren't quite sure what it is or why it matters, you're in the right place. Let's dive into the basics of Docker.

What is Docker?

Imagine you're working on a project that needs a specific environment to run perfectly. Maybe it requires a certain version of Python, a particular database setup, and a handful of specific libraries. Setting this up on your computer is one thing, but what if you need to share your project with someone else? Or deploy it on a server? Or you just don't want to install it in your machine just for a single project? This is where Docker comes in.

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers are like lightweight, portable virtual machines. They bundle up your application with everything it needs to run – all the code, libraries, dependencies, and even parts of the operating system – into a single package. This means you can run your container on any machine that has Docker installed, and it will work exactly the same way.

Getting Started with Docker

Let’s go through some basic steps to get you started with Docker.

Step 1: Install Docker

First things first, you need to install Docker on your machine. You can download it from the Docker website and follow the installation instructions for your operating system.

Note: Windows users must install WSL 2 before installing Docker. Follow this guide by Microsoft to do so.

Step 1a: (Just for Windows users)

After the successful installation of Docker, open the Windows PowerShell and write:

wsl -l -v

If this gives you three things (at least) - one OS (Ubuntu, most probably), docker-desktop, and docker-desktop-data, then everything has been installed correctly.

Now start the Linux environment by writing (You can replace 'Ubuntu' with any other OS that you saw in the result of wsl -l -v):

wsl -d Ubuntu

This will start the Linux shell in the terminal, like the one shown below:

You will carry out all the Docker commands in this Linux shell.

If you want to shut down the Linux environment, type exit in the Linux shell and press Enter. Then write in the Windows PowerShell:

wsl --terminate Ubuntu

You can check the running or stopped status using wsl -l -v.

For the purpose of the tutorial, start the Linux environment again with wsl -d Ubuntu.

Step 2: Understand Docker Images and Containers

  • Docker Image: An image is a read-only template with instructions for creating a Docker container. Think of it as a blueprint for your application. It is an actual file that you can download or upload.

  • Docker Container: A container is a runnable instance of an image. It’s a bit like a virtual machine, but much more lightweight. It is not a file that can be downloaded or uploaded.

Step 3: Run Docker Desktop

Docker Desktop will start a Docker 'daemon' on your machine, which lets you perform container-related operations with Docker.

Step 4: Pull an Image

Docker Hub is a repository where you can find a variety of Docker images. Let’s say you want to run a basic web server using Nginx. You can pull the Nginx image from Docker Hub like this:

docker pull nginx

This will download the 'nginx' image on your machine. (Remember, I told you that a Docker image is an actual file that can be downloaded)

Step 5: Run a Container

Once you have the image, you can create and run a container from it. Here’s how you can start an Nginx container:

docker run -d -p 80:80 nginx
  • -d runs the container in detached mode (in the background).

  • -p 80:80 maps port 80 on your host to port 80 in the container.

Now, if you open your web browser and go to localhost:80, you should see the Nginx welcome page! You successfully configured Docker and ran your first Docker container. Congratulations 🎉

Step 6: Managing Containers

You can see all running containers with:

docker ps

If you want to see all the containers - running or stopped, then write:

docker ps -a

To stop a running container, use:

docker stop <container_id>

To remove a container, use:

docker rm <container_id>

Step 7: Executing Containers

We’ve discussed how to start, stop, and manage Docker containers. But what if you want to interact with a container that’s already running? Maybe you need to troubleshoot an issue, check the logs, or run a command inside the container. This is where the docker exec command comes in handy.

It allows you to run commands inside a running Docker container. It’s like opening a terminal inside the container, letting you interact with it as if you were directly logged into the machine.

For example, to run a shell inside a container, use:

docker exec -it <container_id> bash
  • -it makes the session interactive (you can type commands).

  • bash specifies that you want to open a bash shell.

After running this command, you’ll be inside the container, with access to its file system and environment, just like you would on a regular Linux system. This is incredibly useful for debugging or exploring the container’s environment.

Note: Keep in mind that changes made this way won’t persist if the container is stopped and restarted, unless you commit those changes to a new image. To do so, use:

docker commit <container_id> <new_image_name>

This creates a new image with all the changes you made inside the container. You can list all the images by writing:

docker images

Here you should see your new image listed, along with other images already present in your system.

Applications of Docker

The concept of 'containers' and the introduction of Docker has revolutionized the way developers and operations teams work. Here are some common real-world applications of Docker:

  • Developers can use Docker to create consistent development environments. Instead of manually installing all the tools and dependencies needed for a project, a developer can simply pull a Docker image (like Redis, MongoDB, etc.) that contains everything.

  • Docker makes it easy to deploy microservices by isolating each service in its own container, which simplifies the process of scaling, updating, and managing individual components of an application.

  • Docker is commonly used in Continuous Integration/Continuous Deployment (CI/CD) pipelines. Containers ensure that the application is tested and deployed in a consistent environment, reducing the "it works on my machine" problem.

  • Docker is a key component of modern DevOps practices. By using it along with tools like Kubernetes and Terraform, teams can automate the deployment and management of infrastructure in a repeatable way.

  • Docker can be used to containerize legacy applications, making them easier to manage, migrate, and scale, even if they were originally designed to run on older infrastructure.

Wrapping Up

And there you have it – a very basic introduction to Docker!

For simplicity's sake, we have just scratched the surface of Docker's capabilities. It’s a powerful tool that simplifies the process of setting up and managing your development and production environments. With Docker, you can ensure that your applications run smoothly across different machines and configurations, making development and deployment a lot less stressful.

Happy Coding!


👋 Connect with me on: LinkedIn
✨ Check my Twitter (X) space: Twitter (X)
🧑‍💻 Here's my code: GitHub