# 容器网络入门操作1

## 测试1：从宿主机访问容器内的mysql

启动一个mysql镜像：

```
sudo docker run --env MYSQL_ROOT_PASSWORD='test1234' -d -v /data/mysql4:/var/lib/mysql --name mysql4 -it mysql:5.7
```

然后在宿主机上使用`docker inspect mysql4 | grep IPAddress` 获取到容器的IP地址，例如`"IPAddress": "172.17.0.2"`

当然也可以使用docker inspect -f 配合模板指令获取，例如：

```
root@09-1:/data/mysql3# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql4
172.17.0.2
```

最后通过在宿主机上使用`mysql -uroot -p -h 172.17.0.2`去访问这个数据库即可。

关于docker inspect的模板指令，建议参考此文：<https://www.jianshu.com/p/65377285662e>


---

# 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://maxidea.gitbook.io/k8s-testing/dan-ji-ce-shi/container-network-startup-testing1.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.
