What is Docker ?Writing First Docker File ,Building Docker Images and Basic Docker Commands

Rahul Desharaj
5 min readJun 19, 2024

--

Docker is an open-source software platform that enables developers to create, deploy, and manage applications in a wide variety of computing environments.

It provides a container-based virtualization system that allows developers to package their applications into isolated containers, which can then be deployed on any operating system or cloud platform.

With Docker, developers can quickly and easily create, test, and deploy applications without having to worry about compatibility issues or hardware requirements.

Containers vs Virtual Machines

Containers and virtual machines (VMs) both provide virtualization, but in different ways. VMs include entire operating systems, making them heavier and slower to start. Containers, on the other hand, share the host system’s OS kernel and isolate only the application and its dependencies. This makes containers much lighter, more efficient in resource usage, and faster to launch than VMs. Containers offer a more streamlined, application-focused approach to virtualization compared to the broader system emulation of VMs.

What is a Dockerfile?

A Dockerfile is a text document that has all the command line instructions needed to assemble an image. With the help of a Dockerfile, users can create an automated build that executes several command-line instructions in succession.

Docker builds images automatically by reading instructions from a Dockerfile, which is a text file that contains all commands needed to build a given image in the correct order. A Dockerfile follows a predefined format and set of instructions.

Docker Images

A Docker image is an executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. It is lightweight and standalone. The image informs how a container should instantiate, determining which software components will run and how.

Docker Containers

Docker Containers are running instances of Docker images. We can think of it as a virtual environment that bundles application code with all the dependencies required to run the application, so you don’t need to rely on what’s installed on the host.

How to create a docker file from scratch

Pre-Requisite: Docker should be installed on your system

Below are the steps to create Dockerfile from scratch

This is the sample Dockerfile .

Here’s what each instruction does:

  • FROM: Specifies the base image. In this case, we're using an almalinux.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies the application code from your host or local machine to the container.
  • RUN: Executes commands in the container (e.g., installing dependencies).
  • EXPOSE: Opens a port for communication.
  • ENV: Sets environment variables.
  • CMD: Defines the command to run when the container starts.

Be sure to customize these instructions based on your project’s requirements.

Here are some basic Docker commands to get you started:

1.docker build: Build a Docker image from a Dockerfile.

docker build -t <image_name>:V1 <path_to_Dockerfile>

2. docker images: List all available images on the system.

docker images

3. docker run: Create and start a container from an image

docker run -d -p <host_port>:<container_port> <image_id>

With these steps, you have successfully created a simple web application using Docker and Nginx , we can see the web page as follows after running the above command.

4. docker ps: List running containers.

docker ps

5. docker ps -a: List all containers, including stopped ones.

docker ps -a

6. docker pull: Download an image from a container registry (e.g., Docker Hub).

docker pull <image_name>

7. docker stop: Stop a running container.

docker stop <container_id/container_name>

8. docker start: Start a stopped container.

docker start <container_id/container_name>

9. docker rm: Remove a stopped container.

docker rm <container_id/container_name>

10. docker rmi: Remove an image.

we can not delete directly the image when it is used by the container because the container is using that image , to stop container use stop command which is number 7

so first stop the container →then → remove container →then remove the image

docker rmi <image_name>
   (or)
docker image rm <image Id>

11. docker exec: Run a command inside a running container.

docker exec -it <container_id/container_name> bash

12. docker inspect: prints container details

docker inspect <container_id>

Hope this article gave you a good start in your Docker Journey. Happy Learnings ahead!

Do follow me for more such content related to DevOps world.

Thank you for visiting my blog.

--

--

Rahul Desharaj

Currently, I am working as a DevOps engineer. I am very passionate about learning new things and writing blogs. www.linkedin.com/in/rahul-desharaju-299b872a7