First, list the containers that have been created on the host
Docker Ps-a
Second, create an interactive container
Command: Docker run-i-T Ubuntu/bin/bash
Where-i-t represents the creation of a container that provides an interactive shell.
Ubuntu is the name of the image, if it does not exist locally, go back to the repository to download.
/bin/bash is the command that executes immediately after the specified container is created.
Note: Each container has a unique ID as the container's identity. Each container also has a unique name, which can be specified by the--name name when created with the Docker Run command, and if not specified, the system automatically generates a name.
such as: Docker run--name mycontainer-i-T Ubuntu/bin/bash
For an interactive container, when you exit the shell, the container closes. The container can be restarted later by command.
Iii. Start/Stop container
If a container has stopped, you can perform the following Docker start command to restart the container, which can be the ID of the container or the name of the container.
Docker start 3d72d0283dc8
Returns the ID of the container after execution
Note that if the specified container is already in the boot state, the above command simply returns the container ID and does not restart the container.
If you want to restart a container that is already started, you can use the Docker restart command
If you want to stop a running container, you can use the Docker Stop command
Iv. attaching to the interactive container
When you restart the container, the parameters specified when you create the container (Docker run) command are used to run. If the container is created, the shell is specified.
When you restart the container, you can attach to the container with the Docker attach command, and when you execute the Docker attach command (you may need to hit the ENTER key), go back to the container's bash prompt,
This is already the equivalent of a shell operation inside the container. If the shell is exited during the operation. The container will also stop with it.
So this kind of container is usually done for a specific task and is not suitable for running a service program.
V. Create a Guardian Container
This container refers to containers that can run for long periods without interactive sessions, and are ideal for running background applications and services (such as database services, Web servers, and so on) in containers.
Example:
Docker run--name mydaemon-d ubuntu/bin/sh-c "while True;do echo Hello world;sleep 1;done"
The above statement creates a daemon container with the-D identity, which launches a shell that loops through a message to ensure that the shell does not exit.
Container logs can be obtained via the Docker logs command
You can also view the currently running process information within the container through the Docker top command.
Vi. interacting with a guardian container
A new process can be started inside the container with the Docker EXEC command.
As in the host, execute the statement Docker exec-t-I Mydaemon/bin/bash
A shell session (inside the container, not the host) will appear so that you can interact with the container and do what you want.
Vii. removing containers
Command: Docker RM id/name
Note that the containers that are running cannot be deleted.
Docker Learning Note 2: Container operations