Lesson 1
Introduction to Docker and Containerization
Introduction to Docker and Containerization

Welcome to the first lesson of our Docker Mastery path. We're diving into the world of containerization, a fundamental shift in how applications are developed and deployed. Containerization allows for packaging an application along with its dependencies into a single unit called a container. This ensures consistency across different computing environments, making it easier to develop, ship, and run applications seamlessly.

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications within containers. It provides a lightweight and efficient way to package applications and their dependencies, ensuring consistency across different environments. By using Docker, developers can focus on writing code without worrying about compatibility issues arising from differences in operating systems or software versions. Docker achieves this by leveraging containerization technology to isolate applications from the underlying infrastructure, making it easier to develop, test, and deploy applications quickly and reliably.

Why is Docker Useful?

Docker is highly advantageous for teams due to its ability to streamline the development and deployment processes. By encapsulating applications in containers, Docker ensures that software runs consistently regardless of where it's deployed—be it a developer's laptop, a test server, or a production environment.

Imagine a software development team working on a web application. Each developer uses a different operating system, causing issues with dependencies and software versions. With Docker, the team can package the application and its dependencies into a container, creating a consistent development environment. This eliminates "but it works on my machine" issues, allowing the team to be more productive and reducing integration headaches.

Additionally, Docker's quick start-up times enable faster testing cycles, enhancing the overall agile development process and speeding up the time to market.

Key Docker Concepts

To understand Docker, we need to familiarize ourselves with a few key concepts:

  • Docker Images: Think of images as blueprints or templates for containers. Each image contains everything needed to run an application, including the code, runtime, libraries, and settings. Importantly, from a single Docker image, you can create multiple containers—much like how you can build several houses from one blueprint.

  • Docker Containers: Containers are running instances of Docker images. They are isolated environments that execute your application without affecting the host system, ensuring it runs the same way regardless of where it's deployed. You can create multiple containers from the same image, enabling different environments or configurations as needed.

For example, if you have a Docker image of a Java application, you can create multiple containers from this image. Each container runs an instance of the Java application, allowing for scalability and redundancy. The diagram below illustrates this concept:

In this diagram, a single Java Docker image is used to create three separate containers, each running an instance of the Java application.

Is Docker Similar to a Virtual Machine?

Docker and virtual machines (VMs) may seem similar because they both provide isolated environments for applications, but they differ fundamentally in how they achieve this isolation.

  • Virtual Machines: A VM includes a full copy of an operating system, the application, necessary binaries, and libraries—making it heavier and requiring more resources. VMs run on a hypervisor that emulates hardware for each VM, allowing multiple VMs to run on a single physical machine. This offers strong isolation but at the cost of performance and efficient resource usage.

  • Docker Containers: In contrast, Docker containers share the host system's OS kernel, making them much lighter and faster to start. Containers package the application and its dependencies, but they do not include an entire OS, relying instead on features provided by the host OS. This results in faster performance and more efficient resource utilization compared to VMs.

Thus, while both technologies allow for application isolation, Docker containers provide a more lightweight and efficient solution, especially suited for cloud-native and distributed applications where quick scaling and portability across environments are critical.

Setting Up Docker

To begin working with Docker on your machine, you need to install Docker Desktop. This comprehensive tool includes Docker Engine, Docker CLI, Docker Compose, and other essential components, providing everything you need to develop, test, and manage containers seamlessly. By installing Docker Desktop, you'll be equipped with:

  • The ability to use the Docker Command Line Interface (CLI) for executing commands such as managing containers, images, and networks.
  • Access to a user-friendly graphical interface to easily monitor and manage your Docker setup.

In this course, however, we will provide a fully configured environment where Docker is pre-installed and ready to use. This means you can focus on learning and practicing without worrying about setting up the environment from scratch. For those who wish to install Docker on their own systems, detailed instructions can be found in Docker's official documentation.

Interacting with Docker: CLI, Daemon, Engine

To effectively work with Docker, it's important to understand its architecture and how interaction takes place within this system. Let's break down some core components:

  • Docker CLI: The Docker Command Line Interface (CLI) is the primary tool used to interact with Docker. It allows developers to issue commands that are executed by the Docker Daemon. The docker keyword is used at the start of the command, followed by specific subcommands and options to carry out tasks such as managing containers, images, networks, and more. The CLI acts as the user-facing interface for Docker operations.

  • Docker Daemon: The Docker Daemon (or dockerd) is a background service running on the host system that manages Docker objects like images, containers, networks, and volumes. It listens for API requests, both from the CLI and other tools, and performs the necessary actions to build, run, and manage Docker containers. The daemon responds to commands from the Docker CLI to facilitate the overall Docker workflow.

  • Docker Engine: At the core of Docker is the Docker Engine, a client-server application that includes three major components: the server (Docker Daemon), a REST API, and the client (Docker CLI). The REST API manages communication between the client and server, facilitating container management tasks such as building, running, and distributing Docker containers. This architecture provides the underlying platform for developing and running containers efficiently.

