Explain how to enter and exit the docker container in detail

  • 2021-10-11 20:03:57
  • OfStack

1 Start the docker service

First of all, you need to know that starting docker service is:


service docker start

Or:


systemctl start docker

2 Shutdown the docker service

Shutting down the docker service is:


service docker stop

Or:


systemctl stop docker

3 Start docker container (container) of an image (mirror)

The mirror image of Docker is called image, and the container is called container.

For Docker, image is static, similar to an operating system snapshot, while container is dynamic, a running instance of image.

For example, if there is an image named ubuntu, let's start the container of the image and go to the bash command line of the container:


docker run -t -i ubuntu /bin/bash

Official website said this:

docker run: runs a container.
ubuntu: is the image you would like to run. -t: flag assigns a pseudo-tty or terminal inside the new container. -i: flag allows you to make an interactive connection by grabbing the standard in (STDIN) of the container. /bin/bash: launches a Bash shell inside our container.

The understanding is simple:

docker run: Start container ubuntu: The image you want to start -t: Entry terminal -i: Get an interactive connection by getting the input of container /bin/bash: Start 1 bash shell in container

So you enter the interior of container:


root@af8bae53bdd3:/#

If you have an container running, you can run it on the same external operating system as the container:


docker ps

See this container.

If you want to see all container, both running and non-running or sleeping images, run:


docker ps -a

If you want to quit:

Ctrl-D

Or:


root@af8bae53bdd3:/# exit

If you want to open this container again, run:


docker start goofy_almeida

Where "goofy_almeida" is the name of the container.

4 Enter container (container)

4.1 Use the "docker attach" command to enter

At this time, container runs in the background. If you want to enter its terminal, then:


systemctl start docker
0

Just do it.

4.2 Use the "docker exec-it" command to enter

One disadvantage of using the "docker attach" command to enter the container (container) is that container exits each time it exits from container to the foreground.

To exit container with container still running in the background, use the "docker exec-it" command. Every time you use this command to enter container, when you exit container, container still runs in the background. The command is used as follows:


systemctl start docker
1 goofy_almeida: The name of the container to start /bin/bash: Start 1 bash shell in container

When you enter "exit" or press "Ctrl + C" to exit container, the container is still running in the background by:


systemctl start docker
2

You can find it.

5 Exit container

Enter:


exit

Or press the button:


systemctl start docker
4

Related articles: