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

Common Docker commands and container lifecycle management

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "commonly used Docker commands and container life cycle management". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "commonly used Docker commands and container life cycle management".

Catalogue

1. Container life cycle management

(1) docker run

(2) start/stop/restart

(3) docker kill

(4) docker rm

(5) pause/unpause

(6) create

(7) docker exec

(8) docker ps

(9) docker inspect

(10) top

1. Container life cycle management (1) docker run

Command description

Create a new container and run a command

Grammar

Docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

OPTIONS description:

-a stdin: specify the standard input and output content type, optional STDIN/STDOUT/STDERR;-d: run the container in the background and return the container ID;-i: run the container in interactive mode, usually used with-t -P: random port mapping, container internal port randomly mapped to host port-p: specify port mapping, format: host (host) port: container port-t: reassign a pseudo-input terminal to the container, usually used with-I;-- name= "nginx-lb": specify a name for the container -- dns 8.8.8.8: specify the DNS server used by the container, default is the same as the host;-- dns-search example.com: specify the container DNS search domain name, default is the same as the host;-h "mars": specify the hostname;-e username= "ritchie" of the container: set the environment variable;-- env-file= []: read the environment variable from the specified file -- cpuset= "0-2" or-- cpuset= "0Magne1Grade2": bind the container to the specified CPU to run;-m: set the maximum memory used by the container;-- net= "bridge": specify the network connection type of the container, support bridge/host/none/container: four types;-- link= []: add a link to another container;-- expose= []: open a port or set of ports;-- volume,-v: bind a volume

Common examples

Use the docker mirror fate:latest to start a container in background mode and name the container myfate.

Docker run-- name myfate-d fate:latest

Use mirrored fate:latest to start a container in background mode and map the container's port 80 to the host random port.

Docker run-P-d fate:latest

Using mirror fate:latest, start a container in background mode, mapping port 80 of the container to port 80 of the host, and the directory / data of the host to the / data of the container.

Docker run-p 80:80-v / data:/data-d fate:latest

Bind port 8080 of the container and map it to port 80 of the local host 127.0.0.1.

$docker run-p 127.0.0.1:80:8080/tcp ubuntu bash

Use the mirror fate:latest to start a container in interactive mode and execute the / bin/bash command inside the container.

Wh@wh-pc:~$ docker run-it fate:latest / bin/bashroot@b8573233d675:/# (2) start/stop/restart

Command description:

Docker start: start one or more containers that have been stopped

Docker stop: stop a running container

Docker restart: restart the container

How to use it:

Start the stopped container myfate

Docker start myfate

Stop the running container myfate

Docker stop myfate

Restart container myfate

Docker restart myfate (3) docker kill

Command description

Kill a running container.

Example

Kill the running container myfate

Wh@wh-pc:~$ docker kill-s KILL myfate (4) docker rm

Command description

Delete one or more containers

Grammar

Docker rm [OPTIONS] CONTAINER [CONTAINER...]

OPTIONS description:

-f: forcibly deletes a running container through the SIGKILL signal. -l: remove the network connection between containers, not the container itself. -v: delete the volume associated with the container.

Common examples

Force deletion of container fate01 and fate02:

Docker rm-f fate01 fate02

Remove the connection of container fate01 to container fate02, connection name db:

Docker rm-l db

Delete the container fate and delete the data volume mounted by the container:

Docker rm-v fate

Delete all containers that have been stopped:

Docker rm $(docker ps-a-Q)

Kill all running containers

Docker kill $(docker ps-a-Q)

Delete all containers that have been stopped

Docker rm $(docker ps-a-Q)

Delete all images that are not labeled with dangling

Docker rmi $(docker images-Q-f dangling=true)

Delete the specified image through the mirrored id

Docker rmi

Delete all mirrors

Docker rmi $(docker images-Q) (5) pause/unpause

Command description

Docker pause: pauses all processes in the container.

Docker unpause: restores all processes in the container.

Grammar

Docker pause CONTAINER [CONTAINER...]

Docker unpause CONTAINER [CONTAINER...]

Common examples

Suspend the database container fate service.

Docker pause fate

Restore database container fate to provide services.

Docker unpause fate (6) create

Command description

Docker create: create a new container without starting it

The usage is the same as docker run

Grammar

Docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

Syntax is the same as docker run

Common examples

Create a container using the docker image fate:latest and name the container myfate

Wh@wh-pc:~$ docker create-name myfate fate:latest (7) docker exec

Command description

Execute commands in the running container

Grammar

Docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

OPTIONS description:

-d: detach mode: run in the background

-I: keep STDIN open even if there is no attachment

-t: assign a pseudo terminal

Common examples

Execute the in-container / root/init.sh script in interactive mode in the container myfate:

