Intro to Docker with Simple Exercises

Intro to Docker with Simple Exercises

·

6 min read

Introduction

Hey friends! Ready to chat about something cool? Today, we're diving into Docker – a tool that makes life easy for your computer programs. No techy mumbo-jumbo, just a straightforward guide to get you going. Let's roll!

Before jumping into What is Docker? Lets understand WHY DOCKER?

Why Docker?

Let's imagine you're a parent managing a household with multiple kids, each with their own set of toys and preferences.

Traditionally, organizing this could be like having all the toys in one big play area. If you want to update or customize one child's play corner, it might affect the other kids' areas, and toys could get mixed up.

Now, think of Docker as a way to organize the toys more efficiently. Each child gets their own designated play space, like separate toy boxes or shelves. If you need to add or change toys for one child, you can do it without disturbing the others – they all have their own toy spaces.

So, Docker helps you manage your household (or software) by isolating different kids' toys into their own toy boxes. Each box contains the toys (code) and instructions on how to play with them (dependencies). Changes to one child's toy collection won't disrupt the others, making it easier to maintain a happy and organized household. Docker is like having personalized toy boxes for each child, ensuring that modifications are contained and don't create a playtime mess for everyone.

What is Docker?

Docker is a container platform that separates applications from the underlying infrastructure.

It bundles code and dependencies into a self-contained unit, ensuring consistency across different systems.

Analogy: Docker Containers vs. Physical Shipping Containers

  • Shipping containers transport various items; consistent size simplifies management.

  • Similarly, Docker containers bundle software, ensuring consistent deployment regardless of underlying infrastructure.

Challenges in Software Development:

  • Deploying code, managing dependencies, and handling rollbacks are complex.

  • Development and production environments rarely identical, leading to challenges.

Docker Containers: Key Concepts

  • Isolation: Containers isolate processes, network, and resources.

  • Versatility: Compatible with any programming language or Linux distribution.

  • Packaging: Bundles code, libraries, and binaries into a single executable unit.

Docker vs. Virtual Machines (VMs):

  • VMs: Emulate hardware, run full OS; slower startup, resource-intensive, requires patching, large disk space.

  • Docker: Shares kernel with host OS, eliminates guest OS; faster startup, efficient resource usage, minimal disk space.

Docker Architecture:

  • Host OS: Core operating system.

  • Docker Engine: Manages containers and execution.

  • Binaries, Libraries, Application Code: Components bundled into a Docker container.

  • Kernel Sharing: Containers share the host OS's kernel.

Advantages of Docker:

  • Standardization: Ensures consistent deployment for applications.

  • Efficiency: Faster startup, minimal resource consumption.

  • Portability: Applications run the same way across different environments.

  • Docker simplifies deployment, ensuring applications run consistently.

Docker on AWS - Playing with Docker in the Cloud

Getting Started

To kick things off, head over to the AWS Management Console by clicking on the magic button that says "Open Cloud Environment."

Use the credentials to sign in and step in AWS Management Console.

Connecting to the Docker Host

  1. In the AWS Management Console, search for EC2 in the top search bar, and click on the EC2 result under Services.

    Note : You should know how to create instances.

  2. Navigate to Instances in the left-hand menu.

  3. Select the instance named "docker-playground." (I have created an instance)

  4. Click on Connect at the top.

  5. In the Connect to Instance dialogue, select the EC2 Instance Connect tab and enter the following:

    • User name: ec2-user

  6. Click Connect at the bottom.

Now comes the fun part – exploring Docker in this AWS playground. Here are some suggested activities to get you started:

  1. Run the hello-world Docker image:

     docker run hello-world
    
  2. When executed command, docker will search Hello-World image locally. Since image is not available, it will be pulled from docker hub (a place like playstore to download images).

    Inspect the hello-world image: Use the Docker command-line interface to inspect the details of the hello-world image.

     docker inspect hello-world:latest
    

    When executed inspect command, output of image configuration shows up as result.

  3. Create a new tag for the hello-world image: Docker tagging with the tag sub-command.

     docker tag hello-world:latest hello-world:v1
    

    Creating existing image of different version using 'tag' command

Docker Playtime - Hands-On Fun with Exercises

Note: These are my own exercises but you can follow these methods and commands to learn and understand.

Exercise 1: Find the Name of a File in a Container

Docker container named "pine" has file called 'd0ck3r'. Follow these steps to unveil the file and bring it into the light. Also copy file to your pwd.

docker exec pine ls /tmp 
#file name returns as output
docker cp pine:/tmp/d0ck3r /home/ec2-user/
#dockerfile is copied to my current dir

Exercise 2: Run an Nginx Web Server Container

Execute the following command to launch an Nginx container on your Docker host:

docker run -d -p 8888:80 --name web nginx:1.19.0
  • d: Run the container in the background (detached mode).

  • -p 8888:80: Map port 8888 on the Docker host to port 80 on the container. This allows you to access the Nginx web server on port 8888 of the host machine.

  • --name web: Assign the name "web" to the container.

  • nginx:1.19.0: Use the Nginx image with the specified tag (version 1.19.0).

Navigate to http://localhost:8888 in your browser, and behold the power of Nginx!

Exercise 3: Create an Image Using a Dockerfile

Create a Dockerfile with the following content:

# Use the busybox image with tag 1.31.1 as the base image
FROM busybox:1.31.1

# Set the working directory
WORKDIR /app

# Expose port 5000
EXPOSE 5000

# Set the default command to listen for incoming TCP connections on port 5000
CMD ["nc", "-lv", "-p", "5000"]

Build the image:

docker build -t myapp:0.0.1 /path/to/directory/containing/Dockerfile

Replace /path/to/directory/containing/Dockerfile with the actual path to the directory containing your Dockerfile.

This command will build an image named myapp with the tag 0.0.1 based on the configurations specified in your Dockerfile. The EXPOSE instruction is used to inform Docker that the container will listen on the specified network ports at runtime.

Run a container:

docker run -p 5000:5000 --name myapp_instance myapp:0.0.1

This command maps port 5000 on the Docker host to port 5000 on the container and assigns the name myapp_instance to the running container. You should now be able to connect to the container's port 5000 from the host machine.

Exercise 4: Add a Custom Tag to an Image

For the final challenge, let's add a touch of customization to an existing Alpine Linux image. Execute the following command to give it a custom tag:

docker tag alpine:latest alpine:challenge

Check using below command:

docker images

There you have it, Docker enthusiasts! These exercises are your gateway to understand Docker commands and techniques. Keep exploring, experimenting, and building with Docker. Happy containerizing! 🚀🐳

Did you find this article valuable?

Support Farhan's Scripted Explorations by becoming a sponsor. Any amount is appreciated!