Introduction to Docker: A Beginner's Guide

Introduction to Docker: A Beginner's Guide

What is Docker?

Docker is a platform that lets developers automate the deployment of applications in lightweight, portable containers. Containers package an application with all its dependencies, making sure it runs the same way on different systems.

Key Concepts:
  • Containers: Lightweight, standalone, and executable software packages that include everything needed to run an application (code, runtime, system tools, libraries, and settings).

  • Images: Read-only templates used to create containers. An image includes everything needed to run an application—base operating system, application code, runtime, and dependencies.

  • Docker Engine: The runtime that builds and runs Docker containers. It has a client-server setup, with a Docker daemon that handles building, running, and managing containers, and a REST API to interact with the daemon.

Installation

To start using Docker, you need to install it on your operating system. Here are the installation steps for different platforms:

Docker Installation Guide

Windows:

  1. Go to the Docker Desktop for WindowsT download page and download the installer.

  2. Run the installer and follow the instructions.

  3. After installation, the Docker Desktop should start automatically. If it doesn’t, start it manually.

  4. Verify the installation by opening a terminal and running: docker --version.

macOS:

  1. Go to the Docker Desktop for Mac download page and download the installer.

  2. Open the downloaded .dmg file and drag the Docker icon to the Applications folder.

  3. Open Docker from the Applications folder.

  4. Verify the installation by opening a terminal and running: docker --version.

Linux (Ubuntu):

  1. Update your existing list of packages: sudo apt update.

  2. Install a few prerequisite packages: sudo apt install apt-transport-https ca-certificates curl software-properties-common.

  3. Add the GPG key for the official Docker repository to your system: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -.

  4. Add the Docker repository to APT sources: sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable".

  5. Update the package database with the Docker packages from the newly added repo: sudo apt update.

  6. Install Docker: sudo apt install docker-ce.

  7. Verify that Docker is running: sudo systemctl status docker.

Basic Commands

Once Docker is installed, you can start using basic Docker commands:

  • docker run: Runs a command in a new container. Example: docker run hello-world.

  • docker pull: Downloads an image from a Docker registry. Example: docker pull nginx.

  • docker build: Creates an image from a Dockerfile. Example: docker build -t my-image ..

  • docker ps: Shows all running containers. Example: docker ps.

  • docker stop: Stops a running container. Example: docker stop container_id.

  • docker rm: Deletes a stopped container. Example: docker rm container_id.

Practice Exercise:
  1. Run the hello-world image: docker run hello-world.

  2. Pull the nginx image: docker pull nginx.

  3. List the running containers: docker ps.

  4. Run an nginx container: docker run -d -p 8080:80 nginx.

  5. Stop the nginx container: docker stop container_id.

  6. Remove the nginx container: docker rm container_id.

Understanding Docker Images and Containers

Images vs. Containers:

  • Images: These are unchangeable files that have everything needed to run an application, like the source code, libraries, and tools.

  • Containers: These are running instances of images. You can start, stop, move, and delete containers.

Managing Images:
  • Creating an Image: Use a Dockerfile to define the steps needed to build an image. Example:

      # Dockerfile
      FROM ubuntu:latest
      RUN apt-get update && apt-get install -y nginx
      CMD ["nginx", "-g", "daemon off;"]
    

    Build the image: docker build -t my-nginx-image ..

  • Listing Images: docker images.

  • Removing an Image: docker rmi image_id.

Managing Containers:
  • Creating and Running a Container: docker run -d my-nginx-image.

  • Listing Containers: docker ps -a.

  • Stopping a Container: docker stop container_id.

  • Removing a Container: docker rm container_id.

By learning these basic concepts and commands, you'll have a good starting point for using Docker to develop, ship, and run applications easily.