What is Docker? Use of docker in Development

Ashvin Vanol
Yudiz Solutions
Published in
7 min readJun 14, 2024

What is Docker?

  • Docker is an open-source platform that allows developers to package, deploy, and run applications in containers easily. Containers are lightweight, standalone executable packages that include everything needed to run an application, including code, libraries, and system tools.
  • The main benefits of using Docker include:
  • Portability: Docker containers can run consistently across different environments (development, testing, production) without compatibility issues.
  • Efficiency: Containers are lightweight and share the host machine’s operating system kernel, making them faster and less resource-intensive than traditional virtual machines.
  • Scalability: Docker enables easy scaling of applications by running multiple containers across different machines, managed by a single control plane.
  • Docker simplifies the software development lifecycle by providing a standardized way to build, ship, and run applications. It ensures consistency across different computing environments, eliminates the “it works on my machine” problem, and enables developers to focus on writing code rather than worrying about infrastructure setup and compatibility issues.
  • It allows running an Application in an isolated environment called Container.
  • With Docker, developers can ensure consistency across different environments, eliminate compatibility issues, and focus on writing code rather than worrying about infrastructure setup.

Install Docker in Mac

brew install - cask docker
  • Run the following command to verify that Docker is installed correctly
docker - version

What is a container?

  • A container is a lightweight, standalone executable package that includes an application and all its dependencies, libraries, and system tools. Regardless of the underlying infrastructure, it provides a consistent and isolated environment for the application to run.
  • Makes deployment and development Efficient.
  • Containers simplify building, deploying, and scaling applications, making the development lifecycle more efficient and agile.

Key points:

  • Lightweight: Containers share the host machine’s OS kernel, making them more resource-efficient than virtual machines.
  • Portable: Containers can run consistently across different environments (dev, test, prod) and platforms.
  • Isolated: Each container runs in its isolated environment, ensuring no interference between containers or with the host system.
  • Here you can see this one application is running in node version 14 and one is running in 16

Both applications run in an isolated container

  • So you can see that with the help of containers we can run applications in different versions in the same system.

Docker Images

  • It is a kind of ready-to-use software read-only template.
  • A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and config files.
  • It serves as a template for creating Docker containers, which are running instances of the image.
  • Docker images are built from a series of layers, each representing an instruction in the image’s Dockerfile.
  • Images are stored in a Docker registry, such as Docker Hub, and can be shared and distributed easily.

What is DockerFile?

  • A Dockerfile is a text file that contains a set of instructions used to build a Docker image. You can create a file like dockerFile in your project.
  • It is a script that automates the process of creating a Docker image by defining the environment and dependencies required for an application to run.
  • It is a simple text File with instructions to build an Image.
  • Basic instructions:
  • ​FROM: Specifies the base image to start building from.
  • COPY: Copies files or directories from the host machine to the image.
  • RUN: Executes commands inside the image during the build process.
  • ENV: Sets environment variables within the image.
  • EXPOSE: Specifies the ports on which the container will listen.
  • CMD: Defines the default command to run when a container starts.

Step to Create an image using DockerFile.

Step: 1 Create DockerFile in your project directory

Step: 2 Add details.

# Use an official Node.js runtime as the base image
FROM node:20
# Set the working directory in the container
WORKDIR /app
# Install the application dependencies
RUN npm install
# Copy the application code to the working directory
COPY . .
# Expose the port on which the application will run
EXPOSE 3000
# Define the command to run the application
CMD ["node", "index.js"]

Step: 3 Build docker Image using this command

docker build -t first-app

Step:4 List created image

docker image ls

Create a container using Image.

  • ​​To create a container using a Docker image, you can use the docker run command. Here’s the basic syntax:
