Swati
Swati

Reputation: 575

Execute script when docker container start

I want container with "centos:latest" image to be started and should execute my script. The scripts are copied with docker cp commands.

docker create --name centos1 centos:latest
docker cp . 5db38b908880:/opt   ---> scripts are in current directory, hence .
docker commit centos1 new_centos1  --> now new_centos1 image has scripts

Now I want to start new container with the scripts to be executed: I tried below commands:

docker run -ti --rm --entrypoint "cd /opt && deploy_mediainfo_lambda.sh" new_centos1:latest

docker run -ti --rm new_centos1:latest "cd /opt && deploy_mediainfo_lambda.sh"

Both of above commands failed with:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"cd /opt && deploy_mediainfo_lambda.sh\": stat cd /opt && deploy_mediainfo_lambda.sh: no such file or directory": unknown.
ERRO[0000] error waiting for container: context canceled 

if used bash command while starting container, I can run my script using 'execuateble path'/'execuatble name' inside container, but I can not do this while starting container on commandline

docker run -ti --rm new_centos1:latest bash
[root@c34207f3f1c4 /]# ./opt/deploy_mediainfo_lambda.sh 

If used below command, which calls executable directly, it gives path error.

docker run -ti --rm new_centos1:latest "deploy_mediainfo_lambda.sh"

docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"deploy_mediainfo_lambda.sh\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled 

Also not sure about setting $PATH from commandline while starting the container.

I know, using Dockerfile this is achievable, like:

  1. can set path using ENV,
  2. can copy executables with ADD or COPY
  3. run executables using CMD or ENTRYPOINT

How to achieves it using docker commandline?

Upvotes: 2

Views: 6917

Answers (1)

Swati
Swati

Reputation: 575

Thanks melpomene.

Here is my bash script to automate script execution inside container, after copying them, all using docker commands.

 # Start docker container
docker create --name mediainfo_docker centos:latest
# copy script files
docker cp . mediainfo_docker:/opt
# save container with the new image, which contains all scripts.
docker commit mediainfo_docker mediainfo_docker_with_scripts

# Now run scripts inside docker container
docker run -ti --rm  mediainfo_docker_with_scripts:latest /opt/deploy_mediainfo_lambda.sh

Since deploy_mediainfo_lambda.sh is a script, first line of it is: #!/bin/bash

Upvotes: 1

Related Questions