If you're new to the world of containerization and eager to harness the power of Docker, mastering its command-line interface (CLI) is an essential first step. Docker commands act as the building blocks that empower you to create, manage, and orchestrate containers with ease. While the command-line might seem daunting at first, this beginner's guide is designed to demystify Docker commands and provide you with a solid foundation.
In this comprehensive walkthrough, we'll embark on a journey that starts with understanding the fundamentals of Docker architecture and its core components. From there, we'll dive into the essential Docker commands that every beginner should know, such as pulling images, running containers, and inspecting their statuses. Step by step, you'll gain hands-on experience in working with Docker images, managing containers, persisting data with volumes, and establishing networking between your containerized applications.
Why do we need Docker?
Docker is an open-source platform that revolutionizes the way developers set up, deploy, run, and build applications. While the term "container" might conjure up images of shipping containers, Docker containers are lightweight, isolated environments that allow you to run applications without impacting the host operating system.
The beauty of Docker lies in its ability to streamline the development and deployment process, making it an invaluable tool for developers. Here are a few compelling reasons why you should embrace Docker:
Consistent Environments: Docker ensures consistent and reproducible environments across different systems, eliminating the infamous "it works on my machine" issue. By encapsulating applications and their dependencies within containers, you can seamlessly move your applications from development to testing to production without worrying about compatibility issues.
Efficient Resource Utilization: Unlike traditional virtual machines, Docker containers are lightweight and share the host's operating system kernel, resulting in efficient resource utilization. This allows you to run multiple containers on a single host, maximizing hardware utilization and reducing infrastructure costs.
Rapid Deployment and Scalability: Docker simplifies deployment by packaging applications and their dependencies into portable containers. This enables rapid deployment and scaling of applications across different environments, whether it's a local machine, a cloud platform, or a data center.
How Docker Works Internally?
Docker has three main parts which communicate with each other to run Docker containers on your machine.
Here are these main parts which communicate with each other to make things work properly:
Docker CLI: The Docker CLI is the primary user interface for interacting with Docker. It allows you to issue commands to manage Docker containers, images, networks, and other resources. You use the Docker CLI to build, run, stop, and perform various operations on Docker containers and images.
Docker Registry (or Docker Hub): The Docker Registry is a centralized repository for storing and distributing Docker images. Docker Hub is the public registry maintained by Docker, Inc., but you can also set up private registries. The Docker Registry acts as a library where you can pull (download) and push (upload) Docker images.
Docker Daemon (or Docker Engine): The Docker Daemon, often referred to as the Docker Engine, is the core component responsible for creating and managing Docker containers. It receives commands from the Docker CLI, interacts with the operating system's kernel to create and manage containers, and communicates with the Docker Registry to pull or push images.
These three components work together seamlessly:
You use the Docker CLI to issue commands.
The Docker CLI communicates with the Docker Daemon to execute those commands.
The Docker Daemon interacts with the operating system, creates or manages containers, and retrieves or stores images from/to the Docker Registry.
How to Install Docker in Machine?
To install Docker on your machine, follow these steps:
Download Docker Desktop
- Go to the official Docker website and download Docker Desktop for your operating system (Windows, macOS, or Linux).
Install Docker Desktop
For Windows and macOS, simply run the downloaded installer and follow the on-screen instructions.
For Linux, you may need to first install some prerequisites and set up the Docker repository. Follow the official installation guide for your specific Linux distribution.
Start Docker Desktop
After the installation is complete, launch Docker Desktop from your Applications (macOS) or Start Menu (Windows).
Docker Desktop will start and may prompt you to authorize the installation of network components and additional resources.
Verify Docker Installation
Once Docker Desktop is running, open a terminal or command prompt.
Run the following command to verify that Docker is installed correctly:
docker --version
If Docker is installed, it will display the version of Docker that is currently running on your system.
Run a Test Container (Optional)
To ensure that Docker is working properly, you can run a simple test container. In your terminal or command prompt, run:
docker run hello-world
This command will pull the
hello-world
image from Docker Hub (if it's not already present on your system) and run a lightweight container that prints a "Hello from Docker!" message.
Docker Commands to know:
A Docker container runs the image and image comes from the dockerhub.
So as we understood Docker helps us to run the container and Blah Blah...
So let's try this out by running the following command in the terminal.
docker pull ubuntu
This will pull the ubuntu image from the docker hub and to run the image in container you just have to run the command.
docker run -it --name my-ubuntu ubuntu
-it
: it runs the image in the interactive mode, basically, you will get into the container in the terminal.
--name my-ubuntu
: it attaches the name to the container and here we have attached the name my-ubuntu
.
So if you are done running the containers now let's try to stop the container.
#docker stop {container_name}|{container_id}
docker stop my-ubuntu
you can see the running container either in the docker daemon or in the terminal using the command.
#list all the running container
docker ps
As you know the command to run and stop the container so let's delete the container using the name attached to it or using the container_id.
#delete the container
#docker rm {container_name}|{container_id}
docker rm my-ubuntu
But please first stop the container before deleting the container.
Now that we know the basic commands to work with containers so we will next work on using the container to start the other technologies
For now, we will go with MongoDB database and start it without installing it.
Sure, let's continue with running a MongoDB container without installing MongoDB locally on your machine. We can leverage Docker to run MongoDB directly from its official Docker image.
Here are the steps to start the MongoDB database using a Docker container:
- Pull the MongoDB Docker Image First, we need to pull the official MongoDB Docker image from Docker Hub. Run the following command:
docker pull mongo
This command will download the latest version of the MongoDB Docker image to your local machine.
- Run the MongoDB Container Once the image is downloaded, you can run a container from it using the
docker run
command:
docker run -d --name mongodb-container -p 27017:27017 mongo
Let's break down this command:
-d
: Runs the container in detached mode (in the background).--name mongodb-container
: Assigns a name to the container for easy identification and management.-p 27017:27017
: Maps the container's port 27017 (MongoDB's default port) to the same port on your host machine.mongo
: The name of the Docker image you want to run the container from.
- Verify MongoDB Container To ensure that the MongoDB container is running, you can use the
docker ps
command:
docker ps
You should see the mongodb-container
listed in the output.
- Connect to MongoDB Now that the MongoDB container is running, you can connect to the MongoDB instance using a MongoDB client or driver.
If you want to connect from your host machine, you can use the MongoDB shell or any other MongoDB client tool and specify the host as localhost:27017
.
For example, to connect using the MongoDB shell, run:
mongo --host localhost:27017
This will open the MongoDB shell, and you can start interacting with the database.
Alternatively, if you want to connect from within the same container, you can execute a new shell inside the running container:
docker exec -it mongodb-container mongo
This command will open the MongoDB shell directly inside the container.
With these steps, you have successfully started the MongoDB database using a Docker container without installing MongoDB locally on your machine. You can now interact with the MongoDB instance, create databases, and collections, and perform various operations as needed.But remember this data is not persistent and that's why we will have to store the data in volumes and that we will cover in next segment of blog.
In the next blog, we will dive deeper into advanced Docker concepts such as working with volumes for persistent data storage, building custom Docker images using Dockerfiles, and pushing your Docker images to Docker Hub and many more. Stay tuned as we continue to unlock the full potential of Docker for and enhancing application portability. Happy coding ๐จโ๐ป๐จโ๐ป