Last active
July 31, 2023 18:31
-
-
Save ehzawad/4f689cb4d84a290e9e8dfb0b1886877a to your computer and use it in GitHub Desktop.
Docker Python OMG!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```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 | |
| ``` | |
Author
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.
-
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.pyinside the container where the volume is mounted. The/app/srcdirectory in the container is mapped to the/home/ehzawad/python-dockerbaby/srcdirectory on your host machine.
- Here you are explicitly pointing to the full path
-
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.
- In this case, you are using a relative path
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
#!/bin/bash
if [ "$1" == "ipython" ]; then
shift
ipython "$@"
elif [ "$1" == "python" ]; then
shift
python "$@"
else
/bin/bash
fi