可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am new to the docker world. I have to invoke a shell script that takes command line arguments through a docker container. Ex: My shell script looks like:
#!bin/bash echo $1
Dockerfile looks like this:
FROM ubuntu:14.04 COPY ./file.sh / CMD /bin/bash file.sh
I am not sure how to pass the arguments while running the container
回答1:
Update file.sh
#!/usr/bin/env bash echo $1
Build the image using the existing Dockerfile:
docker build -t test .
Run the image with arguments abc
or xyz
or something else.
docker run -ti test /file.sh abc docker run -ti test /file.sh xyz
回答2:
with this script in file.sh
#!/bin/bash echo Your container args are: "$@"
and this Dockerfile
FROM ubuntu:14.04 COPY ./file.sh / ENTRYPOINT ["/file.sh"] CMD []
you should be able to:
% docker build -t test . % docker run test hello world Your container args are: hello world
回答3:
With Docker, the proper way to pass this sort of information is through environment variables.
So with the same Dockerfile, change the script to
#!/bin/bash echo $FOO
After building, use the following docker command:
docker run -e FOO="hello world!" test
回答4:
What I have is a script file that actually runs things. This scrip file might be relatively complicated. Let's call it "run_container". This script takes arguments from the command line:
run_container p1 p2 p3
A simple run_container might be:
#!/bin/bash echo "argc = ${#*}" echo "argv = ${*}"
What I want to do is, after "dockering" this I would like to be able to startup this container with the parameters on the docker command line like this:
docker run image_name p1 p2 p3
and have the run_container script be run with p1 p2 p3 as the parameters.
This is my solution:
Dockerfile:
FROM docker.io/ubuntu ADD run_container / ENTRYPOINT ["/bin/bash", "-c", "/run_container ${*}", "--"]
回答5:
If you want to run it @build time :
CMD /bin/bash /file.sh arg1
if you want to run it @run time :
ENTRYPOINT ["/bin/bash"] CMD ["/file.sh", "arg1"]
Then in the host shell
docker build -t test . docker run -i -t test