Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

WHAT IS DOCKER?

Docker is a tool that allows you to package and run your applications in a consistent and
isolated environment called containers. Think of containers as lightweight, portable units that
contain everything your application needs to run, including code, runtime, libraries, and
dependencies.

Here’s a simplified explanation:

Imagine you have a recipe for a cake. To bake the cake, you need specific ingredients like
flour, eggs, sugar, etc. Docker is like a container that holds all these ingredients together.
With Docker, you can package your entire application along with its dependencies into this
container. This makes it easy to share and run your application on any system that has Docker
installed, without worrying about compatibility issues or differences in the underlying
environment.

In interviews, you can explain Docker as a tool that simplifies the process of building,
shipping, and running applications by providing a consistent environment across
different systems. It’s like a virtual environment for your applications that ensures they
run the same way everywhere.

WHAT IS DOCKER CONTAINER?


A Docker container is like a small, self-contained package that holds everything your
application needs to run. It’s kind of like a lunchbox for your app!
Inside the container, you put your app’s code, plus all the other stuff it needs to work, like
libraries and settings. This way, your app can run smoothly no matter where it goes.
When you want to run your app, you just open the container, and everything inside is ready to
go. It’s like opening your lunchbox and finding your sandwich, chips, and drink all ready to
eat!
In interviews, you can explain Docker containers as lunchboxes for apps, making them easy
to move, share, and run anywhere.

WHAT IS DOCKER IMAGE?


A Docker image is like a recipe or blueprint for creating a Docker container.

Imagine you’re baking cookies. Before you start baking, you follow a recipe to make cookie
dough. The recipe tells you exactly what ingredients to use and how to mix them together.

Similarly, a Docker image is like the recipe for your container. It contains all the instructions
and ingredients (like your app’s code, libraries, and settings) needed to create a Docker
container.

Once you have the image (recipe), you can use it to create multiple containers (batches of
cookies) that are exactly the same. This makes it easy to share and run your applications
consistently across different environments.
So, in simple terms, a Docker image is like a recipe for creating Docker containers. It’s the
starting point for building and running your applications in Docker.

WHAT IS DOCKER FILE?


Sure, here's a simple explanation of a Dockerfile with an example:

A Dockerfile is a text file that contains instructions for building a Docker image. It's like a
recipe that Docker follows to create the image.

Let's say you want to create a Docker image for a basic Python web application. Here's what
your Dockerfile might look like:

Let's break down each part:

- `FROM python:3.9`: This line tells Docker to use the official Python 3.9 image from
Docker Hub as the base image for our image.

- `WORKDIR /app`: This line sets the working directory inside the container to `/app`.

- `COPY requirements.txt .`: This line copies the `requirements.txt` file from your local
machine into the `/app` directory in the container.

- `RUN pip install --no-cache-dir -r requirements.txt`: This line installs the Python
dependencies listed in the `requirements.txt` file using pip.
- `COPY . .`: This line copies the rest of your application code (including `app.py` and any
other files) from your local machine into the `/app` directory in the container.

- `CMD ["python", "app.py"]`: This line specifies the command that Docker should run when
the container starts. In this case, it runs the `app.py` Python script.

With this Dockerfile, you can use the `docker build` command to build your Docker image:

```
docker build -t my-python-app .
```

And then use the `docker run` command to run your container:

```
docker run my-python-app
```

This is a basic example, but Dockerfile can be much more complex depending on the needs
of your application.

Dockerfile for REACT JS basic project:

FROM node:20.11.0 as build


WORKDIR /my-app/src
COPY package.json /my-app/src/
COPY package-lock.json /my-app/src/
RUN npm install
COPY . /my-app/src/
RUN npm run build
CMD ["npm", "start"]
EXPOSE 8080
Let's break down each part:
- `FROM node:20-alpine`: This line tells Docker to use the official Node.js 20 image from
Docker Hub as the base image for our image. The `-alpine` tag specifies that we want to use
a lightweight Alpine Linux-based version of the Node.js image.
- `WORKDIR /app`: This line sets the working directory inside the container to `/app`.
- `COPY package*.json ./`: This line copies the `package.json` and `package-lock.json` files
from your local machine into the `/app` directory in the container.
- `RUN npm install`: This line installs the Node.js dependencies listed in the `package.json`
file using npm.
- `COPY . .`: This line copies the rest of your application code (including the React source
files) from your local machine into the `/app` directory in the container.
- `RUN npm run build`: This line builds the React app using the npm script specified in your
`package.json` file.
- `CMD ["npm", "start"]`: This line specifies the command that Docker should run when the
container starts. In this case, it runs the `start` script defined in your `package.json` file,
which typically starts the React development server.

