Detailed explanation of how to enter and exit the Docker container

Detailed explanation of how to enter and exit the Docker container

1 Start the Docker service

First you need to know how to start the docker service:

service docker start

or:

systemctl start docker

2. Shut down the docker service

To shut down the docker service:

service docker stop

or:

systemctl stop docker

3 Start a container of a docker image

The Docker image is called an image, and the container is called a container.

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

For example, there is an image named ubuntu. Now let's start the container of this image and enter the bash command line of this container:

docker run -t -i ubuntu /bin/bash

The official website says:

  • 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.

It's simple to understand:

  • docker run: start the container
  • ubuntu: the image you want to start
  • -t: enter terminal
  • -i: Get an interactive connection by getting input from the container
  • /bin/bash: Start a bash shell in the container

This will take you inside the container:

root@af8bae53bdd3:/#

If you have a running container, you can run it in the external operating system where the container is located:

docker ps

Check out this container.

If you want to see all containers, including running ones and non-running or dormant images, run:

docker ps -a

If you want to exit:

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 Entering the container

4.1 Use the "docker attach" command to enter

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

docker attach goofy_almeida

That's it.

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

There is a disadvantage of using the "docker attach" command to enter the container, that is, every time you exit from the container to the foreground, the container also exits.

To exit the container while leaving it running in the background, use the "docker exec -it" command. Each time you use this command to enter the container, after you exit the container, the container will still run in the background. The command usage is as follows:

docker exec -it goofy_almeida /bin/bash
  • goofy_almeida: The name of the container to start
  • /bin/bash: Start a bash shell in the container

When you exit the container by typing "exit" or pressing "Ctrl + C", the container will still run in the background, through:

docker ps

You can find it.

5. Exit the container

enter:

exit

Or press the key:

Ctrl + D

This is the end of this article on how to enter and exit the docker container. For more information about how to enter and exit the docker container, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to create, start, and stop a Docker container
  • Restart the Docker service to apply the automatic start and stop command (recommended)
  • How to keep running after exiting Docker container

<<:  Improving the effect of hyperlinks in web design and production

>>:  CSS to implement sprites and font icons

Recommend

Tutorial on installing PHP on centos via yum

First, let me introduce how to install PHP on Cen...

Deploy Confluence with Docker

1. Environmental requirements 1. Docker 17 and ab...

Detailed explanation of adding click event in echarts tooltip in Vue

Table of contents need Workaround 1. Set tooltip ...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

Specific usage of fullpage.js full screen scrolling

1.fullpage.js Download address https://github.com...

Detailed explanation of Nodejs array queue and forEach application

This article mainly records the problems and solu...

Tutorial on installing Elasticsearch 7.6.2 in Docker

Install Docker You have to install Docker, no fur...

JS ES6 asynchronous solution

Table of contents Initially using the callback fu...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Javascript front-end optimization code

Table of contents Optimization of if judgment 1. ...

Linux Basic Tutorial: Special Permissions SUID, SGID and SBIT

Preface For file or directory permissions in Linu...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...