DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Docker WorkFlow 2024

Docker's architectural flow involves several key components that interact to create, build, and run containers. Here’s the step-by-step architectural flow, starting from the creation of a Dockerfile and ending with the creation of a container.
.
.
.

1. Creation of Dockerfile

A Dockerfile is a text file that contains a series of instructions to create a Docker image. These instructions typically include:

  • The base image (FROM), which serves as the foundation for your image (e.g., Ubuntu, Node.js).
  • Commands (RUN, COPY, etc.) that set up the environment, install dependencies, copy files, etc.
  • The entry point (CMD or ENTRYPOINT) to specify the command the container will run.

Example of a Dockerfile:

   FROM node:14
   WORKDIR /app
   COPY package*.json ./
   RUN npm install
   COPY . .
   EXPOSE 3000
   CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

.
.
.

2. Docker Build Command

The next step is to build the Docker image from the Dockerfile. The docker build command reads the instructions from the Dockerfile and constructs an image layer by layer.

   docker build -t <image_name> .
Enter fullscreen mode Exit fullscreen mode
  • Layers: Each instruction in the Dockerfile results in a new layer being added to the image. These layers are cached to optimize build time. For example, the RUN npm install step will be skipped in subsequent builds if the package.json file hasn’t changed.
  • Image Creation: Docker compiles all the layers into a final image that is stored in the Docker engine (locally).

.
.
.

3. Creation of Docker Image

Once the docker build command is executed, Docker creates a binary artifact known as an image. The image is a read-only template that contains:

  • The operating system (from the base image).
  • Application code and dependencies (installed via instructions like COPY and RUN).
  • The entry point or command that will be executed when the container runs.

The image can be stored locally, shared via Docker Hub, or hosted on private registries.

.
.
.

4. Docker Run Command

To create and start a container, you use the docker run command. This command:

  • Pulls the image: If the image isn’t available locally, Docker pulls it from a registry (Docker Hub or a private registry).
  • Creates a container: A container is an instance of an image. It is created by adding a writable layer on top of the image’s read-only layers.
  • Starts the container: The container runs the application specified by the CMD or ENTRYPOINT instruction in the Dockerfile.
   docker run -d -p 3000:3000 <image_name>
Enter fullscreen mode Exit fullscreen mode
  • -d: Runs the container in detached mode.
  • -p: Maps the container’s internal port (3000 in this case) to the host machine.

.
.
.

5. Creation of Container

The container is the runtime instance of a Docker image, and it is isolated from the host system:

  • Isolation: Each container has its own environment, including file system, network interface, and process space.
  • Volume and networking: You can attach volumes to persist data or configure networks to allow communication between containers.
  • Execution environment: The container runs the command defined in the Dockerfile (CMD or ENTRYPOINT) within the isolated environment.

.
.
.
.

Docker Architectural Flow Summary:

  1. Create Dockerfile: Define your application environment and dependencies.
  2. docker build: Build the Docker image from the Dockerfile, layer by layer.
  3. Create Docker Image: A lightweight, portable package that contains everything needed to run the application.
  4. docker run: Create and start a container from the image, mapping ports and attaching volumes if necessary.
  5. Container: A running instance of the image, isolated from the host and other containers, executing the command specified in the image.

This architectural flow allows developers and DevOps engineers to encapsulate applications, making them portable across environments (dev, test, production) and enabling rapid scaling and deployment.

Top comments (0)