Im new to docker.
I am starting the run command with a script called r, which has the following code
proxy="--build-arg http_proxy=http://wwwcache.open.ac.uk:80 --build-arg https_proxy=http://wwwcache.open.ac.uk:80"
if [ "$http_proxy" == "" ]; then
proxy=
fi
docker build $proxy -t bi-tbcnn docker
docker run -v $(pwd):/e -w /e --entrypoint bash --rm -it bi-tbcnn -c ./run
When I execute r I am getting the following error
bash: ./run: No such file or directory
but when I directly execute the ./run command on my terminal is ok
I use Docker Toolbox on windows
The project address is https://github.com/bdqnghi/bi-tbcnn
thanks
This is a known issue on docker for windows
https://blogs.msdn.microsoft.com/stevelasker/2016/09/22/running-scripts-in-a-docker-container-from-windows-cr-or-crlf/
it seems you're facing an issue with Carriage Return(CR) and Line Feeds(LF) characters, maybe your code editor is changing the newline format automatically
can you to try open a bash session on the container and execute the script manually?
docker run -v $(pwd):/e -w /e --entrypoint bash --rm -it bi-tbcnn
root#a83fcd779f8e:/e# ./run
Please paste the output here
Related
I need to have bash shell commands run through python in order to be universal with pc and mac/linux. ./bin/production doesn't work in powershell and putting 'bash' in front would give an error that it doesn't recognize 'docker' command
./bin/production contents:
#!/bin/bash
docker run --rm -it \
--volume ${PWD}/prime:/app \
$(docker build -q docker/prime) \
npm run build
This is the python script:
import subprocess
from python_on_whales import docker
cmd = docker.run('docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
print(out)
This is the error I get when running the python script:
python_on_whales.exceptions.NoSuchImage: The docker command executed was C:\Program Files\Docker\Docker\resources\bin\docker.EXE image inspect docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build.
It returned with code 1
The content of stdout is '[]
'
The content of stderr is 'Error response from daemon: no such image: docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build: invalid reference format: repository name must be lowercase
'
Running the command, docker run --rm -it--volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build in one long line in powershell works but we want a universal standard command for both pc and mac/linux
The Python on Whales docker.run() function doesn't take a docker run ... command line. It is a native Python API where you need to express the various Docker options as function parameters.
In principle you could rewrite this Python script using that API:
from pathlib import Path
from python_on_whales import docker
# build the image, returns an Image object
image = docker.build(Path.cwd() / 'docker' / 'prime')
# start the container; like `docker run ...`
docker.run(image,
command=['npm', 'run', 'build'],
volumes=[(Path.cwd() / 'prime', '/app')], # -v $(PWD)/prime:/app
interactive=True, # -i (required?)
tty=True, # -t (required?)
remove=True) # --rm
The return value from docker.run() (without detach=True) is the container's stdout, and the examples print() that data.
This might not be what you're looking for but you can always try this:
import platform
import subprocess
import os
cur_os = platform.system()
if cur_os == "Windows":
print("You are on windows")
os.system('Command here') # for windows
elif cur_os == "Darwin":
print("You are on mac")
subprocess.call('Command goes here') # for mac
Edit:
I'm intermediate with python so don't judge, if I did something wrong please give me feedback. Thanks.
when I run "docker exec -it docker-name bash" on centOS7 service ,it will go into docker container and can run " python xx.py config.yaml " to execute some works .
but if I use Jenkins shell run "docker exec -it docker-name bash" ,it will have no response ,I write "python xx.py config.yaml " behind ,Jenkins show [ python: can't open file 'xxx.py': [Errno 2] No such file or directory ] ,I think this error is not into the docker container ,so can't find the python file that in the docker container .How can I enter the docker container with Jenkins shell .
When you run docker exec -it docker-name bash, you get an interactive shell inside the container that gets connected to your console and the next command you type to the console is executed in that shell.
But Jenkins has no console. It is executing a script, with the standard input connected to a null device (which always returns end of file on read). So in effect it is executing the equivalent of
docker exec -it docker-name bash </dev/null (the /dev/null is the null device and < connects it to standard input of the command). And if you do that on your console, nothing happens and you'll get your original prompt again.
But you don't have to, and shouldn't be, running bash in this case at all. You give docker exec the command you want to run in the container and it runs it there. So you just do
docker exec -i docker-name python xx.py config.yaml
and that runs the python command, prints any output and when the command ends, disconnects from the container again.
I've omitted the -t because that instructs docker to use the terminal (console), but Jenkins does not have any console, just the -i, instructing it to connect the stdin, stdout and stderr, is good enough.
Now there is also a way to send the commands on the standard input of the bash similar to what the console would do, but I strongly recommend reading the documentation of bash before attempting that.
I am having an odd problem when running a python script in a docker container.
When I start the script in the same line as I am starting the docker containter e.g.
docker run -it --rm <containter>:<version> /bin/bash --login -c "python /opt/project/main.py"
It raises an ImportError for a module.
However when I first start the docker conainer and afterwards start the script afterwards
docker run -it --rm <containter>:<version> /bin/bash
python /opt/project/main.py
everything behaves as it should. So only when i start the script in the same line, the issue occurs.
Hope you can give me a heads-up. Thanks!
I did find a solution which I am happy to share with a random googlers:
The issue I had was that the python dependencies I used were source build catkin dependencies. Thus the setup.bash file from the catkin workspace needed to be sourced in order for the libraries to be found. Since the .bashrc is not sourced when starting the docker like I mentioned, it as to be done manually:
docker run -it --rm <containter>:<version> /bin/bash --login -c "source /path/to/setup.bash && python /opt/project/main.py"
I am trying to set/override the docker entrypoint when I am launching my docker image but I am getting an unexpected behavior
Scenario 1 (Not working)
Docker Run Command --> docker run --rm -it --privileged --net=host
python3.6 -m CameraServerBasler.pylon_video &
python3.6 -m CameraServerBasler.server
Output --> /usr/bin/python3.6: Error while finding module
specification for 'CameraServerBasler.server' (ModuleNotFoundError:
No module named 'CameraServerBasler')
Scenario 2 (Working)
If I execute the same command inside the docker image bash everything is working as expected
Docker Run Command --> docker run --rm -it --privileged --net=host --entrypoint=/bin/bash
Command executed inside the docker image --> python3.6 -m
CameraServerBasler.server & python3.6 -m
CameraServerBasler.pylon_video
Output --> both servers(modules) up and running as expected (server
and pylon_video)
Docker image --> ubuntu:18.04
Am I missing something in the docker run command?
I have been able to resolve this issue using a sh script, but still not very clear why is not working using the Approach #1
Approach #1 Not Working
Entry point declared in the docker file
ENTRYPOINT ["python3.6", "-m", "CameraServerBasler.server", "&", "python3.6", "-m", "CameraServerBasler.pylon_video"]
Approach #2 Working
Entry point declared in the docker file
ENTRYPOINT ["sh", "init.sh"]
init.sh File Content
python3.6 -m CameraServerBasler.server & python3.6 -m CameraServerBasler.pylon_video
Let me clarify what I want to do.
I have a python script in my local machine that performs a lot of stuff and in certain point it have to call another python script that must be executed into a docker container. Such script have some input arguments and it returns some results.
So i want to figure out how to do that.
Example:
def function()
do stuff
.
.
.
do more stuff
''' call another local script that must be executed into a docker'''
result = execute_python_script_into_a_docker(python script arguments)
The docker has been launched in a terminal as:
docker run -it -p 8888:8888 my_docker
You can add your file inside docker container thanks to -v option.
docker run -it -v myFile.py:/myFile.py -p 8888:8888 my_docker
And execute your python inside your docker with :
py /myFile.py
or with the host:
docker run -it -v myFile.py:/myFile.py -p 8888:8888 my_docker py /myFile.py
And even if your docker is already running
docker exec -ti docker_name py /myFile.py
docker_name is available after a docker ps command.
Or you can specify name in the run command like:
docker run -it --name docker_name -v myFile.py:/myFile.py -p 8888:8888 my_docker
It's like:
-v absoluteHostPath:absoluteRemotePath
You can specify folder too in the same way:
-v myFolder:/customPath/myFolder
More details at docker documentation.
You can use docker's python SDK library. First you need to move your script there, I recommend you do it when you create the container or when you start it as Callmemath mentioned:
docker run -it -v myFile.py:/myFile.py -p 8888:8888 my_docker
Then to run the script using the library:
...
client = docker.client.from_env()
container = client.containers.get(CONTAINER_ID)
exit_code, output = container.exec_run("python your_script.py script_args")
...
you have to use docker exec -it image_name python /filename
Note: To use 'docker exec' you must run the container using docker run