Trying to build simple python docker container - dockerfile

Im trying to make a docker container run this python script I made. Every time I run the container, I get an error of it not being able to find the script even though it is in that listed directory.
python: can't open file '/home/johnb/manual_into_ckl.py': [Errno 2] No such file or directory
Dockerfile
FROM python:3.10
ADD manual_into_ckl.py .
WORKDIR /home/johnb
RUN pip install pandas
CMD [ "python", "manual_into_ckl.py"]
Very new to this, not sure what else to do. Thanks
Changed the workdir, changed permissions, all still the same error.

As #DavidMaze commented, you need to set the working directory before copying the file. I also suggest using COPY instead of ADD (it is preferred).
FROM python:3.10
WORKDIR /home/johnb
RUN pip install pandas
COPY manual_into_ckl.py .
CMD [ "python", "manual_into_ckl.py"]

Related

Cannot launch my Django project with Gunicorn inside Docker

I'm new to Docker.
Visual Studio Code has an extension named Remote - Containers and I use it to dockerize a Django project.
For the first step the extension creates a Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10.5
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# File wsgi.py was not found. Please enter the Python path to wsgi file.
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "project.wsgi"]
Then it adds Development Container Configuration file:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.238.0/containers/docker-existing-dockerfile
{
"name": "django-4.0.5",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../Dockerfile"
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
And finally I run the command Rebuild and Reopen in Container.
After a few seconds the container is running and I see a command prompt. Considering that at the end of Dockerfile there's this line:
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "project.wsgi"]
...the Django application must be running, but it isn't and http://127.0.0.1:8000 refuses to connect.
But if I run the same command at the command prompt:
gunicorn --bind 0.0.0.0:8000 project.wsgi
It works just fine?
Why? All files are created by VSCode extension. Same instructions are posted on VSCode official website, yet it's not working.
P.S:
When the container loaded for the first time, I created a project called 'project' so the folder structure is like this:

Sample DockerFile for playwright test

trying to dockerize my playwright test and having looking for some reference on how i can write the dockerfile. Did some searches but could not find much help. Could anyone share a sample that i can refer.
thanks
At the top of your Dockerfile you can put:
FROM mcr.microsoft.com/playwright:focal
followed by the code that executes your tests.
Check the docs here for more info.
Copy paste Dockerfile for those who have no idea how to use docker, source
FROM mcr.microsoft.com/playwright:focal
# copy project (including tests)
COPY . /e2e
WORKDIR /e2e
# Install dependencies
RUN npm install
# Install browsers
RUN npx playwright install
# Run playwright test
CMD [ "npx", "playwright", "test", "--reporter=list" ]

How to translate flask run to Dockerfile?

So I'm trying to learn how to containerize flask apps and so far I've understood two ways of firing up a flask app locally:
One is to have this code in the main file:
if __name__ == '__main__':
APP.run(host='0.0.0.0', port=8080, debug=False)
and run with
python3 main.py
The other is to remove the above code from main.py, and just define an environment variable and do flask run:
export FLASK_APP=main.py
flask run
I tried to convert both of these methods into a Dockerfile:
ENTRYPOINT["python3", "main.py"]
which works quite well for the first. However when I try to do something like:
ENV FLASK_APP "./app/main.py"
ENTRYPOINT ["flask", "run"]
I am not able to reach my server via the browser. The container starts up all well, just that it's not reachable. One trick that does work, is if I add the host address in the entrypoint like so:
ENTRYPOINT ["flask", "run", "--host=0.0.0.0"]
I am not sure why do I have to the --host to the entrypoint when locally I can do also without it. Another funny thing that I noticed, was that if I put the host as --host=127.0.0.1, it still doesn't work.
Can someone explain what really is happening here? Either I don't understand the ENTRYPOINT correctly or maybe flask.. or maybe both.
EDIT: The whole Dockerfile for reference is:
FROM python:stretch
COPY . /app
WORKDIR /app
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
ENV FLASK_APP "/app/main.py"
ENTRYPOINT ["flask", "run", "--host=127.0.0.1"]
Try to define FLASK_APP env via absolute path.
Or put this string to your Dockerfile:
WORKDIR /path/to/dir/that/contains/main.py/file
Oh sorry. Host in ENTRYPOINT statement must be 0.0.0.0 :
ENTRYPOINT ["flask", "run", "--host=0.0.0.0"]
And do not forget to tie 5000 port outside via -p option :
docker run -p 5000:5000 <your container>
i believe this task would be better accomplished implementing CMD as opposed to ENTRYPOINT.
(you should also define the working directory before running COPY command.)
for example, your Dockerfile should look something like..
FROM python:stretch
WORKDIR /app
COPY . .
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
CMD python3 main.py

how to bring out django project from virtualenv

I have django project in virtualenv and now I am publishing it in server but the problem is I can not move project from virtualenv, when I do this then related packages inside site-package, cant be read and errors occur, how can I bring out my project from virtualenv without any issuing
Create a new virtualenv on the server. It's easy
Step 1 Get the list of modules in the current virtualenv
source /path/to/current/bin/activate
pip freeze > /tmp/requirements.txt
Step 2 Create a new virtualenv. Login to your new server, copy the requirements file there. Then either change into a suitable directory before excuting the virtualenv command or give a full path.
deactivate
virtualenv -p python envname
Step 3 Install modules
source envname/bin/activate
pip install -r /tmp/requirements.txt
That's it.
As #bruno has pointed out, you really should be using a virtualenv on the server. And you should be using it on your local dev server as well. Then you can be really sure that the code will run at both ends without any surprises.

Trouble install PIP Windows

I have read posts on here but something doesn't seem to work. I am new to Python dev. I am running windows 7 64bit.
I am trying to install pip which I have downloaded and have easy_install in my c:\python27\scripts directory. When I use powershell and type
"python easy_install pip"
I get the error message:
"can't open file easy_install": [Errno 2] No such file or directory.
This seems odd as I can see the easy_install.exe is in that directory.
I have added c:\python27\scripts to my system PATH but, to be honest, I am not sure if this is right.
Could someone please help?
Thank you.
The selected answer dint fix it
This did:
python -m pip install -U pip
The best way to fix this is to compile easy_install yourself. First download the source code, and then compile it using python.
1 - Go here.
2 - Then extract the file, using 7-zip, or any software that can unzip a tar file. This is a tar.gz file, so after one extraction, it will give you the tar file, and then it will give you the actual directory and files for setup tools
3 - Go into the directory where you stored the contents of the extraction (using command prompt). For example, if you stored the directory on your desktop, you cd Desktop and then you go
cd dir_where_easy_install_is.
4 - Then run this command python setup.py install
That will install easy_install for you.
5 - Go back to your command line again, and then just type in easy_install pip, and that will install pip.
Then go ahead, and test it out, by installing django, just go into your command prompt, and enter this pip install django. After the download and the install, type in pip list, and see if django is in there. If it is, then pip has been successfully installed.
This worked running python from it's main dir against the easy_install.py in the extracted setuptools directory.
Then pip files should be in the Scripts dir.
Don't forget to run your CMD as administrator.