Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the container commands in docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the container commands in docker, which are very detailed and have certain reference value. Friends who are interested must finish reading them.

Let's download a centos image to demonstrate.

[root@t-docker chenzx] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE [root@t-docker chenzx] # docker pull centosUsing default tag: latestlatest: Pulling from library/centos256b176beaff: Already exists Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024fafStatus: Downloaded newer image for centos:latest [root@t-docker chenzx] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos Latest 5182e96772bf 3 weeks ago 200MB starts the container interactively

Function: create and start the container

Parameters:

-- name=' container new name': specify an alias for the container;-d: run the container in the background and return the container ID, that is, start the guardian container;-I: run the container in interactive mode, usually used in conjunction with-t;-t:tty, reassign the container to a pseudo terminal, usually used with-I;-P: random port mapping -p: specifies the port mapping, available in the following four formats: ip:hostPort:containerPort ip::containerPort hostPort:containerPort containerPort

[root@t-docker chenzx] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5182e96772bf 3 weeks ago 200MB [root@t-docker chenzx] # [root@t-docker chenzx] # docker run-it 5182e96772bf # # this sentence means that I use the centos image as a template Generate a container like 23c6bc378d16 [root@23c6bc378d16 /] # [root@23c6bc378d16 /] # ls # # now it has entered the centos of the container, anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

Open another window and execute the following command on the host machine to see the launched container:

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7da6063256a0 centos "/ bin/bash" 6 seconds ago Up 5 seconds practical_ardinghelli

[root@t-docker chenzx] # docker run-it-name mycentos0115 centos

[root@0ecd4b9f8895 /] # # see that we have generated a new container with centos image as template and id as 0ecd4b9f8895.

Voiceover: a single image can generate multiple containers.

[root@t-docker chenzx] # docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

0ecd4b9f8895 centos "/ bin/bash" 3 minutes ago Up 3 minutes mycentos0115

[root@t-docker chenzx] #

Docker ps

Function: check which containers are running and execute the command on the host machine.

Parameters:

-a: lists all currently running containers + historically run containers;-l:last, displays the most recently created containers;-n: shows the last n created containers -Q: silent mode, showing only container number-- no-trunc: output without truncation [root@t-docker chenzx] # docker ps # # without any parameters Represents the currently running container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7da6063256a0 centos "/ bin/bash" 6 seconds ago Up 5 seconds practical_ardinghelli exit container

Exit: container stops exiting

Ctrl+P+Q: the container does not stop exiting

Start the container

First check out the recently launched containers and see that they are closed:

[root@t-docker chenzx] # docker ps-n 2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0ecd4b9f8895 centos "/ bin/bash" 7 minutes ago Exited (137) 16 seconds ago mycentos01157da6063256a0 centos "/ Bin/bash "23 minutes ago Exited (130) 4 minutes ago practical_ardinghelli

Start the container:

[root@t-docker chenzx] # docker start 0ecd4b9f8895 # # Note add the container id0ecd4b9f8895 [root@t-docker chenzx] # docker ps # found by ps above to view the currently running container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0ecd4b9f8895 centos "/ bin/bash" 8 minutes ago Up 5 seconds mycentos0115 restart container

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0ecd4b9f8895 centos "/ bin/bash" 10 minutes ago Up 2 minutes mycentos0115 [root@t-docker chenzx] # docker restart 0ecd4b9f88950ecd4b9f8895 stop Container [root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0ecd4b9f8895 centos "/ bin/bash" 12 minutes ago Up About a minute mycentos0115 [root@t-docker chenzx] # [root@t-docker chenzx] # docker stop 0ecd4b9f8895 0ecd4b9f8895 Force stop Container

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0ecd4b9f8895 centos "/ bin/bash" 13 minutes ago Up 2 seconds mycentos0115 [root@t-docker chenzx] # [root@t-docker chenzx] # docker kill 0ecd4b9f88950ecd4b9f8895 Delete containers that have been stopped

Docker rm: deleting containers

Docker rmi: deleting mirrors

[root@t-docker chenzx] # docker ps-n 2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0ecd4b9f8895 centos "/ bin/bash" 15 minutes ago Exited (137) About a minute ago mycentos01157da6063256a0 centos "/ bin/bash" 31 minutes ago Exited 11 minutes ago practical_ Ardinghelli [root @ t-docker chenzx] # docker rm 0ecd4b9f8895 Delete closed containers 0ecd4b9f8895 [root@t-docker chenzx] # docker rm-f 0ecd4b9f8895 forcibly delete running container 0ecd4b9f8895

Delete multiple containers at once:

Docker rm-rf $(docker ps-a-Q)

Docker ps-a-Q | xargs docker rm

Start the container in a guardian manner

Command format: docker run-d container name

Parameter description:

-d: start the container in a guardian mode:

-it: launch the container interactively

[root@t-docker chenzx] # docker run-d centosfa80295c878acdf30531ecbf58be87b00427e4f941d10edf93b5d066ba40d000 [root@t-docker chenzx] # docker ps has no output, is it strange that CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Problem: above, we see that after starting the container through docker run-d ceonts (that is, the guarded startup container), and then checking it through docker ps-a, we will find that the container has exited. It is important to note that if the Docker container runs in the background, there must be a foreground process.

The commands run by the container will exit automatically if they are not those that have been suspended (such as running top,tail).

This is a problem with the docker mechanism, such as your web container. Let's take nginx as an example. Normally, we only need to start the corresponding service when we configure the startup service. For example, service nginx start. However, in doing so, nginx runs in background mode, resulting in no applications running in the foreground of docker, and when the container is started in the background, he will immediately commit suicide because he thinks he has nothing to do. Therefore, the best solution is to run the program you want to run as a foreground process.

[root@t-docker chenzx] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtomcat latest 690cb3b9c7d1 7 days ago 463MBcentos latest 5182e96772bf 3 weeks ago 200MB [root@t-docker chenzx] # [root@t-docker chenzx] # docker run-it centos [root@50fffecec77f /] # Exitexit [root@t-docker chenzx] # docker run-it centos / bin/bash # # this means to launch a container in interactive mode using a mirror centos:latest And execute the / bin/bash command [root@4eeeb44eef56 /] # exitexit [root@t-docker chenzx] # docker run-it centos / bin/bash in the container

The above example is a container launched interactively with-it.

So you might want to ask, what's the use of using-d to start the container in guardian mode.

View container log

Command format: docker logs-f-t-- tail container id

Parameter description:

-t is to add a timestamp

-f is printed with the latest log

-- tail is a Linux command that indicates how many last messages are displayed.

[root@t-docker chenzx] # docker run-d centos / bin/sh-c "while true;do echo hello zzyy;sleep 2 does done" b118747a68bbfc6698f9ef9e5d817c037c6ecb9835b86964f6a8b90683dcd178 [root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb118747a68bb centos "/ bin/sh-c 'while t …" 5 seconds ago Up 4 seconds elegant_euclid

Description: the above command means to run the container centos in the background with-d, and run the command / bin/bash-c "while true;do echo hello zzyy;sleep 2done" in the container. Finally, we see that status is in up status through docker ps, indicating that although we started it in the background, the container did not exit this time.

Root@t-docker chenzx] # man sh-c string If the-c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

It can be proved that the container running in backend-d mode has not stopped through the following log:

[root@t-docker chenzx] # docker logs-t-f-- tail 3 b118747a68bb2018-08-30T01:47:55.198597337Z hello zzyy2018-08-30T01:47:57.199661966Z hello zzyy2018-08-30T01:47:59.200811444Z hello zzyy2018-08-30T01:48:01.202114367Z hello zzyy2018-08-30T01:48:03.203363568Z hello zzyy to view the processes in the container

Command format: docker top container id

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb118747a68bb centos "/ bin/sh-c'while t …" 8 minutes ago Up 8 minutes elegant_ euclid [root @ t-docker chenzx] # docker top b118747a68bbUID PID PPID C STIME TTY TIME CMDroot 2952 2934 0 09:43? 00:00:00 / bin/sh-c while true Do echo hello zzyy;sleep 2 sleep 2 check the details inside the container at 09:52-00:00:00 in do echo hello zzyy;sleep 2 2952 0

Command format: docker inspect container id

The command is returned as json.

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb118747a68bb centos "/ bin/sh-c'while t …" 13 minutes ago Up 13 minutes elegant_ euclid [root @ t-docker chenzx] # docker inspect b118747a68bb [{"Id": "b118747a68bbfc6698f9ef9e5d817c037c6ecb9835b86964f6a8b90683dcd178", "Created": "2018-08-30T01:43:24.433793568Z", "Path": "/ bin/sh", "Args": ["- c", "while true;do echo hello zzyy;sleep 2" Done ". Enter the running container and interact with the command line

Two commands to re-enter the container:

Docker exec-it container id bash shell

Docker attach Container id

The difference between the above two commands:

Attach: directly enter the terminal of the container startup command and will not start a new process

Exec: is to open a new terminal in the container and start a new process.

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@t-docker chenzx] # docker run-it centos [root@3d6cf42834fa /] # start a container instance and enter the container [root@3d6cf42834fa /] # pwd/ [root@3d6cf42834fa /] # exitexit [root@t-docker chenzx] # docker run-it Centos / bin/bash [root@3cb3babb797f /] # pwd/

Description: docker run-it centos and docker run-it centos / bin/bash are the same. That is to say, docker run-it centos is equivalent to

Docker run-it centos / bin/bash.

Continue our experiment:

[root@t-docker chenzx] # docker run-it centos [root@04b568f7ee84 /] # ctrl+P+Q: the container does not stop exiting [root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES04b568f7ee84 centos "/ bin/bash" 29 seconds ago Up 28 seconds [ Root@t-docker chenzx] # docker attach 04b568f7ee84 [root@04b568f7ee84 /] # ls / tmpks-script-Lu6hIQ yum.log [root@t-docker chenzx] # docker exec-it f188fac12d4f ls / tmpks-script-Lu6hIQ yum.log description:-t means-tty

In our experiment above, we can see that the container that enters the running container in attch mode needs to perform the ls / tmp operation on the command line in the container, while the container in exec mode writes the operation of ls / tmp directly after the exec command and returns the result to the interface of the host. Sorry, the expression may be a little roundabout, see the above example will understand.

However, exec also has the ability to enter the container to execute commands (the same as attach function), as follows:

[root@t-docker chenzx] # docker exec-it f188fac12d4f / bin/bash [root@f188fac12d4f /] # ls / tmpks-script-Lu6hIQ yum.log

Therefore, the function of exec is more powerful than that of attach. It is recommended that you only learn exec to enter the container.

Copy files from within the container to the host machine

Application scenario: when we run the container interactively in the foreground, if the exit operation is performed in the container, the container will stop and there will be no data running in the container. At this point, we need to copy the running container data to the host for retention.

Command format: docker cp container id or container name: container path destination host path

[root@t-docker tmp] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf188fac12d4f centos "/ bin/bash" 25 minutes ago Up 25 minutes heuristic_ nightingale [root @ t-docker tmp] # docker cp f188fac12d4f:/tmp/yum.log / tmp/ [root@t-docker tmp] # lsyum.log copies files from the host to the container

Command format: docker cp host path container id or container name: path within the container

Import and export of containers

one

two

three

[root@#localhost docker] # docker run-it ubuntu:update / bin/bash

Root@cbe3cb7799ed:/# ls

Bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp update.txt usr var

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

[root@#localhost docker] # docker ps-a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Cbe3cb7799ed ubuntu:update "/ bin/bash" 47 seconds ago Exited (1) 6 seconds ago adoring_kare

812a997f614a ubuntu:14.04 "/ bin/bash" 16 minutes ago Exited () 8 minutes ago zealous_euler

69304dea46c7 gitlab/gitlab-ce:latest "/ assets/wrapper" 2 hours ago Exited (127) 53 minutes ago competent_minsky

67ba866e21b gitlab/gitlab-ce:latest "/ assets/wrapper" 2 hours ago Exited (137) 2 hours ago hungry_hoover

2a3d08aa2ff twang2218/gitlab-ce-zh:9.0.3 "/ assets/wrapper" 2 hours ago Exited (137) 2 hours ago nervous_wozniak

6db49540be99 twang2218/gitlab-ce-zh:9.0.3 "/ assets/wrapper" 2 hours ago Exited (255) 2 hours ago 22/tcp, 443/tcp, 0.0.0.0 hours ago Exited 3000-> 80/tcp romantic_elion

B08a6d6ed716 gitlab/gitlab-ce:latest "/ assets/wrapper" 2 hours ago Exited (255) 2 hours ago 22/tcp, 443/tcp, 0.0.0.0 hours ago Exited 8080-> 80/tcp competent_brahmagupta

33fdb1ebd27 gitlab/gitlab-ce:latest "/ assets/wrapper" 3 hours ago Exited (127) 2 hours ago loving_brattain

6f53620a930c twang2218/gitlab-ce-zh:9.0.3 "/ assets/wrapper" 3 hours ago Exited (127) 2 hours ago brave_galileo

88df78f77c4e ubuntu:14.04 "sleep 360" 4 days ago Exited (137) 4 days ago testcopy

81a879a36bd3 wordpress "docker-entrypoint..." 4 days ago Exited () 4 days ago wordpress

A57a3cc492b7 mysql "docker-entrypoint..." 4 days ago Exited () 4 days ago mysqlwp

# Export the image to a file

one

[root@#localhost docker] # docker export cbe3cb7799ed > update.tar

# create a new image and import from the exported file

one

two

[root@#localhost /] # docker import-update < update.tar

Sha256:fd00d520a43eb5dc6cca8717feca04cfdc53b02cad2fb5b50d877b8e6d6c3bc

one

two

three

four

five

six

seven

eight

nine

ten

[root@#localhost /] # docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

Update latest fd00d520a43e 13 seconds ago 165 MB

Ubuntu update 317f10258460 11 minutes ago 188 MB

Twang2218/gitlab-ce-zh 9.0.3 36172b5fefab 44 hours ago 1.19 GB

Gitlab/gitlab-ce latest 5eff2e44957c 2 days ago 1.11 GB

Mysql latest 9546ca122d3a 8 days ago 407 MB

Wordpress latest 4ad41adc2794 2 weeks ago 401 MB

Ubuntu 14.04 7c09e61e9035 5 weeks ago 188 MB

Daocloud.io/library/nginx 1.7.1 e3e043d3ed2f 2 years ago 499 MB

The above is all the contents of the article "what are the Container commands in docker?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report