With this Dockerfile, you can use the `docker build` command to build your Docker image:
```
docker build -t my-react-app .
```
And then use the `docker run` command to run your container:
```
docker run -p 3000:3000 my-react-app
```
This will start your React application inside a Docker container, and you can access it in your
web browser at `http://localhost:3000`.

DOCKER RUN vs DOCKER START vs DOCKER EXEC command


1. Docker run:
- `docker run` is used to create and start a new Docker container from a Docker image.
- It is the primary command used to launch Docker containers.
- When you run `docker run`, Docker pulls the specified image from a registry (if it's not
already available locally), creates a new container based on that image, and starts it.
- You can also use `docker run` to specify options such as port mappings, environment
variables, volume mounts, and more.

2. Docker start:
- `docker start` is used to start one or more existing Docker containers that are currently
stopped.
- It is used to resume containers that have been previously created and stopped.
- When you run `docker start`, Docker starts the container without any additional
configuration options. It simply resumes the container from its previous state.

3. Docker exec:
- `docker exec` is used to run a command inside an already running Docker container.
- It allows you to execute commands inside a container's running process space, similar to
how you would run commands on a regular Linux shell.
- You specify the container name or ID and the command you want to run inside the
container. For example, `docker exec <container_name> <command>`.

In summary:
- `docker run` is used to create and start new containers.
- `docker start` is used to start existing containers that are currently stopped.
- `docker exec` is used to execute commands inside running containers.

LIST OF COMMONLY USED DOCKERFILE COMMANDS


1. FROM: Specifies the base image to use for the subsequent build steps.
2. LABEL: Adds metadata to the image, such as author, version, and description.
3. RUN: Executes commands inside the container during the image build process.
4. CMD: Specifies the default command to run when the container starts (can be overridden
at runtime).
5. EXPOSE: Declares the ports on which the container will listen for connections.
6. ENV: Sets environment variables inside the container.
7. ADD: Copies files from the build context (local filesystem) into the image.
8. COPY: Copies files from the build context (local filesystem) into the image (preferred over
ADD for copying local files).
9. ENTRYPOINT: Specifies the executable to run when the container starts (overrides CMD).
10. VOLUME: Creates a mount point and marks it as externally mounted for persistent
storage.
11. USER: Sets the user or UID to run the container's process.
12. WORKDIR: Sets the working directory for subsequent commands in the Dockerfile.
13. ARG: Defines build-time variables that can be passed to the Dockerfile using the --build-
arg flag.
14. STOPSIGNAL: Sets the signal that will be sent to the container to stop it.
15. HEALTHCHECK: Specifies a command to run to check the container's health status.
16. SHELL: Specifies the default shell used by the RUN, CMD, and ENTRYPOINT instructions.

**Alpine in Docker:**
Imagine you're building a house (or in our case, a software application) using Lego blocks.
Alpine Linux is like a specific type of Lego block that's really small, lightweight, and super
safe.

- **Small and Light:** Alpine is like a tiny Lego block compared to other big blocks. It doesn't
have extra stuff you don't need, so it's quicker to work with and doesn't take up much space.
- **Super Safe:** It's designed with a lot of special safety features to make sure your house
(or application) is secure. Think of it like having extra strong walls and locks on your door.
- **Base for Docker:** In our Lego house-building project, we often start with these special
Alpine blocks as the foundation. They help keep our house small, quick to build, and extra
secure.
- **Easy to Work With:** Even though Alpine blocks are small, they're still really useful. You
can build anything you want on top of them, just like any other Lego block.

So, in simple terms, Alpine in Docker is like using special Lego blocks to build your
application. They're small, safe, and make building things quick and easy!
COMMONLY USED COMMANDS IN DOCKER
 -v/--version: - To get the version of Docker, we can use below command
 docker -v OR docker –version

 pull: - To pull the image/repository from local or registry, we use


 docker pull [imageName]
 docker pull python
 docker pull python:latest
 docker pull python:3.12

 images: - Get list of images in your docker


 docker images

 search: - Search docker hub for image


 docker search python

 run: - Create and run a new container from an image


 docker run –name container_name image_name
 docker run image_name

 exec: - Execute a command in a running container


 docker exec

 build: - Build an image from a Dockerfile


 docker build -t image_name

 ps: - To check the list of running container


 docker ps

 ps -a: - To check the list of all the containers


 docker ps -a

 -d: - To run the docker in the background (detached mode)


 docker run -d image_name
 docker run -d –name container_name image_name

 -p: - To map a port to running container


 docker run -d -p 5050:3000 image_name

 push: - Upload an image to a registry


Make sure you have tagged your image in this format:
{{username}}/{{imagename}}:{{version}}
Example: - docker tag basic-app:v1 kingankur/basic-react-crud:v1
 Then we will run this: docker push kingankur/basic-react-crud:v1
 inspect: To get details of an image

You might also like