Detailed explanation of common commands of docker container

Refer to docker rookie tutorial

1. Docker starts and runs a container:
docker run ubuntu:15.10 /bin/echo "Hello world"
Explanation: ubuntu:15.10 image name
          /bin/echo The command
          "Hello world" running in the container is the output of the above command

2. docker starts and runs the interactive container
docker run -i -t ubuntu:15.10 /bin/bash
-t: Specify a pseudo terminal or terminal in the new container.
-i: Allows you to interact with the standard input (STDIN) in the container.
-p: to map the host port to the container port: for example
docker run -it -p 5000:5000 ubuntu:15.10 /bin/bash 

-v: Mount the data volume, mount the host directory to the container directory, so that the code data in the host directory can be synchronized to the container:

例如:
docker run -it -v /home/user/code:/workspace/code ubuntu:15.10 /bin/bash 

note:

(1) Both the host directory and the container directory are absolute paths. Relative paths will not work.

(2) The synchronization here is two-way, that is to say, if the file is modified in the host directory, the file is also modified in the corresponding container directory; the file is modified in the container directory, and the file is also modified in the host directory.

3. docker starts a container in
background mode docker run -d ubuntu:15.10
Note: If the container is not running any programs after starting a container in the background, then the container will be automatically stopped,
so docker ps does not show that the container is running . Therefore, to start the container in background mode, you must run the program.

4. View the container status command
docker ps: View the currently running container
docker ps -a: View all container information, including stopped

5. View the standard output in the
container docker logs container ID
 -f: to output the standard output inside the container.

6. Stop the container
docker stop container ID

7. Start a stopped container:
docker start container ID

8. Restart the container
docker restart <container ID>

9. Enter the container (container has started)
docker attach <container ID>
docker exec <container ID> It is
recommended that you use the docker exec command, because exiting the container terminal (using exit to exit) will not cause the container to stop.
Example: docker exec -it 243c32535da7 /bin/bash

10. Import the container.
You can use docker import to import from the container snapshot file as a mirror. The
following example imports the snapshot file ubuntu.tar to the mirror test/ubuntu:v1:
cat docker/ubuntu.tar | docker import-test/ubuntu:v1

You can also import
docker import http://example.com/exampleimage.tgz example/imagerepo by specifying a URL or a certain directory

11. Export container
If you want to export a local container, you can use the docker export command.
docker export <container ID>> ubuntu.tar
Export container 1e560fca3906 to the local file ubuntu.tar.

12. Delete the container
docker rm -f <container ID>

Guess you like

Origin blog.csdn.net/Thanours/article/details/108924274