Docker Community Forums

Share and learn in the Docker community.

How to run bash command after startup?


(Talzac) #1

Hi,
I am using Docker on my Synology NAS.
I am using the TS3 Musicbot in a Docker container but I do not know how to run a Bash command after starting up the container now I have to do this manually.

Here is the command I have to run now:
cd TS3MusicBot
nohup ./TS3MusicBot_runscript.sh -account “E-MAIL”-port 8484 -webif-pw “PASSWRD” -webif-pw-user “PASSWRD” -number 3 -webif-bind-ip 0.0.0.0 &

I have to do this manually in a Bash after container start, how can I make this automatic?

I want Docker to run this after start up and I have no idea how to do this and I can not find any Synology information about this, can some one please explain this for someone totally new to docker…

Thanks!


(Lucas da Silva Simão) #2

Hello talzac;

You can use the dockerfile for this operation;

example:
create new document on you server with name Dockerfile

FROM debian

MAINTAINER Lucas Simão<lucas.simao01@gmail.com>

RUN apt-get update && apt-get install -y nano && apt-get clean

EXPOSE 8484

ENTRYPOINT "put your code here" && /bin/bash
#exemple ENTRYPOINT service nginx start && service ssh start && /bin/bash "use && to separate your code"

after save your code on /root, create a new image docker using

docker build -t lucassimao/teste:0.1 /root/

start new container

docker run -it lucassimao/teste:0.1 /bin/bash

(Talzac) #3

Thanks! But I do not understand this fully… What if I already have an active working container can I implement this on that so I do not have to start over?


(David Maze) #4

You should write up a Dockerfile that recreates everything you’ve done. It’s very normal to restart and recreate containers. It would end with

EXPOSE 8484
WORKDIR TS3MusicBot
CMD "./TS3MusicBot_runscript.sh -account ... -port 8484 -webif-bind-ip 0.0.0.0"

The CMD would not have nohup or end with &: the lifetime of the container is the lifetime of that script, and when the script/server exits, the container exits too.

(In contrast to the previous poster, I pretty strongly believe you should not use ENTRYPOINT for this–it gets in the way of a debugging docker run --rm -it myimage bash too much–and it’s really important to avoid commands like service or systemctl that run init scripts, since you won’t have an init.)

To answer your immediately preceding question, if you have a container already running and need to run some administrative command in it, docker exec will do that, but it’s not intended to be the primary way you get software running in a container.


(Chalithaw) #6

thanks this works. clean and clear answer with all the steps


(Satishy18) #7

ENTRYPOINT systemctl start mysqld && “./tmp/mysql_config.sh”

after running docker-compose container stop with below error .

docker logs b5c0f271b4e8
Failed to get D-Bus connection: Operation not permitted