By understanding these components, you can grasp how Docker operates as a whole. When you use the docker keyword, you're directly interacting with the Docker CLI, which sends your commands to the Docker Daemon via the API. The Daemon then executes these commands to manage Docker resources and environments. This interaction model is crucial for effectively harnessing the power of Docker in application development and deployment.

Running Your First Docker Container

Running your first Docker container is an essential and often the first step when learning Docker. It provides a simple way to ensure your Docker setup is working correctly. The hello-world image is specially designed for this purpose; it's a minimal Docker image that, when run, displays a welcome message to confirm that Docker is fully operational on your system.

To get started, open your terminal and type:

Bash
1# Run the hello-world Docker image 2docker run hello-world

Here's what happens when you run this command:

  • Docker checks if the hello-world image is already on your computer.
  • If it isn't, Docker downloads it from a place called Docker Hub, an online library of pre-built images.
  • Once the image is ready, Docker starts the container and runs a small program that shows a welcome message.

The output you see should look like this:

Plain text
1Unable to find image 'hello-world:latest' locally 2latest: Pulling from library/hello-world 3c1ec31eb5944: Pull complete 4Digest: sha256:d211f485f2dd1dee407a80973c8f129f00d54604d2c90732e8e320e5038a0348 5Status: Downloaded newer image for hello-world:latest 6 7Hello from Docker! 8This message shows that your installation appears to be working correctly. 9 10To generate this message, Docker took the following steps: 11 1. The Docker client contacted the Docker daemon. 12 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 13 (amd64) 14 3. The Docker daemon created a new container from that image which runs the 15 executable that produces the output you are currently reading. 16 4. The Docker daemon streamed that output to the Docker client, which sent it 17 to your terminal. 18 19To try something more ambitious, you can run an Ubuntu container with: 20 $ docker run -it ubuntu bash 21 22Share images, automate workflows, and more with a free Docker ID: 23 https://hub.docker.com/ 24 25For more examples and ideas, visit: 26 https://docs.docker.com/get-started/

This shows Docker is installed correctly and gives you a glimpse of how easy it is to run applications using Docker. For more details on how this command works, refer to the official Docker run command documentation.

Listing Docker Images

To see which Docker images are currently saved on your computer, use this command:

Bash
1# List all Docker images on your system 2docker images

After you've run docker run hello-world, the docker images command should show:

Plain text
1REPOSITORY TAG IMAGE ID CREATED SIZE 2hello-world latest d2c94e258dcb 17 months ago 13.3kB

This list tells you that the hello-world image is now on your computer. You can learn more about the images command by checking the Docker documentation on listing images.

Listing Running Containers

To find out which containers are running at the moment, use the command:

Bash
1# List currently running Docker containers 2docker ps

Since the hello-world container stops right after showing its message, you won't see it running:

Plain text
1CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

This will appear empty if no containers are actively running. For more information, refer to the Docker documentation on listing containers.

Listing All Containers

To view all containers on your system, whether they are currently running or have been stopped, you can use the following command.

Bash
1# List all Docker containers, both running and stopped 2docker ps -a

By adding the -a option, Docker will list all containers, not just the actively running ones. This command will display a comprehensive list of every container, showing those that have finished executing, like the hello-world container. For example, the output might look like this:

Plain text
1CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2981fddc7cbd2 hello-world "/hello" About a minute ago Exited (0) About a minute ago tender_gagarin

Here's a beginner-friendly explanation of each column and the values you might see:

  • CONTAINER ID: A unique identifier for each container. This serves as a shorthand reference for Docker to perform operations on the container.
  • IMAGE: The Docker image that was used to create the container. In this case, "hello-world" was the image utilized.
  • COMMAND: The command that runs when the container starts. For the hello-world container, it executed "/hello".
  • CREATED: The time elapsed since the container was created. For example, "About a minute ago" indicates it was created within the last minute.
  • STATUS: Displays the current state of the container. "Exited (0)" means the container has finished running successfully, and the number "0" typically indicates no errors occurred.
  • PORTS: Lists any network ports the container is using to communicate with the outside world. This might be blank if no ports are configured.
  • NAMES: A user-friendly name automatically assigned to the container, such as "tender_gagarin". This name can be used to identify the container instead of the long CONTAINER ID.
Summary and Practice Preparation

In this lesson, we've covered the essentials to get you started with Docker. We've explored how Docker fits into the broader concept of containerization, understood its core components, and walked through basic commands to manage images and containers. These fundamentals form the bedrock of your Docker journey. Understanding and using these commands will prepare you for more advanced Docker functionalities we’ll cover in future lessons.

Now, as you move forward to hands-on practice, try out these commands on CodeSignal's pre-installed environment and your own system. Practicing these steps will reinforce your learning and help you get comfortable with Docker's toolset. Good luck, and enjoy exploring the world of containerization!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.