在Docker运行时使用ENTRYPOINT和CMD在Centos 7 OS Docker容器中启动sshd

我知道我可以在Docker docker run <debug_container_id>的CentOS 7基本Docker容器中启动sshd CMD ["/usr/sbin/sshd","-D"] ,因为我在别处看到过它

  1. https://stackoverflow.com/a/25449705/1258525
  2. https://engineering.riotgames.com/news/jenkins-ephemeral-docker-tutorial
  3. https://github.com/CentOS/CentOS-Dockerfiles/blob/master/ssh/centos7/Dockerfile

当我运行我的容器时我无法启动sshd,我认为这与我的ENTRYPOINT中的ENTRYPOINTCMD组合有关。

我采取的步骤:

  1. docker run --name="<debug_container_id>" -d <debug_image>
  2. docker exec <debug_container_id> ps aux

在这里输入图像说明

你可以看到我的ENTRYPOINT ,“dotnet TSL.Security.Service.dll”与docker exec <debug_container_id> ps aux的输出中的“/ usr / sbin / sshd -D”

我目前的工作是让sshd运行:

 docker exec -d <debug_container_id> bash -c "/usr/sbin/sshd -D" 

用我的解决方法,我可以进入容器。

所以你可以把ENTRYPOINT和CMD结合起来, 这里虽然文章的作者正在构build一个单一的string命令来执行使用ENTRYPOINT和CMD的组合。 我正在尝试执行两个命令string。

在重读那篇文章之后,我开始意识到,使用ENTRYPOINT和CMD一起被用来构build一个单一的string命令,在docker run用户可以覆盖CMD参数的情况下执行。

在我试图在容器运行中执行两条命令的地方,所以我尝试将我的入口点更改为以下,并完全删除CMD:

 ENTRYPOINT ["dotnet", "TSL.Security.Service.dll", "&&", "/usr/sbin/sshd", "-D"] 

这在相同的结果结束,sshd没有运行:

在这里输入图像说明

当我进一步调查,我想我试图在我的Docker容器中启动两个进程,我将无法做到这一点: Docker多入口点

这是我的debug_image的Dockerfile:

 FROM centos:7 MAINTAINER Brian Ogden #Timezone ENV TZ=America/Los_Angeles RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN yum update -y && \ yum clean all ############################################# # .NET Core SDK ############################################# RUN yum install -y \ libunwind \ libicu RUN curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?linkid=848821 RUN mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet RUN ln -s /opt/dotnet/dotnet /usr/local/bin #speed up dotnet core builds ENV NUGET_XMLDOC_MODE skip ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true ############################################# ############################################# # Setup Container for SSH ############################################# WORKDIR / RUN yum install -y \ unzip \ openssh-server \ curl RUN mkdir -p /var/run/sshd RUN echo 'root:password' | chpasswd # SSH login fix. Otherwise user is kicked off after login #ref https://engineering.riotgames.com/news/jenkins-ephemeral-docker-tutorial RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd # gen dummy keys, centos doesn't autogen them like ubuntu does #ref https://engineering.riotgames.com/news/jenkins-ephemeral-docker-tutorial RUN /usr/bin/ssh-keygen -A #to pass environment variables when running a Dockerized SSHD service. #SSHD scrubs the environment, therefore ENV variables contained in Dockerfile #must be pushed to /etc/profile in order for them to be available. ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" >> /etc/profile ############################################# ############################################# # .NET Sevrice setup ############################################# #install CLRDBG, Microsoft's new cross-platform command line debugger used for debugging code running on .NET Core RUN curl -sSL https://aka.ms/getclrdbgsh | bash /dev/stdin vs2015u2 ~/clrdbg # Copy our code from the "/src/MyWebApi/bin/Debug/netcoreapp1.1/publish" folder to the "/app" folder in our container WORKDIR /app COPY ./src/TSL.Security.Service/bin/Debug/netcoreapp1.1/publish . ARG ASPNETCORE_ENVIRONMENT # Expose port 5000 for the Web API traffic ENV ASPNETCORE_URLS http://+:5000 ENV ASPNETCORE_ENVIRONMENT $ASPNETCORE_ENVIRONMENT EXPOSE 5000 22 ENTRYPOINT ["dotnet", "TSL.Security.Service.dll"] ############################################# #I wish this would start ssh when the container is ran but it doesn't, tried lots to get this to work CMD ["/usr/sbin/sshd","-D"]