Docker Overview

Docker Overview

Table of contents

No heading

No headings in the article.

Introduction:

Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications using containerization. Docker containers are lightweight, isolated, and portable environments that encapsulate an application along with its dependencies and configuration. Unlike traditional virtual machines, Docker containers share the host operating system's kernel, which makes them highly efficient and resource-friendly. Containers offer consistency across different environments, ensuring that applications run reliably regardless of the underlying infrastructure.

Docker Installation:

To begin using Docker, you need to install the Docker Engine on your system. Docker provides installation packages for various operating systems, including Windows, macOS, and Linux. You can also visit the docker website (https://docs.docker.com/engine/install/) and follow the installation instructions for your specific platform or OS like Linux, Windows, etc.

To install docker in Ubuntu we can run the following command.

#Update the ubuntu first
sudo apt-get update
#Docker install
sudo apt install docker.io

Docker's Basic Command:

docker run <image_name>: Pulls the specified Docker image and runs a container based on that image.

docker ps: Lists the running containers.

docker ps -a: Lists all the containers whether are in running state or not.

docker images: Displays the available Docker images on your system.

docker pull <image_name>: Downloads a Docker image from a registry without running a container.

docker stop <container_id>: Stops a running container safely within the grace time.

docker kill <container_id>: This command terminates the container immediately.

docker rm <container_id>: Removes a stopped container.

docker rmi <image_name>: Deletes a Docker image from your local repository. docker exec -it <container_id>: Executes a command within a running container.

Creating and Managing Containers:

To create a custom Docker container, we will typically start with a base image and add our application code and dependencies which we called Dockerfile or we can define it like Dockerfile is a text file that contains the series of commands.

FROM python:3
RUN pip install django==3.2
COPY . .
RUN manage.py migrate
CMD ["python","manage.py","0.0.0.0:8002"]

FROM python:3 => indicates that the base image is Python 3

RUN pip install django==3.2 => The RUN instruction in a Dockerfile is used to execute commands during the build process of a Docker image. In the context of RUN pip install django==3.2, it means that during the image build, it will execute the command pip install django==3.2 inside the container.

COPY . . => It means that you are copying the contents of the current directory (referred to as .) on the host machine into the current working directory of the Docker image.

RUN manage.py migrate => It means that during the image build, it will execute the command manage.py migrate inside the container.

CMD ["python","manage.py","0.0.0.0:8002"] => In a Dockerfile, the CMD instruction is used to specify the default command or parameters to be executed when a container is started from the Docker image.

Build the Docker Image:

Save the Dockerfile in a directory and run the following command.

docker build . -t todoapp:latest

You can check the container by below command

docker ps
docker ps -a