Docker Fundamentals

Always learning
6 min read6 days ago

--

Docker is a platform that enables developers to build, ship, and run applications in containers. Containers are lightweight, portable, and efficient, making them a popular choice for modern application development and deployment.

buymeacoffee ☕ 👈 Click the link

Containers & Virtual Machines (VMs)

While both containers and VMs provide isolated environments for applications, they differ significantly:

  • VMs: Virtualize the entire hardware, including the operating system. They are heavyweight and consume more resources.
  • Containers: Virtualize the operating system, sharing the kernel with the host machine but isolating the application processes. They are lightweight and use fewer resources.

Challenges with Traditional Deployment

Traditional deployment methods often face challenges such as:

  • Dependency Conflicts: Different applications may require different versions of libraries or dependencies, leading to conflicts.
  • Environment Consistency: Applications may behave differently in various environments due to differences in configurations.
  • Scalability: Scaling applications across multiple servers can be complex and resource-intensive.

Containerization addresses these challenges by encapsulating applications and their dependencies in a container, ensuring consistency and portability across environments.

Understanding Docker Architecture

Docker follows a client-server architecture, with the main components being Docker Client, Docker Daemon, and Docker Registry.

Docker utilizes a client-server architecture with several key components:

  • Docker Client: This is the command-line interface (CLI) tool you use to interact with Docker. It allows you to build, run, and manage your containers. Think of it as your remote control for Docker.
  • Docker Engine (Daemon): This software runs on your system and manages the building, running, and distribution of containers. It listens for commands from the Docker client and puts them into action.
  • Docker Host: This is the physical machine (or virtual machine) where Docker is installed. It provides the resources and environment to run your Docker containers.

Docker Objects (Images & Containers):

  • Docker Image: A blueprint that contains instructions for creating a Docker container. It defines the environment (operating system, libraries, application code) needed to run an application. Imagine it as a recipe for creating your application environment.
  • Docker Container: A running instance of a Docker image. It’s lightweight and isolated from other containers, sharing the underlying host system’s kernel.

Image → Image is the actual package we built using Dockerfile, it’s a read- only format.
Container → Running environment of our image is container.

Docker Flow:

The Docker workflow typically involves the following steps:

  1. Build: Create a Docker image from a Dockerfile.
  2. Push: Upload the image to an intermediate registry or Docker Hub.
  3. Pull: Download the image from the registry.
  4. Run: Deploy the container from the image.
  5. Share (Optional): You can push your image to a registry (like Docker Hub) to share it with others.

Docker Commands:

Using Docker commands, you can create, run, stop, remove, and manage Docker containers easily. These commands can help you automate and streamline the process of deploying and managing your applications in a containerized environment.

Let’s explore some of the most commonly used Docker commands that can help you manage Docker containers effectively.

  1. docker run – Used to start a new Docker container from an image.
  2. docker ps – Used to list all the running Docker containers.
  3. docker stop – Used to stop a running container.
  4. docker rm – Used to remove a Docker container.
  5. docker images – Used to list all the Docker images that are currently available on your system.
  6. docker pull – Used to download a Docker image from a registry.
  7. docker exec – Used to execute a command in a running container.
  8. docker-compose – Used to manage multi-container Docker applications.

More Commands Click here

Installing Docker

The process of installing Docker may vary slightly depending on your operating system. Below are the general steps for installing Docker on some popular platforms:

1. Install Docker on Windows:

1. Download the Docker Desktop for Windows installer from the Docker website 🐬 and run it.

2. Follow the on-screen instructions to complete the installation. Docker Desktop will install the necessary components, including Docker Engine, on your system.

3. Once the installation is complete, Docker Desktop will be available in your system tray, and Docker commands can be run from the command prompt (or) PowerShell.

2. Install Docker on Linux (Ubuntu/Debian):

Open a terminal and run the following commands one by one:

# Update the package index

sudo apt update

# Install required dependencies

sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

# Add Docker GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg — dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add Docker repository

echo “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list

# Update the package index again

sudo apt update

# Install Docker

sudo apt install -y docker-ce docker-ce-cli containerd.io

# Start Docker service

sudo systemctl start docker

# Add your user to the ‘docker’ group to run Docker commands without sudo

sudo usermod -aG docker $USER

3. Install Docker on macOS:

1. Download the Docker Desktop for Mac installer from the Docker website 🐬 and double-click it to start the installation.

2. Drag and drop the Docker.app to the Applications folder to complete the installation.

3. Launch Docker Desktop from the Applications folder, and it will run in the background.

4. Verify Docker Installation:

After installing Docker, you can verify the installation by running the following command in the terminal or command prompt:

If Docker is properly installed, it will display the version number.

Running Your First Docker Container

Pull Nginx Image Open a terminal (or) command prompt and pull the official Nginx image from Docker Hub using the following command:

docker pull nginx

Run Nginx Container Now, run the Nginx container using the docker run command:

docker run -d -p 80:80 — — name my_web_app nginx.

-d: Detached mode. The container will run in the background.

· -p 80:80: Publishes port 80 from the container to port 80 on the host machine. This allows you to access the web server on your browser.

· — — name my_web_app: Assigns a custom name (“my_web_app”) to the container for easier identification.

· nginx: The name of the image to run (in this case, the official Nginx image).

localhost:80

You should see the default Nginx landing page, indicating that the web server is up and running successfully.

List local images

docker images

To list currently running containers:

docker ps

If you want to stop the container, you can use the following command:

docker stop my_web_app

And if you want to remove the container, you can use:

docker rm my_web_app

Successfully created a simple web application using Docker and Nginx after removed the image also.

Conclusion

Containerization with Docker offers a powerful and efficient way to package, deploy, and manage applications. By leveraging Docker’s features and tools, developers can streamline the development process, ensure application consistency, and improve scalability and portability.

Thank you 🙏 for taking the time to read our blog.

--

--

Always learning

கற்றுக் கொள்ளும் மாணவன்...