Two Implementation Methods of docker Container Exiting bash under Linux

  • 2021-09-16 08:34:23
  • OfStack

If you want to exit bash, there are two actions:

Type 1:

Ctrl + d exits and stops the container;

Type 2:

Ctrl + p + q exits and runs the container in the background;

Additional knowledge: Docker starts multiple services at the same time

The previous articles on Docker introduced that only one background service is started when starting the container. Today, let's talk about how to start multiple services through supervisor

1. First, create a directory and create an Dockerfile under the directory. The file content is as follows

FROM centos:centos6MAINTAINER Fanbin Kong "kongxx@hotmail.com"RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmRUN yum install -y openssh-server sudo mysql-server mysql supervisorRUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config RUN useradd adminRUN echo "admin:admin" | chpasswdRUN echo "admin ALL=(ALL) ALL" > > /etc/sudoers RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_keyRUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_keyRUN mkdir /var/run/sshdRUN /etc/init.d/mysqld start & & \ mysql -e "grant all privileges on *.* to 'root'@'%' identified by 'letmein';" & & \ mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by 'letmein';" & & \ mysql -u root -pletmein -e "show databases;"RUN mkdir -p /var/log/supervisorCOPY supervisord.conf /etc/supervisord.confEXPOSE 22 3306CMD ["/usr/bin/supervisord"]

2. Create the supervisord. conf file in the directory where Dockerfile is located, as follows:

[supervisord]nodaemon=true[program:sshd]command=/usr/sbin/sshd -D[program:mysqld]command=/usr/bin/mysqld_safe

3. Run the build command in the directory where Dockerfile is located to generate the image file, using mysql_server as the image file name

sudo docker build -t myserver .

Step 4 Start the container

4.1 Start the container with the following command

sudo docker run --name=myserver -d -P myserver

4.2 After starting the container, you can use "sudo docker ps" to view it. At this time, you can see that the content of PORTS column is

"0.0. 0.0: 49171- > 22/tcp, 0.0.0.0:49172- > 3306/tcp "

Ports 22 and 3306 of the container are mapped to ports 49171 and 49172 of the host machine.

4.3 The ssh and mysql services are now accessible with the following command

ssh admin@ < Host machine > -p < Host machine port > mysql -h < Host machine > -u root -pletmein -P 49172

4.4 Of course, you can also use "sudo docker inspect myserver grep IPAddress" to view the container IP address and then access the ssh and mysql services with the following command

ssh admin@ < Container machine IP > mysql -h < Container machine IP > -u root -pletmein


Related articles: