Running Simple Python Script with Docker on Windows

Asked

Viewed 202 times

-1

I am using Windows environment

I created a Docker.py where I just put: print ('Hello Docker')

that file is in the folder C: python

I did a test to run this file using Docker and it didn’t work I used the following command:

Docker run -v "C: python:/var/www" -w "/var/www" python Docker.py

it returns the following error: Docker: Error Response from daemon: OCI Runtime create failed: container_linux.go:370: Starting container process caused: exec: "Docker.py": Executable file not found in $PATH: Unknown

Any suggestions?

and in case how would use this same file using dockerfile with windows ??

1 answer

0

To run a simple python script in windows, just create a folder for the project. In this case I created the directory pythondocker.

C:\Users\Naokity\development\pythondocker>

Inside this folder we have to create 2 files (at least) to build the image containing our script.

Follows the folder structure:

.
├── main.py
└── Dockerfile

main py.

print('Hello Docker Naokity')

Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY main.py main.py

CMD [ "python", "./main.py" ]
  • To generate my image we are using as a base image python:3, is what we use the word from we are pointing to our base image.

  • Then we make a WORKDIR to the directory /usr/src/app, the WORKDIR command does operations equivalent to a mkdir and cd.

  • The next command we are making a copy (COPY) of the archive main.py (which is in the root directory we created [C:\Users\Naokity\development\pythondocker\main.py]) and putting in the directory /usr/src/app.

  • The command CMD is what will be executed when starting the container, the command that will be executed is the python main.py

Then to build the image:

 docker build .

By executing this command in the powershell we have the following output:

[+] Building 64.0s (8/8) FINISHED  => [internal] load build definition
from Dockerfile                                                       
0.0s  => => transferring dockerfile: 131B
...
0.0s  => => writing image sha256:57cc79c5fa6f542ff761d95d3d17c498e01760da2b9978650bfaca43e0ce6ba6

After building, you can now use your image, for this just run the following command:

docker run -it <sha da imagem>
# docker run -it 57cc79c5fa6f54 este seria o valor para executar a minha imagem

This number came from the SHA256 generated by the image, notice that your sha256 will be different and you will have to pass this number to use the correct image. The value of sha256 is generated when executing the command docker build .

If everything has gone right in these steps the result of the execution will be:

Hello Docker Naokity
  • 1

    Cool Danizavtz!!!!!!! In my case above I had managed to run only the command name "python" Docker run -v "C: python:/var/www" -w "/var/www" python Docker.py Now complicating a little more how do I do a Pip in the Requirements,txt ? , and if you also have more than one py file (which would be some module)?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.