Docker(三)容器管理
容器是镜像的运行时实例。正如从虚拟机模板上启动 VM 一样,用户也同样可以从单个镜像上启动一个或多个容器。
root@ryj-dev10:/home/wuh151# docker container --help Usage: docker container COMMAND Manage containers Commands: attach Attach local standard input, output, and error streams to a running container commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem exec Run a command in a running container export Export a container's filesystem as a tar archive inspect Display detailed information on one or more containers kill Kill one or more running containers logs Fetch the logs of a container ls List containers pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container prune Remove all stopped containers rename Rename a container restart Restart one or more containers rm Remove one or more containers run Run a command in a new container start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes Run 'docker container COMMAND --help' for more information on a command.
一、容器生命周期
#展示本地所有镜像 root@ryj-dev10:/home/wuh151# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest ab2f358b8612 2 weeks ago 545MB harbor.lingda.com/uupm/uupm-api dev-nacos e41a5fe13d65 3 weeks ago 180MB #通过镜像生成容器 docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] root@ryj-dev10:/home/wuh151# docker container run -d -p=11001:11001 --name=uupm-api harbor.lingda.com/uupm/uupm-api:dev-nacos f6ac4d7ff65dacc28e947c782cb568cae99b0a50f4a72dbc7537bff867779813 #展示所有的容器-运行中 root@ryj-dev10:/home/wuh151# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6ac4d7ff65d harbor.lingda.com/uupm/uupm-api:dev-nacos "/bin/sh -c 'java ${…" 20 seconds ago Up 17 seconds 0.0.0.0:11001->11001/tcp uupm-api #停止容器 root@ryj-dev10:/home/wuh151# docker container stop f6ac4d7ff65d f6ac4d7ff65d #展示所有的容器 root@ryj-dev10:/home/wuh151# docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6ac4d7ff65d harbor.lingda.com/uupm/uupm-api:dev-nacos "/bin/sh -c 'java ${…" 58 seconds ago Exited (137) 12 seconds ago uupm-api #启动容器 root@ryj-dev10:/home/wuh151# docker container start f6ac4d7ff65d f6ac4d7ff65d #展示容器中运行的进程信息 root@ryj-dev10:/home/wuh151# docker container top f6ac4d7ff65d UID PID PPID C STIME TTY TIME CMD ubuntu 499535 499515 0 18:39 ? 00:00:00 /bin/sh -c java ${javaOpts} -jar *.jar --logging.config=logback.xml ubuntu 499585 499535 14 18:39 ? 00:02:22 java -jar uupm-api.jar --logging.config=logback.xml #删除容器 root@ryj-dev10:/home/wuh151# docker container rm f6ac4d7ff65d Error response from daemon: You cannot remove a running container f6ac4d7ff65dacc28e947c782cb568cae99b0a50f4a72dbc7537bff867779813. Stop the container before attempting removal or force remove #强制杀死容器 root@ryj-dev10:/home/wuh151# docker container kill f6ac4d7ff65d f6ac4d7ff65d
二、进入容器内部
docker attach:使用该命令有一个问题。当多个窗口同时使用该命令进入该容器时,所有的窗口都会同步显示。如果有一个窗口阻塞了,那么其他窗口也无法再进行操作。
docker exec:在运行的容器中执行命令,命令执行后会进入容器的默认工作目录,如果在Dockerfile中指定了工作目录,则会进入Dokcerfile指定的目录,如果命令中指定了工作目录-w则会进入该目录。
root@ryj-dev10:/home/wuh151# docker container exec --help Usage: docker container exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container
#docker container exec 后接sh,是为了进入容器的终端,可以执行一些如ls pwd等一些简单的shell命令。不同的镜像不一样
root@ryj-dev10:/home/wuh151# docker container exec -it f6ac4d7ff65d sh /opt $ ls -l total 70716 -rw-r--r-- 1 root root 4474 Dec 2 08:51 application.properties -rw-r--r-- 1 root root 1448 Dec 2 08:51 bootstrap.properties -rw-r--r-- 1 root root 5163 Dec 2 08:53 logback.xml drwxr-xr-x 2 genius genius 4096 Dec 26 10:38 logs -rw-r--r-- 1 root root 72381258 Dec 2 08:52 uupm-api.jar