Docker use tutorial related series catalog
table of Contents
Example 1 -i -t (interactive container)
Example 2 -d -i -t (guardian container)
Example 3 -d -i -t --restart (when the docker restarts, the container starts automatically)
Method 1: docker attach container name or container ID
Method two docker exec -it container name or container ID
Query the internal information of the container
Create a container
docker run
-i Run the container in interactive mode
-t means that the container will enter its command line after it is started. Adding these two parameters will directly enter the container after the container is created. Assign a pseudo terminal
--name Name the container and the name cannot be repeated
-v indicates the directory mapping relationship
-d Create a daemon mode to run the container
-p port mapping
Example 1 -i -t (interactive container)
docker run -i -t --name=mycentos1 centos /bin/bash
Note: The function of /bin/bash is to run bash after loading the container. One process must be kept running in docker, otherwise the entire container will be killed immediately after it is started.
Example 2 -d -i -t (guardian container)
docker run -d -i -t --name=mycentos2 centos /bin/bash
After the daemon container was created successfully, it did not enter the container immediately
Example 3 -d -i -t --restart (when the docker restarts, the container starts automatically)
docker run -d -i -t --name=mycentos3 --restart always centos /bin/bash
After docker restarts, the container starts automatically
Enter the container
Method 1: docker attach container name or container ID
docker attach mycentos2
Note: When using exit in this way, the container also stops.
Method two docker exec -it container name or container ID
docker exec -it mycentos2 /bin/bash
Note: When using exit in this way, the container will not stop.
View the running container
Indicates that the newly created container is already running
docker ps
Exit the current container
exit
View all containers
docker ps -a
Stop the ongoing container
docker stop container name or container ID
docker stop mycentos2
Start the container
docker start container name or container ID
docker start mycentos2
Restart container
docker restart container name or container ID
docker restart mycentos2
Go deep inside the container
docker inspect container name or id
docker inspect mycentos2
Query the internal information of the container
-f or -format view
docker inspect -f='{
{.State.Status}}' mycentos2
docker inspect -format='{
{.State.Status}}' mycentos2
Note: case sensitive
Get the IP address of the container
docker inspect -f='{
{.NetworkSettings.IPAddress}}' mycentos2
Delete container
docker rm container name/container ID
docker rm mycentos2