Containers

Docker Fundamentals for Newbies: All You Need to Work with Docker

Damian Igbe, Phd
Sept. 27, 2024, 9:58 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

Introduction to Docker

Docker is a platform that simplifies the development, deployment, and running of applications using containerization. Containers are lightweight, portable, and ensure consistency across various environments. This guide will cover the basics of Docker and include hands-on lab exercises to help you practice.

Key Concepts

1. What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers package an application and its dependencies into a single unit that runs consistently on any environment.

2. Containers vs. Virtual Machines

  • Containers: Share the host system’s OS kernel and are more lightweight and faster to start.
  • Virtual Machines: Run a full OS instance and are more resource-intensive.

3. Docker Architecture

  • Docker Engine: The runtime that manages Docker containers.
  • Docker Images: Read-only templates used to create containers. They include the application code, libraries, and dependencies.
  • Docker Containers: Instances of Docker images that are running processes.
  • Docker Hub: A cloud-based registry where you can find and share Docker images.

Basic Docker Commands

1. Installing Docker

  • Windows/Mac: Download and install Docker Desktop from Docker's official site.
  • Linux: Install Docker using your package manager, e.g., sudo apt-get install docker-ce for Ubuntu.

2. Basic Docker Commands

  • docker --version: Check Docker version.
  • docker pull <image>: Download an image from Docker Hub.
  • docker images: List available Docker images.
  • docker run <image>: Create and start a container from an image.
  • docker ps: List running containers.
  • docker stop <container_id>: Stop a running container.
  • docker rm <container_id>: Remove a stopped container.
  • docker rmi <image_id>: Remove a Docker image.

Lab Exercises

Exercise 1: Setting Up Docker

  1. Install Docker:
    • Follow the installation steps for your operating system.

     2. Verify Installation:

    • Open a terminal and run docker --version to ensure Docker is installed correctly.

Exercise 2: Working with Docker Images

  1. Pull a Docker Image:
    • Run docker pull hello-world to download a test image from Docker Hub.

      2. List Docker Images:

    • Run docker images to see the list of images on your system.

      3. Run a Docker Container:

    • Execute docker run hello-world to create and run a container from the hello-world image.

Exercise 3: Managing Containers

  1. Run a Container:
    • Start a container with a more interactive image, such as ubuntu: docker run -it ubuntu.
    • Install a package inside the container, for example, apt-get update && apt-get install -y curl.

      2. List Running Containers:

    • Use docker ps to see currently running containers.

     3. Stop and Remove a Container:

    • Stop a running container: docker stop <container_id>.
    • Remove a stopped container: docker rm <container_id>.

Exercise 4: Working with Docker Images

  1. Create a Custom Docker Image:

Create a file named Dockerfile with the following content:

FROM ubuntu

RUN apt-get update && apt-get install -y curl

CMD ["bash"]

  • Build the Docker image with: docker build -t my-ubuntu-image .

    2. Run Your Custom Image:

    • Start a container from your custom image: docker run -it my-ubuntu-image.

    3. Remove Docker Images:

    • List images with docker images.
    • Remove an image with docker rmi <image_id>.

Exercise 5: Using Docker Hub

  1. Search for Docker Images:
    • Search for images using: docker search <image_name>.

     2. Push an Image to Docker Hub:

    • First, create a Docker Hub account and log in from your terminal: docker login.
    • Tag your image with your Docker Hub username: docker tag my-ubuntu-image <username>/my-ubuntu-image.
    • Push the image to Docker Hub: docker push <username>/my-ubuntu-image.

    3. Pull Your Image from Docker Hub:

    • Pull the image using: docker pull <username>/my-ubuntu-image.

Conclusion

Understanding these Docker fundamentals and completing the lab exercises will give you a solid foundation in containerization. Docker allows you to manage applications in a consistent environment, making development and deployment more efficient. Continue practicing these commands and concepts to build your expertise.

 

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role