WORKDIR instruction in Dockerfile has no effect when containerize a Python app - python

I have the following Dockerfile to containerize a Python app:
FROM python:3.8-slim
WORKDIR /app
COPY ./simple_http_server.py .
CMD [ "python", "./simple_http_server.py" ]
I run with the following command:
docker run -d -p 5000:8000 simple_http_server
It runs fine. docker ps -a shows the container is up for n seconds and container id of xyz. Then I attach to the container to inspect inside the container using the command
docker exec -i -t xyz /bin/bash
Once inside the container, doing a pwd command reveals it's inside the /code directory.
root#3df086098bf2:/code# pwd
/code
In fact, the WORKDIR /app instruction above seems to have no effect at all. I don't see /app anywhere.
What is the explanation of why WORKDIR has no effect and how it can be fixed?

Have you built the image again after update WORKDIR /app, this maybe because you run the old image with WORKDIR /code.
Try:
docker build -t simple_http_server .
Then:
docker run -dp 5000:8000 simple_http_server

Related

How to create a multistage dockerfile for a python app?

Below is the directory structure and the dockerfile for my python application. In order to run the main.py, I need to create a data set by running generate_data.py, which is in the data directory. How can I create a multistage dockerfile in order to first create the data and then run the main.py file? I'm new to using docker and I feel overwhelmed.
FROM python:3.7.2-slim
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD ["python", "./src/main.py"]
You can create a shell script then use that for CMD
start.sh:
#!/bin/bash
python generate_data.py
python ./src/main.py
Dockerfile:
FROM python:3.7.2-slim
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD ["sh", "start.sh"]
A key point of using docker might be to isolate your programs, so at first glance, you might want to move them to separate containers and talk to each other using a shared volume or a docker network, but if you really need them to run in the same container, you can achieve this by using a bash script. and replacing CMD with:
COPY run.sh
RUN chmod a+x run.sh
CMD ["./run.sh"]
You can also include if statements into a bash script and pass arguments to the bash script through docker.

Docker wants a requirement file even though I do not want it to?

I had a dockerfile with this commands in it:
#base image
FROM python:3-onbuild
WORKDIR /app
ADD . /app
#run the application
CMD ["python", "helloeveryone.py"]
.. and then when I tried the build the image;
sudo docker build -t helloeveryone .
I had this problem:
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM python:3-onbuild
# Executing 3 build triggers
COPY failed: stat /var/lib/docker/tmp/docker-
builder656900257/requirements.txt: no such file or directory
Even though I did not specify a requirements.txt in my Dockerfile.
What is the problem?
The problem is you are using a base image that expects and automatically feeds a requirements.txt file to pip.
If you do not want this, you should select a different base image, e.g. change your dockerfile:
FROM python:3 # or python:3-slim, python:3-alpine or other suitable image
WORKDIR /app
ADD . /app
CMD ["python", "helloeveryone.py"]

How do I pass a local file as an argument with docker run?

I have a Dockerfile like this:
FROM python:3.6
RUN mkdir /code
COPY dist/python-0.1.0.tar.gz /code
WORKDIR /code
RUN pip install python-0.1.0.tar.gz
ENTRYPOINT ["post"]
The "post" command runs my code fine with no arguments.
My question is how can I get the docker container to except a local file at runtime because it may change.
Here is my command that runs the script, but says that there is no such file or directory data/output.xml
docker run container data/output.xml
I have also tried this with no luck
docker run -v data:/data containter /data/output.xml
Thank you for any help!
for docker volumes, I think you need to use the absolute address. The usage is:
$ docker run -tid --name <name_you_want> -v <host_absolute_path>:<path_inside_docker> <image_id> <cmd_such_as_bash>

How to pass json file as an argument using docker run command

Below is my Dockerfile content:
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
RUN pip install numpy==1.12.0
CMD ["python", "t_1.py", "t_1.json"]
I want to pass this file(t_1.sjon) as argument with docker run command at runtime so that CMD ["python", "t_1.py", "RUN TIME ARGUMENT"]. I tried mounting volumes but fails as json file is independent and I want as argument.
Please help.
What you should use is ENTRYPOINT
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
RUN pip install numpy==1.12.0
ENTRYPOINT ["python", "t_1.py"]
Now when you run the docker command
docker run -v ./t_1.json:/data/t_1.json <dockerimage> /data/t_1.json
This will make it equivalent to python t_1.py /data/t_1.json
You can use bash to run any command inside docker container.
docker run <your_image> bash -c "python /app/t_1.json"
I am assuming that the json file is in the directory where you are having the dockerfile. So it being copied inside the container at /app, can be run using the bash command inside container.

Passing argument to python application running on docker

I want to pass argument to python file app.py while running its docker image using command :
docker run -d -p 5000:5000 python_app https://github.com/abc/repo
where python_app is image of my application and https://github.com/abc/repo is the argument which I want to pass. But when I run docker image using above command my container exits with error :No such directory or file found
This is my Dockerfile
FROM python:2.7.13
MAINTAINER xyz "abc#hotmail.com"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py","https://github.com/abc/repo"]
I think my param2 in CMD is treated an argument for docker image rather than argument for python application.
td;lr use ENTRYPOINT ["python", "app.py"] then you can pass parameters to the docker run
see answer here: how to pass command line arguments to a python script running in docker td

Categories