HomeAboutPostsTagsProjectsRSS

docker

Updated
Words103
TagsRead1 minute

Docker daemon ports vs forward container ports

daemon ports

Docker daemon ports: add something like tcp://0.0.0.0:9999 in /etc/docker/daemon.json

It’s about the Docker daemon’s ability to accept commands (like starting/stopping containers, pulling images, etc.) over the network.

forwarding container ports

This is about exposing a specific port of a running container to the host or the outside world, allowing network traffic to reach the service running inside the container.

summary

In summary, setting the Docker daemon to listen on certain TCP ports is about remote management of the Docker engine itself, while forwarding ports for a container is about allowing external access to services running inside that container.

Updated
Words1532
TagsRead3 minutes

I recently ventured into deploying a service on Google Cloud Run. My goal was straightforward: create a service that fetches webpage titles and captures screenshots of URLs. However, the journey led me into a peculiar bug when I actually used it on Goole Cloud Run.

The Bug

During the development phase, I worked with a python:3.11-slim base image on macOS, and my Dockerfile functioned without a hitch. Here’s a snapshot of the Dockerfile I used:

from python:3.11-slim

RUN apt-get update && \
    apt-get install -y git && \
    python -m pip install --upgrade pip && \
    pip install -r requirements.txt && \
    pip install pytest-playwright && \
    playwright install-deps && \
    playwright install && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

Yet, upon deploying to Google Cloud Run and initiating the screenshot capture process, I hit a snag:

playwright._impl._api_types.Error: Executable doesn't exist at /home/.cache/ms-playwright/chromium-1084/chrome-linux/chrome
╔═════════════════════════════
║ Looks like Playwright was just installed or updated.                   
║ Please run the following command to download new browsers: 
║                                                                                                            
║     playwright install                                                                          
║                                                                                                            
║ <3 Playwright Team                                                                         
╚═════════════════════════════

Official Playwright Docker Image Saves the Day

Rather than wrestle with the error, I pivoted to an official Docker image of Playwright, and skipped installation of dependency:

mcr.microsoft.com/playwright/python:v1.39.0-jammy

docker image

Let’s dig down the issue:

The Compatibility Issue

Playwright demands compatibility. It officially supports Python versions 3.8 and higher, and it requires specific Linux distributions:

  • Debian 11 (bullseye)
  • Debian 12 (bookworm)
  • Ubuntu 20.04 (focal)
  • Ubuntu 22.04 (jammy)

However, on docker environment, the official image is only based on Unbuntu .

Use ENV PLAYWRIGHT_BROWSERS_PATH

After some search and experiments, I found the only solution in to specify the chromium binary files using ENV PLAYWRIGHT_BROWSERS_PATH during install . source code Dockerfile also use this environment variable to specify the broswer executable path.

using python:3.11-slim-bookworm

FROM python:3.11-slim-bookworm

ENV PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install git
RUN apt-get update

# install playwright or whatever
RUN python -m pip install --upgrade pip && \
    pip install -r requirements.txt

# install chrominum
RUN PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright && \
    playwright install --with-deps chromium

using ubuntu:22.04

FROM ubuntu:22.04

ENV PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install git
RUN apt-get update && \
    apt-get install -y python3-all python-is-python3 python3-pip

# install playwright or whatever
RUN python -m pip install --upgrade pip && \
    pip install -r requirements.txt

# install chrominum
RUN PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright && \
    playwright install --with-deps chromium

Memory requirement of Google Cloud Run

The playwright 1.39.0 requires slightly more than 512MB of memory to run on Google Cloud Run. Adjust the memory limit on GCR, as it’s 512 MB by default.

Conclusion

Use the official Docker image to save time, or specify the PLAYWRIGHT_BROWSERS_PATH environment variable on a supported linux docker image.

Further reading:

Updated
Words412
TagsRead2 minutes

When building Docker images, it is common to configure Git within the image using commands like git config --global user.name and git config --global user.email. These commands update the global Git configuration for the user during the image build process. However, when running git commit inside a container that is spawned from the image, you may encounter a “please tell me who you are” error. This error occurs because the Git configuration set during the build process does not persist in the container.

The Issue

The git config --global command updates the global Git configuration for the user running the command. By default, the configuration is stored in a .gitconfig file located in the home directory of the user. When you add the git config commands to your Dockerfile, they only affect the image build process and do not persist when a container is created.

Solution Methods

Method 1: Set Git Config at Runtime

One way to resolve this issue is by setting the Git configuration within the container at runtime. This can be done manually or by adding the commands to a startup script. For example, you can run the following commands when starting the container:

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

By setting the Git configuration at runtime, it ensures that the configuration is applied consistently whenever a container is started.

Method 2: Copy a Prepared .gitconfig File

Another approach is to prepare a .gitconfig file on your host system with the desired settings and then copy it into the Docker image. This method allows you to define the Git configuration outside of the Docker build process.

To do this, create a .gitconfig file on your host system with the desired Git settings. For example:

[user]
    name = Your Name
    email = email@example.com

Next, include the following line in your Dockerfile:

COPY .gitconfig /root/.gitconfig

This line copies the .gitconfig file from the host system into the /root/.gitconfig path within the image. When a container is created from the image, it will include the copied .gitconfig file, ensuring that the Git configuration is persisted.

Conclusion

When using Git within Docker containers, it is important to ensure that the Git configuration persists across container runs. This can be achieved by setting the Git configuration at runtime or by copying a prepared .gitconfig file into the container. By following these approaches, you can avoid the “please tell me who you are” error and have consistent Git configurations within your Docker containers.