The Docker base command USES the detailed of recommendation

  • 2020-06-07 05:48:19
  • OfStack

Since services need to be containerized at work, I recently started learning about Docker. The common commands for getting started with Docker are summarized as follows:

Run the container


$ sudo docker run -i -t ubuntu /bin/bash

The -ES8en flag ensures that STDIN in the container is open
The -ES11en flag tells Docker to assign a dummy tty terminal to the container to be created
ubuntu represents the image we used to create the container
/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 gives the container a name, which is more convenient to use than ID.

3. Restart the stopped container


# Using a container ID Start the container 
$ sudo docker start f5a9f05f4214
# Start the container with the container name 
$ sudo docker start my_container
$ sudo docker restart my_container

In addition to container ID, we can use the container name to run the container, or we can restart a container with the 'docker restart' command to run the above command and use 'sudo docker ps' to see that our container is already running.

4. Attach to container


$ sudo docker attach my_container

When the Docker container is restarted, it will run with the parameters specified in the 'docker run' command, so our container will run an interactive shell after restarting and can be reattached to the container's session with the 'docker attach' command.
You may need to press enter to enter the session after running the command, and if you exit shell of the container, the container will stop running again.

Create a daemon container


$ sudo docker run --name my_container -d ubuntu /bin/bash

The -ES60en flag Docker puts the container in the background to run


`docker exec` Commands start additional new processes inside the container, and there are two types of processes that can be run inside the container: background tasks and interactive tasks. 
# Run the background task in the container 
$ sudo docker exec -d my_container touch /etc/new_config_file
# Run the interactive task inside the container 
$ sudo docker exec -t -i my_container /bin/bash

6. Stop the guardian container


# Stop a running container by its container name 
$ sudo docker stop my_container
# By the container ID Stop the running container 
$ sudo docker stop f5a9f05f4214
# Stop container process 
$ sudo docker kill f5a9f05f4214

If you want to stop a container quickly, use the 'docker kill' command to send a stop signal to the container.

7. Automatically restart the container


$ sudo docker run --restart=always --name my_container -d ubuntu /bin/bash
--restart  The flag checks the exit code of the container to determine whether or not to restart the container. By default, it does not restart. 
--restart Parameter description of 
always : Whatever the exit code for the container is, Docker Will automatically restart the container. 
on-failure : Only if the container's exit code is not 0 Value will be automatically restarted. In addition, this parameter also accepts 1 An optional restart number parameter, `--restart=on-fialure:5` Means when the container exits the code is not 0 When, Docker Attempts to automatically restart the container, at most 5 Times. 

8. Delete the container


# Remove the container based on the container identity 
$ sudo docker rm my_container 
$ sudo docker rm f5a9f05f4214

If containers are no longer in use, they can be removed using the 'docker rm' command, or by passing the -ES82en flag to 'docker rm' to remove a running Docker container (Docker 1.6.2+).


# Delete all containers 
$ sudo docker rm `docker ps -a -q`

The 'docker ps' command lists all existing running container information
The -a flag lists all containers, both running and stopped
The -ES93en flag indicates that only the ID of the container is returned and no other information about the container is returned

9. View container information


$ sudo docker run --name my_container -i -t ubuntu /bin/bash
0

Related articles: