What is Docker? And Why? Why Containers? docker setup.
What is Docker?
Docker is an open-source platform that automates the deployment, scaling, and management of applications. It uses containerization technology to package applications and their dependencies into containers, which can run on any system with a compatible operating system.
Key Components:
Why Docker?
Docker provides several benefits that make it popular among developers and IT operations teams:
Why Containers?
Containers provide a method for packaging an application along with its dependencies, ensuring that it runs consistently across various environments. This approach addresses several challenges in modern software development:
Docker Setup
To set up Docker on your system, follow these steps:
Step 1: Install Docker
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install -y docker-ce
Step 2: Verify Installation
Run the following command to verify that Docker is installed correctly:
领英推荐
docker --version
Step 3: Run a Test Container
Run a simple test to ensure Docker is working:
docker run hello-world
Step 4: Manage Docker as a Non-Root User (Linux)
Add your user to the Docker group to avoid using sudo for Docker commands:
sudo usermod -aG docker $USER
Log out and log back in to apply the changes.
Example Usage
Building a Docker Image
Create a Dockerfile in your project directory:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory
WORKDIR /usr/src/app
# Copy the current directory contents into the container
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"]
Build the Docker image:
docker build -t my-python-app .
Running a Container
Run the image as a container:
docker run -p 4000:80 my-python-app
This command maps port 80 inside the container to port 4000 on your host machine.
By understanding Docker and its benefits, as well as setting it up and using it effectively, you can streamline your development workflow, ensure consistency across different environments, and enhance the scalability of your applications.