python – How to create a Docker container correctly?

Question:

I need to wrap my parser, namely the avito_parser_cli.py file from the https://github.com/denis5417/avito_parser repository into a Docker container.

I created a Dockerfile :

FROM python:3
ADD avito_parser.py avito_parser_cli.py requirements.txt /
RUN python3 -m venv env
CMD ['source', 'env/bin/activate']
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3", "avito_parser_cli.py"]

I create a virtual environment and install the dependencies I need into it. I am using ENTRYPOINT instead of CMD to accept command line arguments on startup.

Then I built the sudo docker build -t avito_parser_cli .

For testing, I went to another folder and ran the docker run avito_parser_cli "трактор мтз" -t -m 300000 -s 'date' -a image docker run avito_parser_cli "трактор мтз" -t -m 300000 -s 'date' -a

All arguments were parsed correctly and the script produced what was expected. But he also had to write the result to the file output.csv , he did not give any errors, but I did not find the file anywhere. When running the script without Docker, the script successfully created a file and wrote to it.

I have the following questions:

  1. Have I Dockerfile and built the image? Is it customary to create a virtual environment inside the image and install all dependencies in it, or do you need to do something differently (for example, do not create an environment, but immediately install all dependencies)?
  2. Where has my output.csv ? How to make it create in the same directory in which the image is launched and is it customary to do so?
  3. How are such images usually distributed? Is it enough to just leave the Dockerfile in the project repository?

Answer:

  1. The virtual environment inside the container is superfluous. The container itself is an isolated virtual environment. If your application is the only thing that runs in a container (and the Docker ideology tends to this option), then all the programs and libraries you need can be installed directly.
  2. Files created by programs running inside the container are stored in the virtual file system of the container and will disappear after it stops. In order to get them out of the container and save, you need to mount the host folder to your container and force the program to write to the mounted folder. It is described in detail in the documentation , examples and nuances for Windows can be found here , briefly: docker run -v /path/on/host/machine:/path/inside/container my-docker-image .
  3. Depends on how much you want to stress the users of your container. If you want to save them from having to download the sources and build the image yourself, you can publish your image to Docker Hub or to some alternative repository (for example, JCenter) so that your users can run the container simply through docker run .
Scroll to Top