Docker container shell

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Copyright, without permission, is prohibited reprint


chapter


Docker image is a file system, sometimes you need to enter the shell inside the container, such as: view the contents of the configuration file.

Running container, you can docker execenter its shell terminal command:

docker exec -it <container-id> /bin/bash
  • -i Flag tells docker remain open stdin (so you can enter commands).
  • -t Flag assign a pseudo tty.

for example

1. Run nginx Mirror

[root@qikegu docker]# docker run -d nginx

2. Check the container ID nginx


[root@qikegu docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
87c324a31476        nginx               "nginx -g 'daemon of…"   54 seconds ago      Up 53 seconds       80/tcp              trusting_vaughan

3. Go to the container, run a bash shell

[root@qikegu docker]# docker exec -it 87c324a31476 /bin/bash
root@87c324a31476:/#

We can see into the container shell.

4. Run the shell of the container

root@87c324a31476:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@87c324a31476:/# ps
bash: ps: command not found

Command does not exist in the container (above ps) can not be performed

5. Exit shell

Use direct exitcommand to exit the shell, the container itself will also stop running.

Use ctrl + p + q, you can exit shell, the vessel will continue to run.

docker attach, docker exec, docker run 区别

  • docker attach- not to run extra stuff in the container, but to attach to a running process. If the container is running nginx, nginx will attach to the process, get the standard output of nginx.
  • docker exec - dedicated to running the new stuff in the container has been started, either shell or other processes
  • docker run- used to run a new container, the container commands to be run can be specified. If not specified, the default command to run a mirror, for example: When you run nginx container, docker run nginxdo not specify a command, the application runs nginx, docker run -it nginx /bin/bashthat specifies /bin/bash, will run this command.

Guess you like

Origin blog.csdn.net/weixin_43031412/article/details/94598826