Skip to main content

Python Container

Setup

  1. Create python scripts (e.g. index.py)

  2. In project’s root, add the following files

    1. Dockerfile
    2. docker-compose.yml
    3. .dockerignore
  3. Build Docker container using Docker cli commands or docker-compose

Docker CLI Commands

Build container

docker build -t docker-container-name .

Run previously built container

docker run docker-container-name

Docker Compose Commands

Build container

docker-compose up -d --build

Run previously built container

docker-compose up

Stop running container

docker-compose stop

Non-Interactive Python Script Example

Sample package.json

package.json
{
"name": "python-docker-example",
"version": "0.0.0",
"scripts": {
"docker-build": "docker build -t python-docker-example .",
"docker-run": "docker run python-docker-example",
"dc-build": "docker-compose up -d --build",
"dc-up": "docker-compose up",
"dc-stop": "docker-compose stop"
},
"private": true
}

index.py

print ("Hello World")

Dockerfile

# pull official base image
FROM python

# set working directory
WORKDIR /usr/src/app

# Copy the rest of your app's source code from your host to your image filesystem.
COPY . .

# execute python script
CMD ["python", "./index.py"]

.dockerignore

.dockerignore
.editorconfig
.git
.gitignore
*.md
Dockerfile
Dockerfile.prod
docker-compose.yml

docker-compose.yml

version: '3.7'

services:
python-docker-example:
container_name: python-docker-example
build:
context: .
dockerfile: Dockerfile
image: python-docker-example
volumes:
- '.:/usr/src/app'

Write to Host

In this example, the Python script will create and write to a .txt file in a folder named ‘output’.

index.py

import os
from datetime import datetime

today = datetime.today()
dateFormat = "%m-%d-%Y %H:%M:%S"
generatedFilename = "output/test-generated.txt"

# Remove generated file from output directory if it already exists
if os.path.exists(generatedFilename):
os.remove(generatedFilename)
print("Removed existing file from output directory")

# Create file and write to it
with open(generatedFilename, "w") as file:
file.write("Hello World!\n" + today.strftime(dateFormat))

# Print some info about the file to the console
lastModifiedTime = os.path.getmtime(generatedFilename)
lastModifiedDate = datetime.fromtimestamp(lastModifiedTime)
print("Created " + generatedFilename + " (" + lastModifiedDate.strftime(dateFormat) + ")")

Dockerfile

# Create a container based on a parent Python container.
FROM python

# Create a directory to save the Python script's generated file to.
# We'll bind mount this directory when running the container so we have access to the file in the host.
RUN mkdir /output

# Copy the Python script into the root of the container
ADD index.py /

# Execute the Python script
CMD ["python", "./index.py"]

README.md

README.md
# Basic Python Docker example

## Build the Docker container

`
docker build -t python-write-host .
`

## Run the Docker container with a bind mount so we can get the file created by the Python script

When creating a bind mount (hostDir:containerDir), use the absolute path to the host.

`
docker run -v /Users/jcornwell/workspaces/docker-examples/python-write-to-host/output:/output python-write-host
`

You can also use the --mount param to specify the bind mount on a directory. Using this approach, I was able to specify a relative path on the host.

`
docker run --mount type=bind,source="$(pwd)"/output,target=/output python-write-host
`