Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Last active July 31, 2023 18:31
Show Gist options
  • Select an option

  • Save ehzawad/4f689cb4d84a290e9e8dfb0b1886877a to your computer and use it in GitHub Desktop.

Select an option

Save ehzawad/4f689cb4d84a290e9e8dfb0b1886877a to your computer and use it in GitHub Desktop.
Docker Python OMG!!
```bash
HOST
sudo chown 1000:1000 /home/ehzawad/python-dockerbaby/src
chmod 777 /home/ehzawad/python-dockerbaby/src
Container
ARG version=alpine
FROM python:${version} as base
# Installing basic tools
RUN apk add --no-cache bash
# Create a user with specific UID and GID
RUN addgroup -g 1000 ehzawad && adduser -D -G ehzawad -u 1000 ehzawad
# Switch to user
USER ehzawad
# Install iPython
RUN pip install ipython
# Create app directory
WORKDIR /app
# Create the src directory and set permissions
RUN mkdir src
# Volume mount point
VOLUME /app/src
# Copy the startup script
COPY --chown=ehzawad:ehzawad startup.sh /app/startup.sh
# Make the startup script executable
RUN chmod +x /app/startup.sh
# Expose the port
EXPOSE 80
# Set the startup script as the entry point
ENTRYPOINT ["/app/startup.sh"]
build and run
docker build -t python-dockerbaby:1 .
docker run -it -v /home/ehzawad/python-dockerbaby/src:/app/src -p 80:80 --network python-dockerbaby-net python-dockerbaby:1
docker network
docker network create python-dockerbaby-net
```
@ehzawad

ehzawad commented Jul 31, 2023

Copy link
Copy Markdown
Author

#!/bin/bash

if [ "$1" == "ipython" ]; then
shift
ipython "$@"
elif [ "$1" == "python" ]; then
shift
python "$@"
else
/bin/bash
fi

@ehzawad

ehzawad commented Jul 31, 2023

Copy link
Copy Markdown
Author

Both of the commands are working because of the way you have set up your volume mapping and the current working directory inside the Docker container.

  1. docker run -it -v /home/ehzawad/python-dockerbaby/src:/app/src -p 80:80 --network python-dockerbaby-net python-dockerbaby:1 python /app/src/hello.py

    • Here you are explicitly pointing to the full path /app/src/hello.py inside the container where the volume is mounted. The /app/src directory in the container is mapped to the /home/ehzawad/python-dockerbaby/src directory on your host machine.
  2. docker run -it -v /home/ehzawad/python-dockerbaby/src:/app/src -p 80:80 --network python-dockerbaby-net python-dockerbaby:1 python src/hello.py

    • In this case, you are using a relative path src/hello.py. Since you've set the working directory inside the Dockerfile to /app (WORKDIR /app), this path is relative to /app, and it gets resolved to the same full path /app/src/hello.py.

Both commands are, therefore, pointing to the same location inside the container, and that's why they are both working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment