Docker common operation container commands

Docker use tutorial related series catalog


table of Contents

Create a container

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)

Enter the container

Method 1: docker attach container name or container ID

Method two docker exec -it container name or container ID

View the running container

View all containers

Stop the ongoing container

Start the container

Restart container

Go deep inside the container

​ Query the internal information of the container

Delete 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

0

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

0

0

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.

0

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.

0

View the running container

Indicates that the newly created container is already running

docker ps

0

Exit the current container

exit

0

View all containers

docker ps -a

0

Stop the ongoing container

docker stop container name or container ID

docker stop mycentos2

0

Start the container

docker start container name or container ID

docker start mycentos2

0

Restart container

docker restart container name or container ID

docker restart mycentos2

0

Go deep inside the container

docker inspect container name or id

docker inspect mycentos2

0 Query the internal information of the container

-f or -format view

docker inspect -f='{
   
   {.State.Status}}' mycentos2

0

 docker inspect -format='{
   
   {.State.Status}}' mycentos2

0

Note: case sensitive

Get the IP address of the container

docker inspect -f='{
   
   {.NetworkSettings.IPAddress}}' mycentos2

0

Delete container

docker rm container name/container ID

docker rm mycentos2

0

Guess you like

Origin blog.csdn.net/shi_hong_fei_hei/article/details/114232257