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

How to use docker container

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Docker container

The docker container is another core concept, and the container is a running instance of the image. The difference is that the image is a static read-only file, the container has the writable layer needed by the runtime, and the application process in the container is running.

A virtual machine simulates a complete set of operating systems, while docker runs only one application and its running environment.

Create Container New Container The new container created by docker [container] create command is stopped [root@docker01 ~] # docker create-it ubuntu:18.04550c14d7db29b3fbcdff0819546403779f8ce717fa2a5012909b057c2f8b1806 [root@docker01 ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES550c14d7db29 ubuntu:18.04 "/ bin/bash" 34 seconds ago Created Kind_rosalind startup container Docker [container] start command to start a created container [root@docker01 ~] # docker start 5555 [root@docker01 ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES550c14d7db29 ubuntu:18.04 "/ bin/bash" 2 minutes ago Up 5 seconds Kind_rosalind create a new container and start it In addition to starting with the start command after creating the container, you can also create and start it directly. Docker [container] run [root@docker01 ~] # docker run ubuntu:18.04 / bin/echo 'hello'hello

When / bin/echo 'hello' is executed, the container terminates automatically.

When you use docker run to create a startup container, it contains:

1. Check whether the specified image exists locally. If not, go under the public repository.

2. Use the image to create an easy one and start it.

3. Assign a file system to the container and attach a read-write layer to the read-only image

4. Bridge a virtual machine port to the container from the bridge interface of the host

5. Bridge address pool assigns an ip address to the container

6. Execute user applications

7. Automatic termination of the container

Start a bash terminal that allows users to interact

[root@docker01 ~] # docker run-it ubuntu:18.04 / bin/bashroot@4d99166324fb:/# pwd/root@4d99166324fb:/# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib media opt root sbin sys usr

-t: assign a terminal (pseudo-tty) to docker and bind it to the standard input of the container

-I: keep the standard input of the container open

When running in the background, more often you need to allow the docker container to run [root@docker01 ~] # docker run-d ubuntu:18.04 / bin/bash-c "while true;do echo 'hello';sleep 1 in Daemonized mode in the background. Done "[root@docker01 ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa5d805944192 ubuntu:18.04" / bin/sh-c'while t … " 4 seconds ago Up 3 seconds frosty_joliot view container output, docker [container] logs [root@docker01 ~] # docker logs a5d805944192 hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello Stop the container pause the container, docker [container] pause CONTAINER [CONTAINER...]

Execute [root@docker01 ~] # docker run-- name test-- rm-it ubuntu bashroot@98eedc501dec:/# on a terminal

Another terminal execution, pause

[root@docker01 ~] # docker pause testtest [root@docker01 ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES98eedc501dec ubuntu "bash" 2 minutes ago Up 2 minutes (Paused) test

Restore

[root@docker01 ~] # docker unpause testtest termination container [root@docker01 ~] # docker stop testtest restart container [root@docker01 ~] # docker restart testtest enters the container

If you need to enter the container for operation, it is recommended to use the official attach or exec command.

The command format for attach is:

