P
P
pinguine2019-01-13 23:35:14
Python
pinguine, 2019-01-13 23:35:14

How to create a Docker container correctly?

I need to wrap my parser, namely the `avito_parser_cli.py` file from the https://github.com/denis5417/avito_parser repository in 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 use `ENTRYPOINT` instead of `CMD` to accept command line arguments on startup.
I then built the image `sudo docker build -t avito_parser_cli .`
For testing, I went to another folder and ran the image `docker run avito_parser_cli "mtz tractor" -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 `output.csv` file, he did not give any errors, but I did not find the file anywhere. When running the script without Docker, the script successfully created the file and wrote to it.
My questions are:
1. Have I formatted the `Dockerfile` correctly and built the image? Is it customary to create a virtual environment inside the image and install all dependencies in it, or should it be done differently (for example, do not create an environment, but install all dependencies at once)?
2. Where did my `output.csv` go? How to make it so that it is created in the same directory in which the image is launched and is it customary to do so?
3. How do such images usually spread? Is it enough to just leave the `Dockerfile` in the repository with the project?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Surin, 2019-01-14
@jenyaatnow

2. Your file is saved inside the container, not on the host machine. As Aleksey Yarkov said, you need to include volume.
3. Images are usually distributed via Docker Hub. This is an analogue of github for docker images.

M
metajiji, 2019-01-14
@metajiji

The image was collected and yes and no. Firstly, to facilitate the volume, use python: 3-alpine, secondly, do it through the use of an intermediate image, namely 2 times FROM, in the first there will be an assembly of venv, in the second copy of venv and your script. You can leave ENTRYPOINT unchanged and write the name of your program, you can put it in PATH, for example, in /usr/local/bin, but this is not necessary.
But what is mandatory is saving the data that your program generates inside the container to the host disk, not the container, this is done through volume, as written above.
For example, docker run --rm ti -v $(pwd)/data:/data avito_parser_cli "mtz tractor" -t -m 300000 -s 'date' -a The
program inside the container is expected to save the data to the /data folder, and on the host they will be in $(pwd)/data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question