Docker container management, hurry up and learn!

Mondo Technology Updated on 2024-01-29

1 Create a container.

docker run the name of the image, if the image does not exist locally, it will go to the image.

docker run equals the name of the docker run image that was created + started
2 Run a hung container.

This method will produce multiple independent container records, and there is no program running in the container, so docker run centos:78.200

Run the container and enter the container, and execute a command in the container.

Run the container and execute a command docker run -it centos:78.2003 bash or docker run -it centos:78.2003 sh or docker run -it centos:78.2003 ping baidu.com View Running Containers docker ps View all containers natively, including running and suspended containers docker ps -a

Open a container and let it run a program for you, which belongs to the foreground and will get stuck in a terminal.

Run the container and perform a ping docker run centos:78.2003 ping baidu.com

3 Run a living container (i.e., a container that docker ps can see).

Processes inside the container must be running in the foreground, otherwise the container will simply exit.

If nothing is done in the container, the container will also hang down, and there must be a process running in the foreground inside the container.

-d parameter to let the container run in the background (for the host) returns the container id docker run -d centos:78.2003 ping baidu.com

Abundant operating parameters.

--rm parameter automatically deletes the container when it stops running docker run -d --rm centos:78.2003 ping baidu.The container name can be customized after the com --name parameter: docker run -d --rm --name tfos centos:78.2003 ping baidu.com

4 View container running logs.

View all logs for the container.

docker logs container id

View the refresh log.

docker logs -f container id

Review the latest 5 log records.

Use tail -5 to specify the latest 5-day log record of the container docker logs 8c8b82976173 | tail -5

5 Enter the container space.

exec into the running container space, -it interactively.

Go to the running container space docker exec -it container id bash to see the command ps -ef that the container is executing

6 View the details of the container for advanced debugging.

View the details of the specified docker container inspect container ID

7 Port mapping for containers.

Users cannot directly access the container, and need to map the port of the host to the port of the container, and the user can access the container by accessing the port of the host.

-d Background Run ,--name Customize the container name, -p map the host port to port 80 of the container docker run -d --name tofs nginx -p 80:80 nginx to see the running container docker ps
Check the port** status of the container.

docker port container ID
Use the host IP to access nginx inside the container

Random port mapping, -p randomly accesses a host's free port, mapped to a port inside the container.

docker run -d --name tfos_nginx_random -p nginx
8 Container Commits.

Running the underlying centos: 78.In 2003, install vim in the container, and then submit a new image, and then run the new image into the container, which carries vim by default.

Run the container and execute the bash command docker run -it centos:78.2003 bash install vim tool yum install vim -y submit image docker commit container id new image name

Use the submitted image.

docker run -it tfos/centos-vim-7.8.2003 bash

Related Pages