Introduction to Docker and Basic Commands

Photo by Ian Taylor on Unsplash

Introduction to Docker and Basic Commands

Hey there! Welcome to the Docker blog series. Throughout this series, you'll get a full understanding of Docker.

As a software developer, you’re probably familiar with deploying applications in various environments. One of the most frustrating aspects of this process is dealing with inconsistencies across different environments, whether it's the developer's local machine, a test server, or production. Docker eliminates these inconsistencies by enabling developers to package applications and their dependencies in portable containers.

This blog will provide a deep dive into Docker’s basics, focusing on how you can leverage Docker to streamline your development process. We'll cover key concepts, installation, and basic commands to get you up and running efficiently.

At its core, Docker is a containerization platform. Containers are isolated environments where applications run. Unlike virtual machines, which emulate entire operating systems, containers share the host system’s OS kernel while providing isolated user spaces for each application. This lightweight architecture allows containers to spin up quickly and run efficiently, making them ideal for both development and production environments.

  • Docker Containers: Share the host’s OS kernel, consume fewer resources, and are lightweight. Containers boot up in milliseconds.

  • Virtual Machines: Include a full OS, consume more resources, and take longer to boot up since they need to load a complete operating system.

Advantages of Docker Containers:

  • Portability: Containers can be run on any system that has Docker installed.

  • Consistency: A Docker container behaves the same across development, staging, and production.

  • Resource Efficiency: Multiple containers can run on a single host without the overhead of multiple OS kernels.

When to Use Docker:

  • When you need to ensure your application runs the same way across different environments.

  • When working with microservices architectures where each service can be containerized.

  • In continuous integration/continuous deployment (CI/CD) pipelines, where containers ensure consistent builds.

Before diving into Docker commands, let’s ensure Docker is installed on your system.

Docker provides Docker Desktop, an all-in-one package that simplifies running Docker on both Windows and macOS. It includes the Docker Engine, Docker CLI, Docker Compose, and Kubernetes support.

Steps:

  1. Download Docker Desktop from the Docker website.

  2. Follow the installation wizard.

  3. On Windows, make sure Windows Subsystem for Linux (WSL 2) is enabled.

  4. Once installed, open Docker Desktop and ensure it’s running.

sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker
sudo systemctl enable docker

After installation, verify it’s working:

docker --version

You should see something like Docker version 24.0.2, build 123abc.


Before jumping into commands, let’s quickly define Docker’s primary components:

  • Image: A read-only template with the application and its dependencies.

  • Container: A runnable instance of an image.

  • Dockerfile: A script that defines the steps to create an image.

  • Registry: A repository for storing images (e.g., Docker Hub).

  • Volume: A persistent storage mechanism for Docker containers.


A Docker image contains everything needed to run an application, including the code, libraries, environment variables, and runtime. You can pull images from Docker Hub, which hosts thousands of pre-built images.

docker pull <image_name>

Example:

docker pull nginx

This command pulls the official NGINX image from Docker Hub. You can view the downloaded image using:

docker images

Once an image is pulled, you can create and run a container from it.

docker run <image_name>

Example:

docker run nginx

This command creates a running instance of the NGINX container. By default, the container will run in the foreground, showing the logs from NGINX. To run the container in the background (detached mode), use the -d flag:

docker run -d nginx

Running a container with port binding:

docker run -d -p 8080:80 nginx

Here, port 8080 on your host machine is bound to port 80 inside the container.

To see the list of currently running containers, use the docker ps command:

docker ps

Output:

CONTAINER ID   IMAGE    COMMAND                  CREATED       STATUS        PORTS               NAMES
abc123         nginx    "/docker-entrypoint.…"   5 seconds ago Up 3 seconds  0.0.0.0:8080->80/tcp hopeful_varahamihira

To list all containers, including stopped ones:

docker ps -a

To stop a running container, use:

docker stop <container_id>

For example:

docker stop abc123

If you want to remove a stopped container:

docker rm <container_id>

If you need to remove a container forcefully:

docker rm -f <container_id>

To remove an image from your system, you need the image ID or tag:

docker rmi <image_id>

For example:

docker rmi nginx

Docker will only allow you to remove images that aren’t being used by running containers. If you get an error, you may need to stop and remove associated containers first.

To view the logs from a container:

docker logs <container_id>

For example:

docker logs abc123

To inspect detailed information about a container, including its IP address, storage mounts, and configuration:

docker inspect <container_id>

This outputs a JSON object with all the details of the container.

For interactive troubleshooting or experimentation, you can run a container with a bash shell:

docker run -it <image_name> /bin/bash

For example, if you want to experiment within a running Ubuntu container:

docker run -it ubuntu /bin/bash

This will drop you into a bash shell inside the Ubuntu container.


Docker Hub is a public registry where you can find official images for popular software like NGINX, Redis, MySQL, Node.js, and more. As an experienced developer, you’ll want to become comfortable with finding and pulling the correct images from Docker Hub, as it will save you time in configuring environments manually.

Example:

docker search mysql

This command searches for MySQL images on Docker Hub. It’s important to always look for official images when possible, as they are regularly updated and maintained.

You can also create your own private Docker Hub repositories for storing images:

docker push <username>/<image_name>

Docker simplifies the process of building, testing, and deploying applications by providing a consistent environment across all stages of development. With Docker, you can pull images from public repositories, run containers effortlessly, and even create your own custom images.

In this first blog, we've introduced Docker and covered essential commands. In subsequent blogs, we’ll explore more advanced topics like networking, custom Dockerfiles, and multi-container applications.

Did you find this article valuable?

Support Bit Fetch by becoming a sponsor. Any amount is appreciated!