How to pass arguments to Shell Script through docker run
Use the same file.sh
#!/bin/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 --rm test /file.sh abc
docker run -ti --rm test /file.sh xyz
How do you pass arguments to docker run entrypoint?
Whatever is after the image name replaces any defined CMD and is sent as parameter to the entrypoint.
So if you have an entrypoint defined, that you want to pass 'dotnet test' to, you'd do
docker run api-tests dotnet test
An example could be the alpine/curl image, that runs curl with the arguments you pass.
docker run --rm alpine/curl -s https://www.google.com/
will fetch the front page of Google. The parameters are just -s https://www.google.com/
. The image has curl
as the entrypoint, so you don't need to specify that.
Append argument to ENTRYPOINT in Docker run where some args are already defined
The main container command is made up of two parts. The string you pass after the docker run image-name
replaces the Dockerfile CMD
, and it's appended to the Dockerfile ENTRYPOINT
.
For your docker run
command to work, you need to provide the command you want to run as ENTRYPOINT
and its arguments as CMD
. You do not need an environment variable here. However, it is important that both parts use JSON-array syntax and that neither invokes a shell. If ENTRYPOINT
includes a shell then things get syntactically complex (see @KamilCuk's answer); if CMD
includes a shell then it won't get invoked but the command will be invoked with /bin/sh
and -c
as parameters instead.
FROM debian:latest
COPY myscript.sh /usr/local/bin/myscript # preserves execute permissions
ENTRYPOINT ["myscript"] # in a $PATH directory
CMD ["my", "arg", "in", "Dockerfile"]
docker run --rm the-image
docker run --rm the-image my arg from command line
If you want the initial set of command-line arguments to be included and the docker run
arguments to be appended, you can move them into the ENTRYPOINT
line; note that the docker run --entrypoint
is syntactically awkward if you ever do decide you need to remove some of the options.
ENTRYPOINT ["myscript", "--first-default", "--second-default"]
# CMD []
docker run --rm the-image
docker run --rm the-image --user-option
docker run --entrypoint myscript the-image --first-default --no-second-default
If you can update your application to accept options as environment variables in addition to command-line settings, this makes all of this syntactically easier.
ENV FIRST_DEFAULT=yes
ENV SECOND_DEFAULT=yes
CMD ["myscript"]
docker run --rm the-image
docker run --rm -e USER_OPTION=yes the-image
docker run --rm -e SECOND_DEFAULT=no the-image
How to pass command line argument to python script from docker run command?
When you docker run
the container, you can supply a command at the end of it. This replaces the Dockerfile's CMD
, so you need to repeat the script name, but any arguments there will be passed through as-is.
If the script begins with #!/usr/bin/env python
as its very very first line, and it's executable chmod +x app.py
, then you don't need to repeat the python
interpreter; these examples will assume you've done that.
docker run --rm your-image \
./app.py first-argument
It's also possible to use a Dockerfile ENTRYPOINT
here. This isn't my preferred use of ENTRYPOINT
-- it makes some kinds of debugging a little bit harder, and there is a different pattern for it I prefer -- but it is common enough to be included in the Docker documentation. The basic setup here is that the CMD
is passed as arguments to the ENTRYPOINT
so you can set the ENTRYPOINT
to the actual command and the CMD
to its arguments.
FROM python:3.10
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ ./
ENTRYPOINT ["./app.py"] # must be JSON-array syntax
CMD [] # must be JSON-array syntax, could include defaults
In this setup both the ENTRYPOINT
and CMD
must use JSON-array "exec" syntax (shell-form ENTRYPOINT
won't allow CMD
arguments; shell-form CMD
makes sh -c
visible as arguments to your program) but that means you can't use shell features like environment-variable expansion as part of the command (the program can still use os.environ
as normal).
The docker run
option only overrides the CMD
, and so only supplies the program's arguments
docker run --rm your-image \
first-argument
But some kinds of routine debugging need an awkward docker run --entrypoint
option to not run the script.
# run an interactive shell instead of the program
docker run --rm -it --entrypoint bash your-image
# without ENTRYPOINT: `docker run --rm -it your-image bash`
# double-check the file listing and permissions
docker run -it --entrypoint ls your-image -l /app
# without ENTRYPOINT: `docker run --rm your-image ls -l /app
How do I pass arguments to docker run in a CLI (Command Line Interface)?
When you use the feature to deploy container directly on Compute Engine, you are limited to the definition of
- Entry point
- Args to pass at the entry point
- Environment variables
That's all, you can't add additional/custom params.
One solution is, instead of using the built in feature, to use the container-optimized OS (COS) on your Compute Engine and to create a startup script to download and run the container with the docker args that you want
METADATA=http://metadata.google.internal/computeMetadata/v1
SVC_ACCT=$METADATA/instance/service-accounts/default
ACCESS_TOKEN=$(curl -H 'Metadata-Flavor: Google' $SVC_ACCT/token | cut -d'"' -f 4)
docker login -u oauth2accesstoken -p $ACCESS_TOKEN https://gcr.io
docker run … gcr.io/your-project/your-image
On the latest line, you can customize the run param in your startup script.
So now, for the update, you have to update the startup script and to reset your VM (or to create a new Compute Engine with COS and the new startup script; and to delete the previous one).
It's matter of tradeoff between the convenience of a built in feature and the customization capacity.
command line arguments to docker run
use the -c option to bash to give it a command line as a string:
sudo docker run -i -t crystal/mono-base bash -c "/usr/local/bin/mono /home/crystal/Downloads/BackgroundProcesser.exe & /bin/bash"
and put something after the backgrounded command to the container doesn't immediately exit
Printing command line args (java program) when using docker run
Build command to be used is
docker build -t my-app:1.0 .
Docker run command with args is
docker run my-app:1.0 "Hello" "World"
Command Line Arguments for Container in Kubernetes
Try:
apiVersion: apps/v1
kind: Deployment
metadata:
name: transfer-sh
namespace: transfer-sh
labels:
app: transfer-sh
spec:
replicas: 1
selector:
matchLabels:
app: transfer-sh
template:
metadata:
labels:
app: transfer-sh
spec:
containers:
- name: transfer-sh
image: dutchcoders/transfer.sh:latest
args: # <-- in this case each arg is individual
- --provider
- local
- --basedir
- /tmp
ports:
- containerPort: 8080
NAME READY UP-TO-DATE AVAILABLE AGE
transfer-sh 1/1 1 1 91s
Related Topics
How to Add Export Statement in a Bash_Profile File
How to Access Environment Variables Inside .Gdbinit and Inside Gdb Itself
How to You Configure The Command Prompt in Linux to Show Current Directory
Create a Hard Link from a File Handle on Unix
Checking If a Binary Compiled with "-Static"
How to Find Files Except Given Name
Difference Between Arm-None-Eabi and Arm-Linux-Gnueabi
How to Use Systemd to Restart a Service When Down
Where to Put Svn Repository Directory in Linux
Installing Gcc from Source on Alpine
Can't Remove, Purge, Unistall Mongodb from Debian
Linux Service Can't Load Library Path in The /Etc/Ld.So.Conf.D
Under What Circumstances Does The Read() Syscall Return 0
"Must Be Connected to a Terminal Error" with Screen -X Command on a Linux Container
Apache Cgi in User Directory "End of Script Output Before Headers"
Why Does The Call Latency on Clock_Gettime(Clock_Realtime, ..) Vary So Much