Wh@wh-pc:~$ docker exec-it myfate / bin/sh / root/runoob.sh

Open an interactive mode terminal in the container fate:

Wh@:~$ docker exec-I-t myfate / bin/bash

You can also use the docker ps-a command to see which containers are already running, and then use the container ID to enter the container.

Check the container ID that is already running:

# docker ps-a... 9df70f9a0714 openjdk "/ usercode/script.sh..." ...

The 9df70f9a0714 in the first column is the container ID.

Bash the specified container through the exec command:

# docker exec-it 9df70f9a0714 / bin/bash (8) docker ps

Command description

List containers

Grammar

Docker ps [OPTIONS]

OPTIONS description:

-a: displays all containers, including those that are not running.

-f: filter the displayed content according to the criteria.

-format: a template file that specifies the return value.

-l: displays the recently created container.

-n: lists the most recently created n containers.

-no-trunc: the output is not truncated.

-Q: silent mode, showing only the container number.

-s: displays the total file size.

Common examples

Lists all running container information.

Wh@wh-pc:~$ docker psCONTAINER ID IMAGE COMMAND... PORTS NAMES09b93464c2f7 fate:latest "fate-g'daemon off"... 80/tcp, 443/tcp myfate96f7f14e99ab mysql:5.6 "docker-entrypoint.sh"... 0.0.0.0 80/tcp 3306-> 3306/tcp mymysql

Description of output details:

CONTAINER ID: container ID.

IMAGE: the image used.

COMMAND: the command that runs when the container is started.

CREATED: the time when the container was created.

STATUS: container status.

There are 7 states:

Created (created)

Restarting (restarting)

Running (running)

Removing (migrating)

Paused (pause)

Exited (stop)

Dead (death)

PORTS: the port information of the container and the connection type used (tcp\ udp).

NAMES: automatically assigned container name.

Lists the information of the 5 recently created containers.

Wh@whpc:~$ docker ps-n 5CONTAINER ID IMAGE COMMAND CREATED 09b93464c2f7 fate:latest "fate-g'daemon off" 2 days ago... B8573233d675 fate:latest "/ bin/bash" 2 days ago... B1a0703e41e7 fate:latest "fate-g'daemon off" 2 days ago... F46fb1dec520 5c6e1090e771 "/ bin/sh-c 'set-x\ t" 2 days ago... A63b4a5597de 860c279d2fec "bash" 2 days ago.

Filter by label

$docker run-d-name=test-nginx-label color=blue nginx$ docker ps-filter "label=color" $docker ps-filter "label=color=blue"

Filter by name

$docker ps-filter "name=test-nginx"

Filter by statu

$docker ps-a-filter 'exited=0'$ docker ps-filter status=running$ docker ps-filter status=paused

Filter by Mirror

# Image name $docker ps-- filter ancestor=nginx# image ID$ docker ps-- filter ancestor=d0e008c6cf02

Filter according to startup sequence

$docker ps-f before=9c3527ed70ce$ docker ps-f since=6e63f6ff38b0 (9) docker inspect

Command description

Docker inspect: gets the metadata of the container / image.

Grammar

Docker inspect [OPTIONS] NAME | ID [NAME | ID...]

OPTIONS description:

-f: specifies the template file for the return value.

-s: displays the total file size.

-type: returns JSON for the specified type.

Common examples

Gets the meta-information of the mirror fate:1.6.

Wh@wh-pc:~$ docker inspect fate:1.6 [{"Id": "sha256:2c0964ec182ae9a045f866bbc2553087f6e42bfc16074a74fb820af235f070ec", "RepoTags": ["fate:1.6"], "RepoDigests": [], "Parent": "", "Comment": "", "Created": "2016-05-24T04:01:41.168371815Z" "Container": "e0924bc460ff97787f34610115e9363e6363b30b8efa406e28eb495ab199ca54", "ContainerConfig": {"Hostname": "b0cf605c7757", "Domainname": "," User ":"," AttachStdin ": false," AttachStdout ": false," AttachStderr ": false "ExposedPorts": {"3306/tcp": {}},...

Gets the IP of the running container mymysql.

Wh@wh-pc:~$ docker inspect-- format=' {{range .NetworkSettings.Networks}} {{.IPAddress}} {{end}} 'myfate192.17.0.3 (10) top

Command description

View the process information running in the container and support ps command parameters.

Common examples

Wh@wh-pc:~/mysql$ docker top mysqlUID PID PPID C STIME TTY TIME CMD999 40347 40331 18 00:58? 00:00:02 mysqld Thank you for your reading. These are the contents of "Common Docker commands and Container Lifecycle Management". After studying this article, I believe you have a deeper understanding of the commonly used Docker commands and container lifecycle management. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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