-1

I am setting up a Python application to run a docker container (sample code below). I have a test.csv file, and I understand I need to copy it along with the source code, as my Python script won't be able to read the file outside the container. Or is there a way to mount my local drive/folder to the container, without having to copy the data?

I am building the image via => docker build -t my-app:1.0. Do I need to create immutable tags? If so can anyone point me to where I can find how to create it?

Once I build the container, how can I copy this container say to a dev machine? Do I need to push it to say Docker hub, and then pull this onto a different machine or can I simply copy this image onto a different machine manually?


FROM ubuntu
FROM python:2.7

# Setting Home Directory for containers
WORKDIR /app

# Installing python dependencies
COPY main.py /app/
COPY test.csv test.csv /app/
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

# Run the application
CMD ["python", "main.py"]
opeonikute
  • 494
  • 1
  • 4
  • 15
haju
  • 95
  • 6
  • Look into `docker export` and the corresponding `docker import` which you can use to get a docker container from machine (or system) to another via an intermediate tarfile. – Dirk Eddelbuettel Aug 31 '23 at 02:05
  • A normal sequence would in fact be to push the image to a registry, but also see [How to copy Docker images from one host to another without using a repository](https://stackoverflow.com/questions/23935141/how-to-copy-docker-images-from-one-host-to-another-without-using-a-repository). Note that this wouldn't let a developer edit the code and the volume mechanism can be tricky, and it might be easiest to just have them `git clone` a repository containing the Python script and run it in a virtual environment. – David Maze Aug 31 '23 at 10:43

1 Answers1

1

Multiple questions to answer:

  1. You can mount a host folder to the container using bind mounts or volumes. But in your case it would be easier to just copy the CSV file, like you are already doing.
  2. Using Docker Hub to push the image and then pull it in a different machine is a valid way of doing it. You can also use the docker export and docker import commands.
# original machine
$ docker export container_id > output_file.tar

# new machine (copy the .tar file first)
$ cat output_file.tar | docker import - new_image_name
# now you can run the imported container using this image
Matias B
  • 302
  • 2
  • 10
  • You pretty much never need `docker export`. Given an image you can `docker run` a new container from it, starting from a known state. – David Maze Aug 31 '23 at 10:43