mandag 30. desember 2024

Docker for Python and PHP Development: A Comprehensive Guide with VS Code

Introduction

Docker has revolutionized how we develop and deploy applications. It ensures consistent environments, simplifies dependency management, and makes it easier to collaborate. This post serves as a comprehensive guide to using Docker effectively with Python and PHP projects, particularly when using VS Code as your IDE. We’ll explore key concepts, best practices, and the transition from development to production.

Why Docker for Development?

Docker containers offer several advantages:

  • Consistent Environments: Eliminate “it works on my machine” problems by providing an environment identical to staging and production.
  • Dependency Isolation: Avoid conflicts between project dependencies.
  • Simplified Onboarding: New developers can set up their environment quickly by running a simple Docker command.
  • Collaboration: Sharing Docker images streamlines team collaboration.
  • Scalability: Containers are fundamental for building scalable applications.

Best Practices: Docker + Python/PHP + VS Code

Let’s dive into the best ways to integrate Docker into your Python or PHP development workflow using VS Code.

1. Project Structure:

  • Dedicated Dockerfile: A Dockerfile should reside at the root of your project. It defines how the Docker image is built.
  • docker-compose.yml (Recommended): Use Docker Compose for managing multiple containers (application, database, etc.).

2. Dockerfile Best Practices:

  • Multi-Stage Builds: Keep your final image small and secure by using multiple stages during the build process.
  • Use Official Images: Start with official Python or PHP images from Docker Hub (e.g., python:3.11-slim, php:8.2-apache).
  • Pin Versions: Use specific versions for Python, PHP, and other dependencies for stability.
  • Install Dependencies: Use requirements.txt (Python) or composer.json (PHP) for dependency management.
  • Copy Code Last: Copy application code after installing dependencies to leverage Docker’s caching.
  • Set a User (Security): Create and use a non-root user.
  • Expose Ports: Use EXPOSE to declare ports your application will use.
  • Clear Entrypoint: Define the application’s start command using CMD or ENTRYPOINT.

Example Dockerfile (Python):

# Stage 1: Build Dependencies
FROM python:3.11-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Stage 2: Final Image
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /app/ ./
COPY . .
RUN useradd -ms /bin/bash appuser
USER appuser
EXPOSE 5000
CMD ["python", "app.py"]

Example Dockerfile (PHP):

# Stage 1: Build Dependencies
FROM php:8.2-apache AS builder
WORKDIR /var/www/html
COPY composer.json composer.lock ./
RUN composer install --no-scripts --no-interaction --prefer-dist

# Stage 2: Final Image
FROM php:8.2-apache
WORKDIR /var/www/html
COPY --from=builder /var/www/html/vendor ./vendor
COPY . .
RUN chown -R www-data:www-data /var/www/html
EXPOSE 80
CMD ["apache2-foreground"]

3. docker-compose.yml (Essential):

  • Define Services: Configure containers and their relationships (e.g., app, db).
  • Environment Variables: Set environment variables using environment:.
  • Volumes: Mount host directories to container directories using volumes:.
  • Networking: Create custom networks for service communication.
  • Build from Dockerfile: Use build: . to build an image or image: to use a pre-built image.

Example docker-compose.yml (Python):

version: "3.9"
services:
  app:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
    environment:
      DEBUG: "True"
    depends_on:
      - db
  db:
    image: postgres:14
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: "myuser"
      POSTGRES_PASSWORD: "mypassword"
      POSTGRES_DB: "mydatabase"
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

Example docker-compose.yml (PHP):

version: "3.9"
services:
  app:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db
    environment:
      DB_HOST: db
      DB_USER: root
      DB_PASSWORD: password
      DB_NAME: my_database
  db:
    image: mariadb:10.6
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: my_database
    ports:
      - 3306:3306

4. VS Code Integration:

  • Docker Extension: Install the official Docker extension for VS Code:
    • Docker Explorer: View containers, images, volumes, and networks.
    • Dockerfile syntax highlighting.
    • Container Management: Manage containers from VS Code.
    • Remote Container Development: Develop code inside containers using Devcontainers.
  • Devcontainers (.devcontainer):
    • Create .devcontainer/devcontainer.json for a configured development environment inside Docker.
    • Specify dockerFile or image.
    • Set workspaceFolder, forwardPorts, extensions, settings, etc.

