How to let docker run as daemon /bin/bash

  docker, question

Please ask the experts a question about docker. I want to run /bin/bash with the -d parameter when docker run, and then let this bash run all the time. when I want to use it, docker attach it. But as soon as I ran Docker Run-d …. This is why, there is no solution. . .

I have encountered this problem myself. However, in different times, I hope that when docker run, it can keep the container running in the background and allow me to log in and operate remotely.

I copied the article:Ssh Remote Login to a Container

-Content Begins-

After starting a container, we may need to log in with ssh for some operations. In order to achieve this goal, there are two points to ensure:

  • The container must be running.
  • Openssh-server is in a startup state.
Note: The following example was completed in ubuntu/13.10

First, ensure that the mirror is installed withopenssh-serverService

# The official image does not generally have ssh server installed, so, one needs to be installed
 apt-get install openssh-server
 # Set an initial password
 passwd 123
 ...

Then, commit the mirror:

docker commit CONTAINER_ID NAME/VERSION

Finally, run the mirror and keep the generated container running in the background:

# The first -d means to have the container run in the background.
 The -D at the end of # indicates the daemon mode to start ssh, otherwise the container will become stopped immediately after starting.
 docker run -d NAME/VERSION /etc/init.d/ssh start -D

Now, you can ssh to the container.

# query container IP
 docker inspect CONTAINER_ID | grep IP
 
 # Login
 ssh root@IP
 # Enter password 123 to complete login
 # To ensure security, please use passwd to change the password immediately