[Docker] Use as a command line tool by specifying arguments (wrapper script)

Introduction

This time, to provide and use the program I created

--Use docker run every time --Specify arguments on the docker run command line

The program was prepared in the form of. Write that note. This follows the trend these days and seems to be the general usage used in open source provided in this way.

With my own program

$ prog <arg1> <args2>

I prepared it as it is in docker image

$ docker run -t prog_image <arg1> <arg2>

Will be available as.

I was able to refer to the following.

-Run multiple services in a container
From docker-docs-jp: Docs »Run apps in production» Container settings »Run multiple services within a container) -Pass the settings required for Docker container operation at startup
The example is very easy to understand. A polite post with explanations.

I think the above information is enough, but if I dare to give a addictive point, ...

--In ENTRYPOINT instead of CMD: I used to specify the execution contents in CMD in the Dockerfile, but it seems that it does not accept any arguments. It worked when I set it to ETNRYPOINT. --Execution permissions: image When internal permissions seemed to be a problem, I wrote RUN chmod + x in the Dockerfile and it worked. (Is that okay ^^;)

Operation check

Constitution

The realization method is docker build

--Install the executable program prog --Prepare a script to execute prog (it seems to call wrapper script) --Set wrapper script to ENTRYPOINT in dockerfile

is. (I think it's okay to specify the executable program directly, but is there anything better to go through the shell? I don't know that.)

Operation example

I made it for testing.

--Execution program. hello.c A program that displays command line arguments.

hello.c



#include <stdio.h>
#include <stdlib.h>

int main(const int argc, const char**argv)
{
    for(int i=0; i< argc; i++){
        printf("argv[%d]=%s\n", i, argv[i]);
    }
    return EXIT_SUCCESS;
}

--wrapper script: just pass arguments

docker_entrypoint.sh


#!/bin/bash
echo "I am a wrapper script."
hello $@

--Dockerfile: Build, install and specify wrapper script as entrypoint

FROM ubuntu:18.04

RUN apt-get update -y && \
  apt-get install -y -qq --no-install-recommends \
  build-essential \
  gcc

COPY hello.c /tmp/
RUN  cd /tmp && gcc /tmp/hello.c -o hello && mv hello /usr/local/bin/
COPY docker_entrypoint.sh /usr/local/bin

ENTRYPOINT ["docker_entrypoint.sh"]

--Let's move it

> docker build -t test .
...
Successfully tagged test:latest

> docker run --rm -t test Dear my friends.
I am wrapper script.
argv[0]=hello
argv[1]=Dear
argv[2]=my
argv[3]=friends.

Works without scripts

In this case, it worked even if I specified the executable program directly in ENTRYPOINT of Dockerfile.

ENTRYPOINT [ "hello" ]

Summary

--For the time being, I found out how to make an environment that runs on Linux into a program that runs on docker run as it is.

Future tasks.

--I think that managing docker run is the same as managing normal container, but check it. I am worried about specifying resources such as CPU, acquiring usage status, and HEATH CHECK. --Understand why you use Wrapper. I wonder if it is possible to specify a combination of multiple programs to be executed sequentially.

I was able to survive today (to be exact, yesterday). (2020/10/17)

Recommended Posts

[Docker] Use as a command line tool by specifying arguments (wrapper script)
Build by specifying docker as a file
A Git-like self-made command line tool that complements Tab
What are command line arguments?