Example devcontainer.json (Python):

{
    "name": "Python Devcontainer",
    "build": {
        "dockerfile": "../Dockerfile"
    },
    "workspaceFolder": "/app",
    "forwardPorts": [
        5000
    ],
    "customizations": {
        "vscode": {
            "extensions": [
                "ms-python.python"
            ]
        }
    }
}

Example devcontainer.json (PHP):

{
    "name": "PHP Devcontainer",
    "build": {
      "dockerfile": "../Dockerfile"
    },
    "workspaceFolder": "/var/www/html",
    "forwardPorts": [
      80
    ],
    "customizations": {
      "vscode": {
          "extensions": [
              "bmewburn.vscode-intelephense-client"
          ]
        }
    }
  }

5. Managing File Changes and Git:

  • Volumes: Use bind mounts (e.g., volumes: - .:/app) to map the host code to the container. Changes on either side are synchronized.
  • Git on the Host: Manage your Git repository on the host, not inside the container. VS Code integration handles Git tasks outside the container environment.
  • .dockerignore: Create a .dockerignore file to exclude unnecessary files from Docker images (e.g., .git, node_modules, etc.) to improve the performance of the image building and to avoid sensitive data to leak.

6. Production Deployment Considerations:

  • Move Code Into the Image: The main difference between production and development is that in production we need our code inside the container. No volumes, therefore no dynamic modifications.
  • Multi-Stage Builds: Ensure your production Dockerfile has the proper configuration.
  • Remove Volume Mounts: Do not use volume mounts in your production docker-compose.yml.
  • Set Environment Variables: Use environment variables for configuration settings.
  • CI/CD Integration: Use a CI/CD pipeline for building and deploying images from your production branch.
  • Secrets Management: Do not hardcode secrets. use tools such as Hashicorp Vault for better security.

7. Going from Production to Development:

  • Create a Dockerfile.dev: Create a development version of your Dockerfile without the COPY . . or equivalent for source code.
  • Add Development Tools: Reinstall development tools.
  • Use a docker-compose.dev.yml: Use a separate Compose file that includes volume mounts.
  • Mount Code: Map the local code directory to the container using volumes.
  • Run: use docker compose -f docker-compose.dev.yml up -d --build
  • Debugging: Now you can use your favourite debugger to debug your code inside the container.
  • Naming Conventions: The filename Dockerfile.dev is a convention for human understanding but not recognized by docker itself. Docker only reads the Dockerfile and will require a -f parameter to indicate the filename to be used if it is not called Dockerfile.

8. Extracting Source Code from Production Images:

  • Temporary Container: Run a temporary container with docker run --name temp_container <image-name> /bin/sh.
  • Copy using docker cp: Copy the source code directory using docker cp temp_container:<source-directory> <host-destination>.
  • Stop and Remove: Remove the temporary container with docker stop temp_container && docker rm temp_container.

Conclusion:

Docker, when used correctly with Python, PHP, and VS Code, streamlines development and deployment. Understanding best practices for Dockerfiles, docker-compose.yml, and VS Code integration is key to a smooth workflow. This guide should equip you with the knowledge to build efficient and maintainable containerized applications.

Key Takeaways:

  • Use multi-stage builds for smaller, secure images.
  • Utilize docker-compose.yml for multi-container setups.
  • Leverage VS Code’s Docker extension and Devcontainers.
  • Use volumes for efficient development but not for production.
  • Handle the transition between production and development using different docker-compose.yml configurations and Dockerfile files.

Setup MySQL repliaction from scratch.

 How to setup a replication instance from only a single server.


First backup the database, 

MASTER SERVER:

Edit the MySQL configuration file (/etc/mysql/mysql.conf.d/mysqld.cnf ) to enable binary logging and specify the server as the master.

[mysqld] 
server-id=1 
log_bin=mysql-bin

