Docker Basic Command usage explained

Source: Internet
Author: User
Tags docker ps docker run

Because the service needs to be containerized at work, you have recently started to learn Docker-related knowledge, and the commands commonly used for Docker are summarized as follows:
1. Running the container

$ sudo docker run-i-T Ubuntu/bin/bash
The-I flag guarantees that the stdin in the container is an on-
T flag that tells Docker to assign a pseudo-TTY terminal to the container to be created
Ubuntu means that we create the container using the mirror
/bin/bash means that when the container is created, Docker executes the/bin/bash command in the container

2. Name the container

$ sudo docker run--name my_container  -i-t Ubuntu/bin/bash
--NAME specifies a name for the container, and it is more convenient to use the specified container name than to use the container ID.

3. Restart the stopped container

#使用容器ID启动容器
$ sudo docker start f5a9f05f4214
#使用容器名称启动容器
$ sudo docker start My_container
$ sudo Docker Restart My_container
In addition to the container ID, we can also use the container name to run the container, or you can use the ' Docker restart ' command to restart a container, run the above command, use ' sudo docker ps ' to see that our container has started to run.

4. Attaching to the container

$ sudo docker attach My_container
When the Docker container restarts, it runs with the parameters set by the ' Docker Run ' command, so our container restarts and runs an interactive shell, which can be re-attached to the container's session with the ' Docker attach ' command.
After you run the command, you can press ENTER to enter the session, and if you exit the container's shell, the container will stop running again.

5. Create a daemon container

$ sudo docker run--name my_container-d Ubuntu/bin/bash
The-D flag Docker will put the container in the background running

the ' Docker exec ' command will launch a new process inside the container, and there are two types of processes that can run within the container: background tasks and interactive tasks.
#在容器中运行后台任务
$ sudo docker exec-d my_container touch/etc/new_config_file
#在容器内运行交互式任务
$ sudo docker exec-t- I My_container/bin/bash

6. Stop the daemon container

#通过容器名称停止正在运行的容器
$ sudo docker stop My_container
#通过容器ID停止正在运行的容器
$ sudo docker stop f5a9f05f4214
# Stop container process
$ sudo docker kill f5a9f05f4214
If you want to quickly stop a container, use the ' Docker Kill ' command to send a stop signal to the container. 

7. Automatic restart of the container

$ sudo docker run--restart=always--name my_container-d Ubuntu/bin/bash
The--restart flag checks the container's exit code and, accordingly, decides whether to restart the container, which by default will not restart.
--restart parameter Description
always: Docker restarts the container automatically regardless of the container's exit code.
on-failure: The container's exit code is automatically restarted only if it has a value other than 0. In addition, this parameter accepts an optional restart number parameter, '--restart=on-fialure:5 ' means that Docker will attempt to automatically restart the container, up to 5 times, when the container exit code is not 0 o'clock.

8. Delete a container

#根据容器标识删除容器
$ sudo docker rm my_container 
$ sudo docker rm f5a9f05f4214
If the containers are no longer in use, you can use the ' Docker RM ' command to remove them, or you can remove the running Docker container (Docker 1.6.2+) by passing the-f flag to ' Docker RM '.
#删除所有容器
$ sudo docker rm ' Docker ps-a-Q '
The ' Docker ps ' command lists all of the existing container information that is running-a
flag that represents all containers listed, including the running and stopped-
Q flags indicating that only the container's ID needs to be returned without returning the container's additional information

9. View container Information

 #查看容器信息 $ sudo docker PS #通过docker inspect to get more detailed container information $ sudo docker inspect My_container 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.