What is Docker_
What is Docker_
What is Containers?
Containers are lightweight, isolated
environments that encapsulate all the
necessary dependencies, libraries, and
configurations required to run an
application.
Feature of Docker :
• Open-source platform
• Fast and efficient development life cycle
• Scalability
• Security
• Networking
• Image management
Architecture of Docker :
Docker Client :
The Docker client allows users to interact
with Docker. It sends commands to the Docker
daemon via the Docker API. Common commands
include ` docker build `, ` docker pull `,
and ` docker run `.
Docker Host :
A Docker host runs containers and includes
the Docker daemon, images, containers,
networks, and storage.
Docker Registry :
Docker images are stored in a registry, with
Docker Hub as the public option. Images can be
pulled from or pushed to a registry using
`docker pull ` and ` docker push `.
Docker Objects:
• Images: Read-only templates with
instructions for creating containers.
• Containers: Run from images, containers are
isolated environments where applications
execute.
• Storage: Manages data within containers
using storage drivers.
Docker Networking :
Provides isolation and enables containers to
connect to multiple networks.
These commands are a great starting point for working with Docker.
For more advanced options, refer to the Docker documentation.
What is Docker Compose?
Docker Compose is a tool that simplifies
managing multi-container applications. It allows
you to define services, networks, and volumes in
a ` docker-compose.yml ` file, making it easy to
run and manage your entire application as a
single unit.
Key Points:
1. Defining Services: Specify the components of your
application (e.g., web server, database) in a YAML file.
Sample Dockerfile :
Here’s a sample Dockerfile that demonstrates
the basic structure and commonly used
commands:
7. # Install dependencies
8. RUN npm install
• ` WORKDIR /src `: Sets the working directory to ` /src ` within the container.
• ` COPY . . `: Copies all the files from the current directory on the host machine to
the ` /src ` directory inside the container.
• ` RUN npm run build `: Executes the build command to compile or bundle the
application.
• ` EXPOSE 3000 `: Exposes port 3000 so that the application can be accessed from
outside the container.
• ` CMD ["npm", "run", "start"] `: Specifies the command to start the application
when the container is run.
2. 2 services:
3. 3 app:
4. image: node:20-alpine # Use the official Node.js image
5. working_dir: /src # Set the working directory inside the container
6. volumes:
- .:/src # Mount the current directory to /src inside the
container
7. ports:
- "3000:3000" # Map port 3000 on the host to port 3000 in the
container
8. command: npm run start # Command to start the Node.js application
9. depends_on:
- db # Ensure the app service starts after the db service
10.db:
11.image: postgres:15-alpine # Use the official PostgreSQL image
12.environment:
13.POSTGRES_USER: example_user # Set the database username
14.POSTGRES_PASSWORD: example_pass # Set the database password
15.POSTGRES_DB: example_db # Set the database name
16.volumes:
- db_data:/var/lib/postgresql/data # Persist database data
17.volumes:
18.db_data: # Define a named volume for database persistence
In this example:
• The ` app ` service uses the ` node:20-alpine ` image from Docker Hub.
o Port mapping is defined to map port 3000 on the host to port 3000 in the
container. This allows access to the Node.js application running inside the
container from the host machine.