简体   繁体   中英

How can I run two commands in CMD or ENTRYPOINT in Dockerfile

In the Dockerfile builder , ENTRYPOINT and CMD run in one time by using /bin/sh -c in back.

Are there any simple solution to run two command inside without extra script

In my case, I want to setup docker in docker in jenkins slave node, so I pass the docker.sock into container, and I want to change the permission to be executed by normal user, so it shall be done before sshd command.

The normal is like jenkins , which will be login into container via ssh command.

$ docker run -d -v /var/run/docker.sock:/docker.sock larrycai/jenkins-slave

In larrycai/jenkins-slave Dockerfile, I hope to run

CMD chmod o+rw /docker.sock && /usr/sbin/sshd -D

Currently jenkins is given sudo permission, see larrycai/jenkins-slave

I run docker in docker in jenkins slave:

First: my slave know run docker.

Second: I prepare one docker image who knows run docker in docker. See one fragment of dockerfile

RUN echo 'deb [trusted=yes] http://myrepo:3142/get.docker.io/ubuntu docker main' > /etc/apt/sources.list.d/docker.list
RUN apt-get update -qq
RUN apt-get install -qqy iptables ca-certificates lxc apt-transport-https lxc-docker
ADD src/wrapdocker /usr/local/bin/wrapdocker
RUN chmod +x /usr/local/bin/wrapdocker
VOLUME /var/lib/docker

Third: The jenkins job running in this slave contain one .sh file with a set of command to run over app code like:

export RAILS_ENV=test
# Bundle install
bundle install

# spec_no_rails
bundle exec rspec spec_no_rails -I spec_no_rails
bundle exec rake db:migrate:reset
bundle exec rake db:test:prepare
etc...

Fourth: one run shell step job with something like this

docker run --privileged -v /etc/localtime:/etc/localtime:ro -v `pwd`:/code myimagewhorundockerindocker /bin/bash -xec 'cd /code && ./myfile.sh'

--privileged necessary for run docker in docker

-v /etc/localtime:/etc/localtime:ro for synchronize host clock vs container clock

-v pwd :/code for share jenkins workspace (app-code) previously cloned from VCS with /code inside container

note: If you have service dependencies you can use fig with similar strategy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM