Pass argument to script in docker run command

Hi! I have managed to create a docker image that works as expected.
The dockerfile looks like this:

# use alpine as base image
FROM alpine as build-env
# install build-base meta package inside build-env container
RUN apk add --no-cache build-base
# change directory to /app
WORKDIR /app
# copy all files from current directory inside the build-env container
COPY . .
# Compile the source code and generate hello binary executable file
RUN gcc -std=c99 myclient.c -lm -o opc_client
# use another container to run the program
FROM alpine
# copy binary executable to new container
COPY --from=build-env /app/opc_client /app/opc_client

WORKDIR /app
# at last run the program
CMD ["/app/opc_client"] 

My challenge is that in “myclient.c” I have specified an IP-address:
UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://192.168.4.100:4840");

I want to send this dockerfile to my team, so they can use the image/container on multiple different PC’s with diferent IP-addresses. Therefore I want to be able to specify an argument in the docker run command.
ie.: sudo docker run -it opc_client “192.168.4.1.00”

I have tried to change the .c file to
UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://argv[0]:4840"); and the dockerfile to:

# use alpine as base image
FROM alpine as build-env
# install build-base meta package inside build-env container
RUN apk add --no-cache build-base
# change directory to /app
WORKDIR /app
# copy all files from current directory inside the build-env container
COPY . .
# Compile the source code and generate hello binary executable file
RUN gcc -std=c99 myclient.c -lm -o opc_client
# use another container to run the program
FROM alpine
# copy binary executable to new container
COPY --from=build-env /app/opc_client /app/opc_client

WORKDIR /app
# at last run the program
ENTRYPOINT ["app/opc_client","param1"]

and run this container with:
docker run image “192.168.4.100”

I was hoping that when I do this we run the container with one argument that is passed to to line where I start the compiled opc_client executable. But I get the error:

[2024-06-14 09:00:26.065 (UTC+0000)] warn/client Could not open a TCP connection to param1

It looks like the argument dont get passed all the way to the .c file, but I’m not sure were it stops.
All kinds of help is appreciated.

Hi

Change:
ENTRYPOINT [“app/opc_client”,“param1”]
to
ENTRYPOINT [“app/opc_client”]

When you run it like:

docker run -it opc_client “192.168.4.1.00”

The IP gets passed as CMD, so in your original entrypoint, that would result in:
app/opc_client param1 “192.168.4.1.00”

So simply remove the ,“param1” from entrypoint and i belive it will work as you intend

1 Like