Docker Decoded: A Beginner's Guide to Container Magic
What is Docker ?
Docker is an?open-source centralized platform designed?to create, deploy, and run applications.
Docker uses?container?on the host's operating system to run applications.
Docker Architecture
Docker follows Client-Server architecture, which includes the three main components that are?Docker Client,?Docker Host, and?Docker Registry
2. Docker Host
3. Docker Registry
4. Docker objects
What is an Docker Image
What is a Container ?
Where do containers live ?
How containers improved Application development and deployment process
Before Containers
After Containers
Advantages of Docker
Disadvantages of Docker
Basic Docker Commands
Docker Network
Network Drivers
Docker File
How to build your own docker image ?
Step 1: Write Your Application Code
Start by preparing the application code you want to containerize. For example, create a simple web application using a framework like Flask. Place your code in a directory, say myapp.
Step 2: Create a Dockerfile
Craft a Dockerfile, a script that contains instructions for building your Docker image. In your project directory, create a file named Dockerfile without any file extension.
Use an official base image - FROM python:3.8-slim
Set the working directory - WORKDIR /app
Copy the local code to the container - COPY ./myapp /app
Install dependencies - RUN pip install --no-cache-dir -r requirements.txt
Expose the application's port - EXPOSE 5000
Define the command to run your application - CMD ["python", "app.py"]
Step 3: Create a requirements.txt File
If your application has dependencies, create a requirements.txt file listing them. Place this file in the same directory as your Dockerfile.
Flask==2.0.1
Step 4: Build the Docker Image
Open a terminal in the directory containing your Dockerfile. Use the following command to build your Docker image, tagging it with a name and version.
docker build -t mydockerapp:v1
Step 5: Verify the Image
Confirm that your Docker image is created successfully by listing the available images.
docker images
领英推荐
Step 6: Run a Container
Now, run a container using the image you just built.
docker run -p 5000:5000 mydockerapp:v1
Step 7: Access Your Application
Visit https://localhost:5000 in your web browser to see your application running inside a Docker container.
How to push the docker image into a private Repository ?
Step 1: Set Up AWS CLI
Ensure that you have the AWS Command Line Interface (CLI) installed and configured with the necessary credentials. The AWS configure command prompts you to enter your AWS access key, secret key, default region, and output format.
aws configure
Step 2: Create an ECR Repository
Create an Elastic Container Registry (ECR) repository on AWS using either the AWS Management Console or the CLI. Replace <your-repository-name> with your desired repository name.
aws ecr create-repository --repository-name <your-repository-name>
Step 3: Login to ECR Registry
Authenticate Docker to your ECR registry using the aws ecr get-login-password command. This generates a token for Docker to log in to the ECR registry securely.
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com
Step 4: Build Docker Image
Build your Docker image using the docker build command. This command takes the Dockerfile in the current directory (denoted by .) and tags the image with the name mydockerrepo:latest.
docker build -t mydockerrepo:latest .
Step 5: Tag the Image
Tag the Docker image with the ECR repository URI. Replace <your-account-id> and <your-region> with your AWS account ID and desired AWS region.
docker tag mydockerrepo:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/mydockerrepo:latest
Step 6: Push Image to ECR
Push the Docker image to your ECR repository. This command uploads the image to your private AWS ECR registry.
docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/mydockerrepo:latest
Step 7: Verify Image Push
Verify the successful push by checking your ECR repository. This command describes the images in the specified repository.
aws ecr describe-images --repository-name mydockerrepo
Docker volume
A Docker volume is a persistent data storage mechanism used to manage and store data in Docker containers. Volumes allow data to persist beyond the lifecycle of a single container and provide a way to share and manage data between containers and between the host machine and containers.
Key aspects of docker volume
Different types of docker volumes
Named volumes
Creation code - docker volume create mynamedvolume
Usage in container - docker run -v mynamedvolume:/app mycontainer
Host mounted volumes
Usage in container - docker run -v /host/path:/container/path mycontainer
Anonymous Volumes
Usage in container - docker run -v /container/path mycontainer
Docker Security
There are four major areas to consider when reviewing Docker security:
The intrinsic security of the kernel and its support for namespaces and cgroups
The attack surface of the Docker daemon itself
Loopholes in the container configuration profile, either by default, or when customized by users
The "hardening" security features of the kernel and how they interact with containers
Intern @Niveussolutions | Mantrika.ai | Artifical intelligence and Machine learning | DevOps
1 年Amazing ??