Home > OS >  Using docker image inside Dockerfile
Using docker image inside Dockerfile

Time:12-15

I have created a Dockerfile which I am using to build and run an image. Now the image that I am creating using Dockerfile requires another Docker image to execute a python script.

My Dockerfile:

FROM python:3

ADD test.py /test.py
CMD ["python", "test.py"]

To execute test.py, I need to have landoop/fast-data-dev image. Can someone give idea how to achieve this.

CodePudding user response:

Your dockerfile should look like this:

FROM python:3 as test
WORKDIR /app/
ADD test.py /app/

FROM landoop/fast-data-dev
COPY --from=test /app/test.py /app/
WORKDIR /app/
CMD ["python", "test.py"]

CodePudding user response:

I’m having trouble understanding your question. I think you’re asking how to python test.py in landoop/fast-data-dev image. Is that correct? if correct,so:

docker run -it -d -P --rm lensesio/fast-data-dev
docker ps -a | grep "lensesio/fast-data-dev"

my result is:

23536c30d44d   lensesio/fast-data-dev   "/usr/bin/dumb-init …"   About a minute ago   Up About a minute   0.0.0.0:49160->2181/tcp, 0.0.0.0:49159->3030/tcp, 0.0.0.0:49158->3031/tcp, 0.0.0.0:49157->8081/tcp, 0.0.0.0:49156->8082/tcp, 0.0.0.0:49155->8083/tcp, 0.0.0.0:49154->9092/tcp   cool_cannon

in your brower visit 127.0.0.1:49159 [because of 0.0.0.0:49159->3030/tcp] if you want to execute python script. you can do below:

docker cp test.py 23536c30d44d:test.py
docker exec -it 23536c30d44d bash
python3 test.py

CodePudding user response:

If I understand you correctly, you are running a container from an image built from this Dockerfile, and this running container is dependent on a service provided from a different running container?

There are a couple of ways to achieve this:

  • The simplest is just to run this other container in detached mode (with the -d flag), first. Then, when you run the test.py container, it should be able to contact the service it requires. You would need to stop and delete the detached one when you're finished.

  • Another way would be to use docker compose. This allows you to deploy multiple containers in the same command.

  • Related