docker run -it - name <container_name> -p <local>:<image> <image_name>
docker run -it - name test_app -p 3031:3031 test-app:v1
  • Let’s break down the command:
  • docker run: The command to create and start a new container.
  • -it : Two combined options:
  • -i: Keeps STDIN open even if not attached, allowing you to interact with the container through the terminal.
  • -t: Allocates a pseudo-TTY (terminal) for the container, enabling an interactive shell.
  • — name test_app: Assigns a name to the container, in this case, “test_app”. This name can be used to reference the container later for various operations.
  • -p 3031:3031: Maps port 3031 from the container to port 3031 on the host machine. This allows accessing the application running inside the container on the specified port. The format is -p <host-port>:<container-port>.
  • test-app:v1: Specifies the Docker image and tag to use for creating the container. In this case, it uses an image named “test-app” with the tag “v1”. The tag typically represents a specific version or variant of the image.

List Container

docker container ls
  • This command displays a list of all the containers that are currently running on your system.
docker container ls -a
  • This command lists all containers, regardless of their state (running or stopped). It provides the same details as mentioned above for each container.

Note: docker container ls is equivalent to the older docker ps command, which is still widely used. Both commands provide the same functionality.

start and stop the container

docker start container_name
docker stop container_name

Remove container

docker rm container_name
docker ps -a

What is DockerHub? How to Push Images in DockerHub.

  • Docker Hub is a cloud-based registry service provided by Docker, Inc. It serves as a central repository for storing, sharing, and distributing Docker container images.
  • To push your image into the docker hub you want to create an account in the docker hub. You can create your account using this link: https://hub.docker.com/
  • Central repository for storing and distributing Docker container images
  • Hosts official images maintained by Docker and community-contributed images
  • Allows developers to easily share and distribute their own container images
  • Provides features like user accounts, organizations, and automated builds
  • Promotes collaboration, reusability, and efficiency in the Docker ecosystem

Step: 1 Create a repository

Step: 2 Login Docker into your terminal

#cmd: 
docker login

Step: 3 Create an image with the same name as the repository name

docker build -t ashvin44/test_app .

Or rename your current Image

docker tag <current_image_name> <new_inmage_name>

Step: 4 Push the image into the docker hub.

docker push abcd/test_app

Step: 5 Pull the image into your local system.

docker run -p 3000:3000 <image_name>
Docker run -p 3000:3000 test_app

What Docker Compose is and why we need it:

  • Docker Compose is a tool for defining and running multi-container Docker applications
  • It allows you to configure application services, networks, and volumes in a single YAML file
  • Compose simplifies the process of creating, starting, and managing multiple containers as a single application stack
  • It enables easy scaling, environment configuration, and dependency management for multi-container applications
  • Compose promotes development productivity, scalability, and portability of Docker-based applications

Create a compose file for your project

Step: 1

Create a docker-compose.yml file in your working directory.

Step: 2

Add necessary dependencies.

Example :

version: ‘3’
services:
web:
build: .
container_name: basic-container
command: npm start
volumes:
- .:/app
- /app/node_modules
ports:
- "3000:3000"
environment:
- NODE_ENV=development
depends_on:
- db
db:
image: mongo:latest
volumes:
- mongodb_data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=secret
volumes:
Mongodb_data:

Step:3 create image and container using the docker-compose file

docker-compose up

  • You can see the container created in the docker desktop.
  • You can pull the image from the docker hub and share this image with anyone

In conclusion, Docker revolutionizes software development by providing a standardized platform for packaging, deploying, and running applications in lightweight, portable containers.

The key advantages of Docker include portability, efficiency, and scalability. Docker containers can run consistently across various environments, from development to production, without any compatibility issues. They are lightweight, share the host machine’s OS kernel, and consume fewer resources compared to traditional virtual machines. Moreover, Docker enables easy scaling of applications by running multiple containers across different machines, all managed by a single control plane.

Furthermore, Docker Hub acts as a central repository for storing, sharing, and distributing Docker container images. Developers can push their images to Docker Hub, making them easily accessible to others. Docker Compose simplifies the management of multi-container applications by allowing developers to define services, networks, and volumes in a single YAML file.

In essence, Docker empowers developers to build, ship, and run applications efficiently, promoting collaboration, reusability, and agility in software development.

Thank you for reading!

--

--