Home >Operation and Maintenance >Linux Operation and Maintenance >How to create, list and delete Docker containers on Linux

How to create, list and delete Docker containers on Linux

不言
不言Original
2019-03-25 17:36:373719browse

This article introduces the content of creating, listing and deleting docker containers on a Linux machine. Let’s look at the specific content below.

How to create, list and delete Docker containers on Linux

1. Start the Docker container

Use the following command to start a new Docker container. This will start a new container and give you access to the container using the /bin/bash shell.

# docker run [OPTIONS] <IMAGE NAME> [COMMAND] [ARG...]

For example, the following command will create a new docker container using an image named "ubuntu". To list all available images, use the docker images command.

# docker run -i -t ubuntu /bin/bash

To exit the Docker container, press ctrl p q. This will cause the container to run in the background and provide a console to the host system. If you use the exit command, it will stop the current container.

2. List Docker containers

After the Docker container exists, execute the following command to list all running containers.

# docker ps

CONTAINER ID     IMAGE     COMMAND        CREATED        STATUS        PORTS    NAMES
f2582758af13     ubuntu    "/bin/bash"    2 hours ago    Up 2 hours             first_ubuntu

By default, the above command will only list running containers. To list all containers, including stopped ones, you need to use the following command.

# docker ps -a

CONTAINER ID   IMAGE   COMMAND        CREATED        STATUS        PORTS    NAMES
f2582758af13   ubuntu  "/bin/bash"    2 hours ago    Up 2 hours             first_ubuntu
6b5b5a969241   centos  "/bin/bash"    2 days ago     Exited (0) 24 hours ago   ubuntu-web

3. Start/Stop/Connect Containers

You can use the following commands to start, stop or attach to any container. To start the container, use the following command.

# docker start <CONTAINER ID|NAME>

To stop the container, use the following command.

# docker stop <CONTAINER ID|NAME>

To attach to a currently running container, use the following command.

# docker attach <CONTAINER ID|NAME>

4. Discard the Docker container

Before deleting any container, make sure you have stopped the container. You can use the 'docker ps -a' command to list the status of the container. If the container is still running, first stop it using the given command in the above step.

Now use the following command to delete single or multiple containers.

# docker rm <CONTAINER ID|NAME> <CONTAINER ID|NAME>

You can also use the following command to delete all stopped containers at once.

# docker rm $(docker ps -a -q)

This article has ended here. For more other exciting content, you can pay attention to the Linux Video Tutorial column on the PHP Chinese website! ! !

The above is the detailed content of How to create, list and delete Docker containers on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn