简体   繁体   English

如何在Dockerfile中的CMD或ENTRYPOINT中运行两个命令

[英]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. Dockerfile构建器中ENTRYPOINTCMD通过使用/bin/sh -c一次运行一次。

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. 在我的情况下,我想在jenkins从节点的docker中设置docker,所以我将docker.sock传递到容器中,并且我想更改要由普通用户执行的权限,因此应在sshd命令之前完成。

The normal is like jenkins , which will be login into container via ssh command. 正常情况类似于jenkins ,它将通过ssh命令登录到容器。

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

In larrycai/jenkins-slave Dockerfile, I hope to run 我希望在larrycai/jenkins-slave Dockerfile中运行

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

Currently jenkins is given sudo permission, see larrycai/jenkins-slave 目前jenkins被授予sudo权限,请参阅larrycai / jenkins-slave

I run docker in docker in jenkins slave: 我在詹金斯奴隶的docker中运行docker:

First: my slave know run docker. 首先:我的奴隶知道运行docker。

Second: I prepare one docker image who knows run docker in docker. 第二:我准备一个知道在docker中运行docker的docker映像。 See one fragment of dockerfile 查看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: 第三:在此从属服务器上运行的jenkins作业包含一个.sh文件,该文件带有一组命令,可以在应用程序代码上运行,例如:

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 -特权对于在docker中运行docker是必需的

-v /etc/localtime:/etc/localtime:ro for synchronize host clock vs container clock -v / etc / localtime:/ etc / localtime:ro用于同步主机时钟与容器时钟

-v pwd :/code for share jenkins workspace (app-code) previously cloned from VCS with /code inside container -v pwd :/以前从VCS使用容器中的/ code克隆的共享jenkins工作区的代码(应用程序代码)

note: If you have service dependencies you can use fig with similar strategy. 注意:如果您具有服务依赖性,则可以将无花果用于类似策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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