Run the following command to restart mysql deamon:

sudo systemctl restart mysql

Backup the original database on the master server.

--master-data=2 er important. it includes the binlog information in the backup file. to be used to synchronize the slave to the master.

mysqldump -u root --p --opt --single-transaction --master-data=2 --databases db_to_backup > backup.sql > /dev/null 2>&1 &

slog in to the MySQL server:

mysql -u root -p CREATE USER 'replica_user'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%'; FLUSH PRIVILEGES;

Run the following command inside mysql command line.

CREATE USER 'replica_user'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%'; FLUSH PRIVILEGES;


mysql -u root -p CREATE USER 'replica_user'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%'; FLUSH PRIVILEGES;

d] 

mysqldump -u root --p --opt --single-transaction --master-data=2 --databases db_to_backup > backup.sql > /dev/null 2>&1 &

Then setup the mysql server instance on a new computer or vm and import the data, 

søndag 7. mai 2023

Visual Studio code replace curly braces with brackets (PHP)

To replace curly braces with brackets using search and replace in Visual Studio code for PHP use this search/replace pattern: 

Enable regex search.

Example:

$image{0} to $image[0]

 

Search: (\$[a-z]+)\{(.)\} 

Replace: $1[$2]


 

søndag 2. mai 2021

PI24 + RTL-AIS

  1. Burn PI24 to SDcard
  2. boot and setup wpa_supplicant and raspi_config if applicable
  3. setup FR24 and it working
  4. install giut and build-essentials
  5. install dependencies, not librtlsdr-dev if you are compiling your own version to get rid of errors see below.
  6. git clonw rtl_ais and compile
    https://pysselilivet.blogspot.com/2020/05/ais-reciever-for-raspberry-pi-2-channel.html
  7. keep rtl-sdr units always connected to the same USB ports on the RPi and use device index parameters to point which unit to which software

    Compiling custom librtlsdr-dev

torsdag 1. november 2018

stepper motor microstepping vs current.

I had a problem with missed steps on my CNC, as far as I can see it relates to the motor drive current. Higher current more lost steps and vice versa.

mandag 20. august 2018

Google Sketchup over RDP problem SOLVED

How to make Google Sketchup work over RDP (Remote Desktop).
I have had problems launching sketchup over RDP session, it throws this error:

If Sketchup has been started locally before using RDP it works fine, sometimes is crashes but most of the time it's ok.

I googled and came up with a smart solution:

- Create a Batch file which includes the following two lines:

tscon 1 /dest:console
"C:\Program Files\SketchUp\SketchUp 2017\SketchUp.exe"
- Run the batchfile as administrator (this will diconnect your current remote desktop session)
- Wait the time sketchup normally takes to start before you reconnect the remote desktop. If you reconnect too quickly you will get the OpenGL error.

I also found that you have to disable the startup screen so the sketchup will launch directly into editing mode.

You can also add a path and filename to a SKP file after the .exe line in the batchfile to load a project automatically.

BTW: This assumes that you are using session 1 on the computer.

mandag 13. august 2018

Mach3 stepper motor problem

Had a problem after testing mach3 for the first time, could not get the stepper motors to move as expected. Only stuttering av vibrating unevenly, did an extensive google search round but I did find any solution for my problem.

I did everything like, switching from WinXP ACPI to standard installation, tried Windows 7 32 bit, nothing helped. tried Other drivers and other motors.


My solutions was enable STEP LOW ACTIVE, that did the trick. The motors are running smooth as silk now.

BTW: I'm currently running a USB breakout board called Novusun NVUM_SK. el cheapo china card. Seems to work fine until now, have read somewhere about some incompability with mach3. I don't know, we'll see.

link to my breakout card, where you can also find the driver plugin for mach3. The card did not kcome with any software included when I bought it at ebay.

fredag 13. april 2018

Google Spreadsheet importDATA error.

I've been struggling with the IMPORTDATA function in Google Spreadsheet to import CSV data, everything seems to work normally the first time I load the file, but after a while I get NA and error in the spreadsheet, and of course the data is removed from the sheet.

