Monday 12 February 2024

8 Free Udemy Docker Courses to Take Your Skills From Beginner to Pro

Docker has become an essential tool for developers and DevOps professionals, and mastering it can open up a wide range of career opportunities. But where do you start? Don't worry, we've got you covered! Here are eight free Udemy Docker courses that will take your skills from beginner to professional.


Beginers:

1. Docker for the Absolute Beginner

https://lnkd.in/eSDNg-Xv

Read more »

Labels:

Friday 25 February 2022

Connecting from Inside a Docker Container to the Localhost of the Host Machine

Docker has revolutionized the way we build, ship, and run applications. It allows us to encapsulate our applications and their dependencies in containers, providing a consistent and isolated environment for running them. However, working with Docker containers sometimes raises challenges, especially when trying to access services running on the host machine, such as databases or APIs exposed on localhost.

In this blog post, we will explore the steps to connect from inside a Docker container to the localhost of the host machine. We will cover various scenarios, including connecting to services running on different ports and accessing services on both Linux and Windows host machines. We will also provide code examples and explanations for each step.

Step 1: Understand Docker Networking Basics

Docker containers have their own networking stack, which means that by default, they are isolated from the host machine's network. This isolation is done for security and stability reasons. However, it also means that if you try to access the host machine's localhost from inside a Docker container, you will encounter a connection error.

Step 2: Use Host Networking Mode

One way to allow a Docker container to access the host machine's localhost is by using the host networking mode. In this mode, the container shares the host machine's network namespace, allowing it to directly access the host's network interfaces. However, this mode has some security implications, as it exposes all host ports to the container.

docker run --network host <your_image>

Step 3: Use Docker Host IP Address

Another approach is to use the host machine's IP address instead of localhost. Docker provides a special DNS name "host.docker.internal" that resolves to the IP address of the host machine. This can be used as a replacement for localhost when connecting from inside the container.

Example (Python):

import requests

response = requests.get('http://host.docker.internal:8000/api')
print(response.text)


Step 4: Use Custom Docker Bridge Network

If you want more control over the networking and avoid using the host network mode, you can create a custom Docker bridge network and attach the container to it. This allows you to access services running on the host machine by using the host's IP address and the exposed port.

Example:

# Create a custom bridge network
docker network create my_bridge_network

# Run the container attached to the custom network
docker run --network my_bridge_network <your_image>


Step 5: Port Forwarding

In some cases, you may want to access a service running on a specific port on the host machine from inside the Docker container. In such scenarios, you can use port forwarding to map a port on the host to a port on the container.

Example:

# Forward port 8000 on the host to port 80 on the container
docker run -p 8000:80 <your_image>


Step 6: Consider Firewall and Security Settings

When connecting from inside a Docker container to the host machine's localhost or IP address, ensure that any firewalls or security settings on the host machine do not block the connection. In some cases, you may need to adjust firewall rules to allow incoming connections from Docker containers.


Let's walk through an example to demonstrate how to connect from inside a Docker container to the localhost of the host machine using the "host.docker.internal" DNS name.


Step 1: Create a Simple Flask API on the Host Machine

First, let's create a simple Flask API running on the host machine. This API will listen on port 8000 and respond with a "Hello, Docker!" message.

Create a file named app.py with the following content:


from flask import Flask

app = Flask(__name__)

@app.route('/api')
def hello():
    return 'Hello, Docker!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)


Step 2: Run the Flask API on the Host Machine

Open a terminal or command prompt and navigate to the directory where app.py is located. Then, run the Flask API using the following command:

python app.py


The Flask API should now be running on http://localhost:8000/api.

Step 3: Create a Docker Container to Access the API

Now, let's create a Docker container that will access the Flask API running on the host machine. We will use the python:3.9 base image and install the requests library to make HTTP requests to the API.

Create a file named `Dockerfile` with the following content:


# Use the Python 3.9 base image
FROM python:3.9

# Install the requests library
RUN pip install requests

# Set the working directory inside the container
WORKDIR /app

# Copy the Python script into the container
COPY app.py /app/app.py

# Run the Python script when the container starts
CMD ["python", "app.py"]


