# Docker基础命令

**Docker服务管理**

docker服务基于C/S结构,可以实现基于本地和远程方式进行管理

```bash
#Dockerd守护进程启动选项
-H tcp://host:port
unix:///path/to/socket
    fd://* or fd://socketfd
    
#守护进程默认配置
-H unix:///var/run/docker.sock
 
#docker client 环境变量
export DOCKER_HOST="tcp://docker-server:2375"

开启远程访问
#开启远程访问
vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 --containerd=/run/containerd/containerd.sock
 
#访问方式
curl http://192.168.238.129:2375/info
docker -H tcp://192.168.238.129:2375 info
export DOCKER_HOST="tcp://192.168.238.129:2375"
```

**启动容器**

docker run \[选项] \[镜像名] \[SHELL命令] \[参数]

```bash
#选项
-it #分配一个tty,进入容器SHELL
-d #后台运行
--name #指定窗口名称
-h #设置容器内主机名
--restart=always #开机自启动
--privileged #通过此选项启动,拥有宿主机操作权限
-p #指定端口
--dns #指定DNS
 
#从容器内退出,且容器不停止
CTRL + P + Q
 
#启动守护式容器,长期运行
docker run -d --name alpine-1 alpine
 
#容器开机自启动
docker run -d --name nginx --restart=always -p 80:80 nginx
 
#修改已映射端口,在容器目录下找到hostconfig.json文件
```

**查看容器信息**

```bash
#显示运行容器
docker ps
 
#显示全部容器,包括退出状态的容器
docker ps -a
 
#只显示容器ID
docker ps -a -q
 
#查看容器内进程
docker top ID
 
#容器使用情况,类似top命令
docker stats ID
#启动和停止
docker start NAME
docker stop NAME
```

**进入容器**

```bash
#attach命令类似于VNC
docker attach ID
 
#exec,在运行容器中启动新进程,可以执行单次命令,以及进入容器,通过这种方式进入,执行exit也不会导致容器退出
docker exec -it ID bash
 
#nsenter,通过PID进入
先通过docker inspect获取容器ID
 
#获取容器内IP地址
docker inspect -f "{{.NetworkSettings.IPAddress}}" busybox-2
nsenter -t PID -m -u -i -n -p
```

**相互复制文件**

```bash
#将容器内文件复制到宿主机
docker cp -a ID:/etc/hosts .
 
#宿主机复制容器内
docker cp /etc/issue ID:/root/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-liu-zhang-yun-yuan-sheng-gong-fang/docker-ji-chu-ru-men/docker-ji-chu-ming-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