I did read somwhere that the problem may be related to caching of data, I do not know if the problem is local cache or remote cache at google servers.

I found a solution which seems to work better than others, it may not work in your case but try it.

The normal way I add the IMPORTDATA formula is as follows:

=IMPORTDATA("http://www.data.com/testdata")
this fails, the solution I foudn was to add IFERROR and repeat the IMPORTDATA and adding trailing slashes to the URL. Trailing slashes does not seem to alter the url at all (may be a problem if you link directly to a CSV file, I'm using url_rewrite on my webserver), it seems to trick the cache problem. For now I'm using this formula with two nested IFERRORs:

=IFERROR(IMPORTDATA("http://www.data.com/testdata/");IFERROR(IMPORTDATA("http://www.data.com/testdata//");IMPORTDATA("http://www.data.com/testdata///"))) 

You can add more IFERRORS as well with even more trailing slashes.

fredag 3. oktober 2014

HOWTO use SSH and PuTTY as SOCKS proxy server for secure surfing on public wifi

I've been looking around for different solutions to secure my wlan connection when traveling, lots of hotels are only using open wifi networks and that's a BIG security flaw.
This solution may also work for bypassing webfilters a work, if you want to surf on websites not allowed by your employer this will help you. (but I'm not liable for any damage you cause your employers network/computers or if you get fired for doing this :) ).

After trying several solutions I came across a blogpost about using a SSH connection as a secure proxy server for whatever software you're using. But I had problems getting it to work, I found only poor guides on how to do it. So here I will try to make a better one.

I'm running windows 8 on my laptop so I will be demonstrating this using PuTTYwhich is a SSH client for Windows. Google it.

The only thing you need for this to work is access to a SSH server, this can be your webhost, home server or whatever.


  • First you need to download PyTTY. Download
  • When you start PyTTY you will see the window below, here you fill in your SSH server hostname, port and choose SSH. (shown with red circles)


  • When you have entered your hostname and port you click on SSH on the category list on the left, and then choose Tunnels.
  • A new configuration page will come into view, identical to the image below. Fill in the following information:
    Source Port (the port you want the proxy to run on, normally 8080)
    Destination (it's the same hostname and port as on the first config page but with a colon between hostname and port number)
    Choose Dynamic just below destination 




  • Click Add to add the configuration for this connection. You should now see a number in the text field above. See image below.



  • You're now finished with your configuration, click open to start the SSH session and log in normally using your username and password at the SSH server.


After finishing the PyTTY configuration you'll also need to add the Proxy server to your browser, In firefox, go to settings, network and choose connection settings.
Here you'll choose manual settings and in the SOCKS field you write localhost and in port you write 8080 (same we set in the PuTTY configuration earlier).
It does not matter if the SOCKS setting is on SOCKS4 or SOCKS5.
Click OK and you're done.

You are now surfing with the SSH serves IP adress as the visible one, go to whatismyip.com to verify your public IP adress.

mandag 8. september 2014

ADS-B reception with RTL SDR dongle and home made collinear coax antenna.

I figured out I wanted to try ADS-B reception with my dustcollecting dongle, and how could I know how much fun that was. The range out of the box was pretty impressive (and I will make a comparison chart later), but I wanted to build a better one. The one I decided to build was a collinear coax antenna. I used cheap RG-6U coax cable with eight segments and a 75 ohm resistor on top. Incredibly easy an quick to build and as far as I can see very efficient. I use windows software RTL1090, and save data into a MySQL database via a PHP script on my server.


This is my reception map, I'm very impressed that with an indoor antenna I'm able to reach at least north-west 150 NM  out to sea. Of course the antenna is mounted on the wall in the north-west corner of my house, and I've got very few obstacles in the vicnity in that direction. Aircraft need to be at cruising altitude as well ~30-40000 feet. The map is created by a tool called SBSplotter, see http://sonicgoose.com/superimpose-polar-plot-in-google-earth/ for more information.

Hopefully I will be building a 12 segment antenna to test later, that will be fun.


Here you can see all ADS-B tracks I've collected for about 20 hours today. 

See http://www.balarad.net/ for detailed instructions on how to build the antenna.


tirsdag 8. april 2014

HOWTO Start Dante Socks server together with OpenVPN client during reboot in Ubuntu 12.04

After installing Dante SOCKS server it would not start during reboot, thee problem was the OpenVPN tun0 interface was not up when dante was starting, and it would not start with error tun0 not found.

I did quite a bit of googling before a solution came to me, i did find it on the page: Turn your Raspberry Pi into a VPN gateway.

Here is a overview of the solution (slightly modified for the original):

First remove the normal start and stop hooks in rc*.d
sudo update-rc.d danted remove
Next create the scripts.
sudo mkdir /etc/dante_scripts
sudo nano /etc/dante_scripts/myprovider.up
Add the following contents to /etc/dante_scripts/myprovider.up:
#!/bin/bash
if [ "$1" = tun0 ]; then
 /etc/init.d/danted start
fi
sudo nano /etc/dante_scripts/myprovider.down
Add the following contents to /etc/dante_scripts/myprovider.down:
#!/bin/bash
if [ "$1" = tun0 ]; then
 /etc/init.d/danted stop
fi
Make the scripts runnable:
sudo chmod +x /etv/dante_scripts/*
Add the hooks to /etc/default/openvpn:
sudo nano /etc/default/openvpn
Modify the OPTARGS:
OPTARGS="--up /etc/dante_scripts/myprovider.up --down /etc/dante_scripts/myprovider.down"


onsdag 5. mars 2014

WORKING HOWTO Install nVidia (CUDA) drivers on Kali 1.0.6

HOWTO Install nVidia drivers on Kali 1.0.6.

I used alot of time and effort to search he net for a guide on how to install nVidia (CUDA) drivers on Kali 1.0.6. I did try the solution described on the official Kali site, but it did not work. Only got the black screen with blinking cursor in upper left corner during reboot. Most of the other guides I found was more or less the same.

Until I stumbled upon this a blog called Samiux's Blog. Which had a guide that worked flawlessly! BIG thank you for this guide.

Guide is shown below but visit his blog for more information. Link to the guide here

Step 1 :

apt-get install libcudart4 linux-headers-$(uname -r) nvidia-cuda-toolkit

Step 2 :

mkdir /etc/X11/xorg.conf.d

echo -e 'Section "Device"\n\tIdentifier "nVidia GPU"\n\tDriver "nvidia"\n\tOption "NoLogo" "1"\n\tOption "RenderAccel" "1"\n\tOption "TripleBuffr" "true"\n\tOption "MigrationHeuristic" "greedy"\nEndSection' > /etc/X11/xorg.conf.d/20-nvidia.conf


Step 3 :

Update the boot loader to disable the open source nvidia display driver.

sed 's/quiet/quiet nouveau.modeset=0/g' -i /etc/default/grub
update-grub
reboot

mandag 27. januar 2014

How to manage SDR software across multiple computers with synced configuration. through cloud storage.

YouTube user Eric William has released another useful video presentation of his equipment. This time he focuses on the software and cloud solution he uses to get the configuration synced across all PCs. Alot to learn for new SDR users.



lørdag 18. januar 2014

Receiving ADS-B signals from inside an Boeing 737-800 in flight with RTL-SDR

This guy has hooked up his RTL-SDR receiver inside an Ryanair aircraft during flight at 36000 feet, and he is able to receive the ADS-B signal without problems.

fredag 3. januar 2014

Removing annoying BEEP tone from Maycom FR-100

Are you annoyed by the BEEP tone from the Maycom FR-100?
It's a pretty simple fix for that, remove a resistor on the PCB inside the maycom fr-100.

You need to remove the 4 screws on the backpanel in order to remove the panel, then you need to remove to screws from inside the batterycompartment. You now remove the battery compartment.
Now fire up your soldering iron and prepare to remove a SMD resistor marked R1 on the photo below, be aware that is pretty tiny and you do not need alot of heat to remove it.

With the resistor removed get the maycom fr-100 back together and you do not have any more BEEP tone.