Step 4: Build and Run the Docker Container

Open a terminal or command prompt in the same directory where the Dockerfile and app.py files are located. Build the Docker image using the following command:

docker build -t my_flask_app .

Next, run the Docker container using the following command:

docker run -p 8000:8000 my_flask_app


Step 5: Access the Flask API from Inside the Docker Container

The Docker container is now running and should be able to access the Flask API on the host machine. Inside the container, we can use the requests library to make an HTTP request to the API.

Create a new terminal or command prompt window, and run the following command to access the Docker container:

docker exec -it <container_id> /bin/bash


Replace <container_id> with the ID of the running container. Once inside the container, run the following Python script:

import requests

response = requests.get('http://host.docker.internal:8000/api')
print(response.text)


This script will make an HTTP GET request to the Flask API running on the host machine using the "host.docker.internal" DNS name. It should print the "Hello, Docker!" message returned by the API.


In this example, we have seen, how to connect from inside a Docker container to the localhost of the host machine using the "host.docker.internal" DNS name. By leveraging this DNS name, we can access services running on the host machine from within the Docker container, making it easier to interact with services and applications during development and testing.

Labels: , ,

Tuesday 12 March 2024

Docker Container Commands for a DevOps Engineer


Read more »

Labels:

Wednesday 10 April 2024

Securely Managing AWS Credentials in Docker Containers


When working with AWS and Docker, a common challenge is securely managing AWS credentials within Docker containers. With the evolution of Docker and AWS services, there are now multiple strategies for handling AWS credentials more securely and efficiently, without resorting to less secure practices like hard-coding them into Docker images or passing them directly through environment variables.Read more »

Labels:

Friday 5 January 2024

Top Free DevOps Tutorials and Courses on Udemy

Are you looking to learn DevOps but not sure where to start? Look no further! This article will provide you with a list of top free DevOps tutorials and courses available on Udemy. Whether you're a beginner or an experienced professional, these courses will help you enhance your skills and knowledge in DevOps practices and tools.


1. Introduction to DevOps, Habits and Practices:

This course is perfect for those who are new to DevOps. It covers the basics of DevOps, its habits, and practices. You'll learn how to create world-class agile, lean, and continuous delivery teams that can deliver high-quality software quickly and efficiently. Check it out here: https://lnkd.in/dsvQQcYj

Read more »

Labels:

Saturday 31 December 2022

Automate Linux SysAdmin tasks with Ansible in 95+ examples - part 1



1. Decrypting an Ansible Vault

You have an Ansible Vault-encrypted file and need to decrypt it to access the secrets.

ansible-vault decrypt secret_file.yml

Ansible will prompt you for the Vault password used to encrypt the file.You can now access the decrypted data in secret_file.yml.

2.Using Ansible Vault in Ansible Playbooks

---
- name: Example Playbook
  hosts: localhost
  vars:
    sensitive_data: !vault |
      $ANSIBLE_VAULT;1.1;AES256
      66366232396135343865393765653837646562306361623863343535333563373362393865393338

You can create this encrypted variable using ansible-vault encrypt_string.

3.Checking Out Git Repositories via HTTPS with Ansible

You need to automate the process of checking out a Git repository hosted on HTTPS using Ansible.

Create a Playbook to Clone the Git Repository
Read more »

Labels: , ,

Sunday 9 October 2022

Building and Deploying a Containerized Application with Amazon Elastic Kubernetes Service - Lab

 SPL-BE-200-COCEKS-1 - Version 1.0.8

© 2023 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. All trademarks are the property of their owners.

Note: Do not include any personal, identifying, or confidential information into the lab environment. Information entered may be visible to others.

Corrections, feedback, or other questions? Contact us at AWS Training and Certification.

Read more »

Labels: ,

Thursday 20 January 2022

How to build a REST API with Flask, a popular Python web framework

Flask is a popular Python web framework that is commonly used to build REST APIs. It provides a lightweight and flexible architecture that makes it easy to get started with building web applications. In this tutorial, we will walk through the process of building a REST API with Flask.

Prerequisites:

Before we get started, you will need to have Python 3 and Flask installed on your machine. 

Read more »

Labels: , ,