Professional Documents
Culture Documents
What Is Docker
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.
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.
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.
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:
- `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.
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`.
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.
**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