docker container
1. docker daemon daemon
Daemon is a daemon Docker's, Docker Client via the command line to communicate with Docker Damon, complete Docker related operations, Docker daemon via Remote API to achieve Docker located /var/run/docker.sock local IPC / Unix socket; in Windows, Docker daemon by listening named npipe: ////./pipe/docker_engine pipeline to achieve. By the configuration, you may be implemented by means of network communication between Docker Client and daemon. Docker non-default TLS network port for 2375, TLS is the default port 2376.
UNIX domain sockets
default is this way, will generate a /var/run/docker.sock file, UNIX domain sockets used for communication between the local process, this approach is more efficient compared to the network socket high, but it is that the only limitations are the local client access.TCP port listening
server open port monitoring: dockerd -H IP: PORT
Clients through specified IP and port access server: docker -H IP: PORT, to create a container on the service side of the server.
2. docker container-related operations
2.1 Related Commands
In the actual operation of the command can be omitted container.
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
2.2 Starting container
A simple way to start is by container docker container run command.
docker container run ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7ddbc47eeb70: Pull complete
c1bbdc448b72: Pull complete
8c3b70e39044: Pull complete
45d437916d57: Pull complete
Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status: Downloaded newer image for ubuntu:latest
Due to the absence of local ubuntu: latest image, when starting from the corresponding image and docker hub pull start. Once pulled into the local mirror, daemon created and applied to the container in which a specified operation.
By docker ps found out of the container, because the process does not start when you start any extra start a process started at this time when the container.
docker container run -it ubuntu bash
root@7f1b6cf7b7aa:/# ps -elf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 4626 - 06:37 pts/0 00:00:00 bash
0 R root 11 1 0 80 0 - 8599 - 06:38 pts/0 00:00:00 ps -elf
When executing exit will quit the No. 1 process, the container will stop running, "kill the main process container, the container will be killed." It is possible to perform the key combination Ctrl-PQ will exit the container, but the container does not terminate running. This will cut back Docker host of Shell, and keep the vessel running in the background.
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f2c72504e24 ubuntu "bash" 20 seconds ago Up 19 seconds sweet_heisenberg
When you can start + -d let container running in the background.
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f2c72504e24 ubuntu "bash" 20 seconds ago Up 19 seconds sweet_heisenberg
docker container stop 3f2c72504e24
3f2c72504e24
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker container run -it -d ubuntu bash
c95be4dcad34d69693964158138ce19f946b13afd669012659e5cff7715f0400
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c95be4dcad34 ubuntu "bash" 11 seconds ago Up 10 seconds priceless_hofstadter
At this time, re-entering the vessel may be performed
docker exec -it c95be4dcad34 bash
2.3 container life cycle
Section focuses on container life cycle - from creation, running, sleeping, until the destruction of the whole process.
- Start container
docker container run --name percy -it ubuntu:latest /bin/bash
- Stop the container
docker container stop command to stop the operation of the vessel, a pause switch (Vacation) state.
docker container stop percy
- Restart container
using a docker container start command to restart the container.
docker container start percy
- Delete container
through the docker container rm -f command later to add parameters to remove the container running-time is feasible. However, the best way to remove a container or two steps, stop and then remove the container. So as to give an application / process running in the container a stop and clean up residual data opportunities.
docker container rm percy
To sum up the life cycle of the container. Many times as needed can stop, start, pause, and restart the container, and perform these operations quickly. But the container and its data is secure. Until explicitly deleted before the container, the container will not discard the data. Even if the container is removed, if the data is stored in container volume, the data will be saved.
2.4 View container details
When constructing Docker mirror you may wish to list the vessel starts running by default application embedded instructions. If you use the run command to see docker image inspect the vessel to run a mirror, you can see a list of applications when the vessel started to run up. At the same time you can view the details of the container by docker container inspect c95be4dcad34
3. Summary
docker container run是启动新容器的命令。该命令的最简形式接收镜像和命令作为参数。镜像用于创建容器,而命令则是希望容器运行的应用。docker container run -it ubuntu /bin/bash命令会在前台启动一个Ubuntu容器,并运行Bash Shell。
Ctrl-PQ会断开Shell和容器终端之间的链接,并在退出后保持容器在后台处于运行(UP)状态。
docker container ls用于列出所有在运行(UP)状态的容器。如果使用-a标记,还可以看到处于停止(Exited)状态的容器。
docker container exec允许用户在运行状态的容器中,启动一个新进程。该命令在将Docker主机Shell连接到一个运行中容器终端时非常有用。docker container exec -it <container-name or container-id> bash命令会在容器内部启动一个Bash Shell进程,并连接到该Shell。为了使该命令生效,用于创建容器的镜像必须包含Bash Shell。
docker container stop命令会停止运行中的容器,并将状态置为Exited(0)。该命令通过发送SIGTERM信号给容器内PID为1的进程达到目的。如果进程没有在10s之内得到清理并停止运行,那么会接着发送SIGKILL信号来强制停止该容器。docker container stop可以接收容器ID以及容器名称作为参数。
docker container start会重启处于停止(Exited)状态的容器。可以在docker container start命令中指定容器的名称或者ID。
docker container rm会删除停止运行的容器。可以通过容器名称或者ID来指定要删除的容器。推荐首先使用docker container stop命令停止容器,然后使用docker container rm来完成删除。
docker container inspect命令会显示容器的配置细节和运行时信息。该命令接收容器名称和容器ID作为主要参数。