-
Notifications
You must be signed in to change notification settings - Fork 0
EN_Virt_Docker
Docker is an open-source platform for deploying applications within software containers. It provides additional abstraction and automation for operating system-level virtualization on Linux.
Docker utilizes Linux kernel's resource isolation features (like cgroups and kernel namespaces) and union file systems (such as OverlayFS) to allow independent containers to run within a single Linux instance, thus avoiding the overhead of starting and maintaining virtual machines.
- Containerization: Packages applications and their dependencies into containers, ensuring consistency across development, test, and production environments.
- Lightweight: Containers share the machine's OS kernel and do not require an OS for each application, making them lighter than virtual machines.
- Portability: Dockerized applications can run anywhere on any platform that supports Docker.
- Isolation: Ensures each container is isolated, having its own file system, networking, and process space.
- Docker Hub: A repository for Docker images where users can pull images and use them as the basis for their applications.
A Dockerfile is a text document containing all commands a user can call from the command line to assemble an image. Using docker build, users can create automated builds that execute a series of command line instructions.
- FROM: Specifies the base image.
- RUN: Executes commands in a new layer above the current image and commits the results.
-
COPY: Copies new files, directories, or remote file URLs from
srcto the container'sdestpath. -
CMD: Provides default values for running containers. Only the last
CMDtakes effect. - EXPOSE: Informs Docker that the container listens on specified network ports at runtime.
-
ENV: Sets an environment variable
<key>to the value<value>. - ENTRYPOINT: Configures a container to run as an executable.
# Use the official Python runtime as a parent image
FROM python:3.7-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "./app.py"]- This Dockerfile creates a Python environment, sets the working directory, copies the current directory's contents, installs dependencies, exposes a port, sets an environment variable, and specifies the command to run on container startup.
Docker Compose is a tool for defining and running multi-container Docker applications. You can configure your application's services, networks, and volumes using the docker-compose.yml file.
version: "3"
services:
web:
image: "webapp:latest"
ports:
- "5000:5000"
db:
image: "postgres:latest"
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password- The corresponding
doker-compose.ymlfile defines web application services and postgreSQL database services. The web service runs on 5000 ports, and the db service is configured through environmental variables.