CentOS / sig-cloud-instance-images

CentOS cloud images
781 stars 564 forks source link

who is the pid 1 process in container #165

Open qsyqian opened 4 years ago

qsyqian commented 4 years ago

I use centos and ubuntu to do test, and the docker file is like this:

# cat dockerfile.centos
FROM centos
CMD top
# cat dockerfile.ubuntu
FROM ubuntu
CMD top

and build image for them like this:

# docker build -t test-centos -f ./dockerfile.centos .
Sending build context to Docker daemon  2.182MB
Step 1/2 : FROM centos
 ---> 470671670cac
Step 2/2 : CMD top
 ---> Using cache
 ---> 7ddf3de7d491
Successfully built 7ddf3de7d491
Successfully tagged test-centos:latest

# docker build -t test-ubuntu -f ./dockerfile.ubuntu .
Sending build context to Docker daemon  2.182MB
Step 1/2 : FROM ubuntu
 ---> 72300a873c2c
Step 2/2 : CMD top
 ---> Using cache
 ---> 8b9dd80d98eb
Successfully built 8b9dd80d98eb
Successfully tagged test-ubuntu:latest

then run container use the above images:

# docker run -itd test-centos
762c49a91eb9602b2d523fec9c0b106f44e869fcd4425726f336e7d4d3209cdc

# docker run -itd test-ubuntu
c073e590ec9678230007e7b51e7e790d8a1527bb830c91e74add5b9c920b3840

then i attach in the container and see the process in container:


# docker exec -it 762c49a91eb9602b2d523fec9c0b106f44e869fcd4425726f336e7d4d3209cdc bash
# ps -elf
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 - 12100 poll_s 02:39 pts/0    00:00:00 top
4 S root         6     0  0  80   0 -  3006 do_wai 02:40 pts/1    00:00:00 bash
0 R root        19     6  0  80   0 - 10990 -      02:40 pts/1    00:00:00 ps -elf

# docker exec -it c073e590ec9678230007e7b51e7e790d8a1527bb830c91e74add5b9c920b3840 bash
# ps -elf
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 -  1157 wait   02:39 pts/0    00:00:00 /bin/sh -c top
4 S root         6     1  0  80   0 -  9150 poll_s 02:39 pts/0    00:00:00 top
4 S root         7     0  0  80   0 -  4627 wait   02:40 pts/1    00:00:00 bash
0 R root        16     7  0  80   0 -  8601 -      02:40 pts/1    00:00:00 ps -elf

as you see , in the centos container, the top process which pid is 1; in the ubuntu container, the /bin/sh process which pid is 1, and top is the child process of /bin/sh. anyone who knows the different of ubuntu and centos? and if i want to make /bin/sh process's pid is 1, what i should do?

anujkumar-df commented 4 years ago

/bin/sh -c top pass this while running the first container. Did you already try it?