Docker basics in 5 mins

Docker basics in 5 mins

What is Docker?

"With Docker, developers can build any app in any language using any toolchain. “Dockerized” apps are completely portable and can run anywhere.

Docker manages and tracks changes and dependencies, making it easier for sysadmins to understand how the apps developers build work. And with Docker Hub, developers can automate their build pipeline and share artifacts with collaborators through public or private repositories.

Docker Images

are Like the blueprint for a container.

They contain all the following things (source code, frame, etc) stored inside them but they don’t have them running inside them they just store them.

They are the runtime environment like the node version etc...

Images have their filesystem too which is independent of your computer

These are read-only. It contains every single piece of information that our application needs to run.

Create Image: `docker build -t(image name ) my app .(path of Dockerfile)`

Docker Container

A runnable instance of an image.

We create an image that is the blueprint for a container. Then when we run the images it creates a container which is a process That can run our application exactly as outlined in the image.

So it's going to have the correct runtime env, and access to the image file system dependencies,

The container is a process that runs our application as outlined by the image that we created

Containers are also known as an isolated process meaning they run independently from any other process on your computer

The key difference between a Docker image Vs a container is that a Docker image is a read-only immutable template that defines how a container will be realized. A Docker container is a runtime instance of a Docker image that gets created when the $docker run command is implemented.

Docker file

A Dockerfile is the custom image that you want to create for your application. The Dockerfile name should by convention `Dockerfile`

Let's start

Clone this repo

It's a simple react app that increases the count of button clicks and stores in local storage to maintain the state.

If you don't know about Docker, you can read this and then now Dockerfile explanation


Dockerfile explanation

Let's break the code into components


FROM  node:20-alpine3.17

WORKDIR  /app

COPY  ./package.json  .

RUN  npm  install

COPY  .  .

EXPOSE  3000

CMD  [  "npm","start"  ]

Every new line or new line command in the Dockerfile is a new layer on top of the previous one.

Docker creates container images using layers. Each command that is found in a Dockerfile creates a new layer.

Each layer contains the filesystem changes to the image for the state before the execution of the command and the state after the execution of the command.

FROM  node:20-alpine3.17

This creates a base layer for our application. Since we are using react, we require node as the base. You can choose any base image, e.g., Ubuntu, but in Ubuntu images we have to manually download node, so instead we can use the node image itself.

WORKDIR  /app

WORKDIR changes the working directory for future commands. The WORKDIR specifies the default directory inside a Docker container where commands will be executed. It is similar to the concept of the current working directory on your computer when you run commands in the terminal or command prompt.

COPY  ./package.json  .

This line copies the package.json file from the current directory on the host machine (where the Dockerfile is located) into the /app directory inside the container. It's the first step in installing the dependencies required for the Node.js application.

RUN  npm  install

This line runs the npm install command inside the container. It installs all the dependencies listed in the package.json file into the /app/node_modules directory inside the container. These dependencies are required for the Node.js application to work correctly.

COPY  .  .

This line copies all the files and folders from the current directory on the host machine into the /app directory inside the container. This includes all the source code and other files needed for the Node.js application.

EXPOSE  3000

This line exposes port 3000 inside the container. It means that the container will listen for incoming connections on port 3000, which is a common port used for web applications.

CMD  [  "npm","start"  ]

This line sets the default command to be executed when the container starts running. It runs the npm start command, which typically starts the Node.js application defined in the package.json file. So, when the container starts, it will run the react application and listen on port 3000 as specified earlier with EXPOSE.


Now, as we got the understanding of Dockerfile Now we will explore some commands to create a Docker image and a Docker container.

Create image from DockerFile

docker build -t react-counter .
  • docker build: This tells Docker to start building a new image.

  • -t react-counter: This specifies the name of the image we want to create. In this case, the image will be named "react-counter."

  • .: This indicates the current directory, where the Dockerfile (the configuration file that defines the image) is located.

    • Docker will look for the Dockerfile in the current directory and use it as a blueprint to create the image.

Create Container of Docker Image

docker run -name counter1 -d -p 4000:3000 react-counter
  • docker run: This tells Docker to run a new container.

  • -name counter1: This sets the name of the container as "counter1".

    • The name is like a nickname for the container so that you can refer to it easily later.
  • -d: This flag runs the container in the background (detached mode) so that you can continue using your terminal.

  • -p 4000:3000: This maps port 4000 on your host machine to port 3000 inside the container.

    • It means you can access the application running inside the container on your host machine using port 4000.
  • docker-img-id or docker-img-name: This is the unique identifier (ID) of the Docker image that you want to run in the container.

    • Docker uses this ID to find the image and create a container from it.

Open you browser

Enter the URI

http://localhost:4000/

ENJOY

Did you find this article valuable?

Support scorcism's Blog by becoming a sponsor. Any amount is appreciated!