Docker attach [--detach-key [= []] [--no-stdin] [--sig-proxy [= true]] CONTAINER

-detach-key [= []] # specifies the shortcut key to exit attach mode. The default is CTRL+p CTRL+q.

-- no-stdin=true | whether false # disables standard input. The default is to keep it open.

-- sig-proxy=treu | false # whether the system signal received by the agent is sent to the application. It is enabled by default.

[root@docker01 ~] # docker run-itd ubuntu:18.04b82dba0090bcb85fafdeef03e67f9973426965f2792efae73de1c07eb0b44bc2 [root@docker01 ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb82dba0090bc ubuntu:18.04 "/ bin/bash" 52 seconds ago Up 50 seconds fervent_ Maxwell [root @ docker01 ~] # docker attach fervent_maxwellroot@b82dba0090bc:/#

Exec is a more convenient tool available after version 1.3.0 docker that can execute any command directly within the running container.

Docker exec [- d |-- detach] [--detach-keys [= []] [- I |-- interactive] [--privileged] [- t |-- tty] [- u |-- user [= USER]] CONAINER COMMAND [ARG...]

-d # execute commands in the background in the container

-- detach-keys= "" # specifies the button for the container to switch back to the background

-e # specify the list of environment variables

-I # Open standard input and accept user input commands. Default is false

-- privileged=treu | whether false # gives the highest permission to execute a command. Default is false.

-t # assigns pseudo terminal. Default is false

-u # user name or ID to execute the command

[root@docker01] # docker exec-it f2554976971b / bin/bashroot@f2554976971b:/# w08 it 02 up 2 days, 18:58, 0 users, load average: 0.03,0.03 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot@f2554976971b:/# ps-efUID PID PPID C STIME TTY TIME CMDroot 100 08:01 pts/0 00:00:00 / bin/bashroot 100 08:02 pts/1 00:00:00 / bin/bashroot 20 100 08:02 pts/1 00:00:00 ps-ef

A new bash terminal will be opened, and the user will interact with the container without affecting other applications in the container.

Delete Container

However, use the docker [container] rm command to delete a container that is in a terminated or exited state. Docker [container] rm [- f |-- force] [- l |-- link] [- v |-- volumes] CONTIAINER

-f # whether to force termination and deletion of a running container

-l # remove the link to the container but keep the container

-v # delete the data volume mounted by the container

View the container in the stopped state and delete

[root@docker01 ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf2554976971b ubuntu:18.04 "/ bin/bash" 46 hours ago Up 46 hours focused_joliotb82dba0090bc ubuntu:18 .04 "/ bin/bash" 47 hours ago Exited (0) 46 hours ago fervent_ Maxwell [root @ docker01 ~] # docker rm b82dba0090bcb82dba0090bc export container

Used to migrate containers from one system to another, you can use the import and export capabilities of docker.

1. Export container means to export a created container to a file, regardless of whether the container is running or not. Docker [container] export [- o |-- output [= "]] CONTAINER

-o # specify the name of the exported tar file, or directly through redirection

[root@docker01 docker] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf2554976971b ubuntu:18.04 "/ bin/bash" 47 hours ago Up 47 hours focused_joliota5d805944192 ubuntu:18.04 "/ bin/sh-c'while t..." 2 days ago Exited 2 days ago frosty_ joliot [root @ docker01 docker] # docker export a5d805944192 > test_ ubuntu2.tar [root @ docker01 docker] # docker export-o test_ubuntu.tar a5d805944192 [root@docker01 docker] # lstest_ubuntu2.tar test_ubuntu.tar Import Container

Exported files can be imported and mirrored using the docker [container] import command. Docker import [- c |-change [= []] [- m |-- message [= MESSAGE]] file | URL |-[REPOSITORY [: TAG]]

-c # execute the Dockerfile instruction to modify the container when it is poured in

[root@docker01 docker] # docker import test_ubuntu.tar test111/ubuntu:v1.0sha256:6c5cd99408ca4648c4d3e5560280e75633e3fbcfdba2778d1b69e4353fb27cc9 [root@docker01 docker] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtest111/ubuntu v1.0 6c5cd99408ca 13 seconds ago 64.2M

Note: the difference between docker load and docker import is that container snapshots (docker import) discard all history and metadata information and save only the snapshot status of the container at that time. The mirrored storage file (docker load) holds the full record and is larger.

View the container

1. View container details, docker container inspect [OPTIONS] ONTAINER

[root@docker01 docker] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf2554976971b ubuntu:18.04 "/ bin/bash" 47 hours ago Up 47 hours focused_ joliot [root @ docker01 docker] # docker Container inspect frosty_joliot [{"Id": "a5d8059441922d3157d2af1d0ab0a724acf13fbe8bb60ddb92fa2536620c2f84" "Created": "2020-03-05T05:50:37.692124654Z", "Path": "/ bin/sh", "Args": ["- c", "while true" Do echo hello;sleep 1 done "]," State ": {" Status ":" exited "

Viewing the specific information of a container will return various information in json format, including ID, creation time, path, status, image, configuration, and so on.

2. Check the processes in the container, docker [container] top [OPTIONS] CONTAINER, similar to linux's top command, including PID, user, time, command, etc. Must be a running container to view

[root@docker01 docker] # docker top focused_joliotUID PID PPID C STIME TTY TIME CMDroot 12365 12348 0 Mar05 pts/0 00:00:00 / bin/bashroot 12431 12348 0 Mar05 pts/1 00:00:00 / bin/bash

3. View statistics. Docker [container] status [OPTIONS] [CONTAINER] will display the statistics of CPU, memory, storage, network and other usage.

-a ~ # outputs all container statistics. By default, it is only in operation.

-format string # formatted output information

-no-stream # does not continue to output, and the continuous real-time results are automatically updated by default

-no-trunc # does not truncate the output information

[root@docker01 docker] # docker stats focused_joliotCONTAINER ID NAME CPU% MEM USAGE / LIMIT MEM% NET I BLOCK O PIDSf2554976971b focused_joliot 0.00% 824KiB / 15.51GiB 0.01% 648B / 0B 0B / 0B 2 other container commands

1. Copy the file

Container cp supports copying files between containers and hosts. Docker [container] cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH |-

In packaging mode, copied files will carry the original UID/GID information

-LMagneWhile link # follows a soft link. When the original path is a soft link, only the link information is copied by default. Using this option, the target content of the link is copied.

Copy the local path oglab/python to the container's / tmp path

[root@docker01 oglab] # docker cp Python-3.8.2 focused_joliot:/tmp

2. View changes

[root@docker01 oglab] # docker container diff focused_joliotA / tmp/Python-3.8.2/Lib/test/test_asyncio/test_streams.pyA / tmp/Python-3.8.2/Lib/test/test_asyncio/test_transports.pyA / tmp/Python-3.8.2/Lib/test/test_asyncio/__init__.pyA / tmp/Python-3.8.2/Lib/test/test_asyncio/echo.pyA / tmp/Python-3.8.2/ Lib/test/test_asyncio/test_queues.pyA / tmp/Python-3.8.2/Lib/test/test_distutils.pyA / tmp/Python-3.8.2/Lib/test/test_posixpath.pyA / tmp/Python-3.8.2/Lib/test/test_subclassinit.pyA / tmp/Python-3.8.2/Lib/test/xmltestdata...

3. Check the port mapping. The container port command can view the port mapping of the container. Docker container port CONTAINER [PRIVATE_ Port [/ PROTO]]

[root@docker01 oglab] # docker container port focused_joliot

4. Update the configuration. The container update command can update some runtime configurations of the container, mainly some resource limit shares. Docker [container] update [OPTIONS] CONTAINER

The total quota is limited to 1 second, and the time taken by the container is 10%. The unit is subtle.

[root@docker01 oglab] # docker update-- cpu-quota 1000000 f2554976971bf2554976971b [root@docker01 oglab] # docker update-- cpu-period 1000000 f2554976971bf2554976971b

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

Servers

Wechat

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

12
Report