使用 CoreOS(预安装了 docker),我无法使两个容器一起通信。
我正在尝试设置一个由 Gogs 容器(Git 服务器)调用的 MySql 容器。
所以这就是我所做的:
端口设置为127.0.0.1,因此无法从外部访问。
docker run --name mysql1 -v /volumedata/mysql/mysql1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=PWDHERE -p 127.0.0.1:33061:3306 -d mysql/mysql-server:latest --character-set-服务器=utf8 --collation-server=utf8_general_ci
然后我已经安装了
docker run --name=go-git-server1 -p 10022:22 -p 10080:3000 -v /data/go-git-server1:/data gogs/gogs
当我检查时它们都在运行docker ps -a我可以毫无问题地访问“go-git-server1”的安装页面,但是当我指定主机地址时:
127.0.0.1:33061
在 Gogs 的设置页面上,验证后出现以下错误:
数据库设置不正确:拨打 tcp 127.0.0.1:33061: getsockopt: 连接被拒绝
我认为使用“docker ps -a”给出的IP和端口(并且已经使用初始“docker run”手动设置),“go-git-server1”容器足以访问MySql数据库。
我是不是少了一步?我们还需要做些什么才能从其他容器访问 MySql 容器吗?
这是 docker Inspection mysql1 给出的内容:
docker inspect mysql1
[
{
"Id": "c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166",
"Created": "2016-07-03T21:55:29.693884299Z",
"Path": "/entrypoint.sh",
"Args": [
"-p",
"127.0.0.1:33061:3306"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 1,
"Error": "",
"StartedAt": "2016-07-26T18:23:36.144915853Z",
"FinishedAt": "2016-07-26T18:23:38.281589339Z"
},
"Image": "4e66d61404cc06e3c40c46f5e83bda5a14dda2838b84210c7eb5a3d6e1f7752b",
"ResolvConfPath": "/var/lib/docker/containers/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/hostname",
"HostsPath": "/var/lib/docker/containers/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/hosts",
"LogPath": "/var/lib/docker/containers/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166-json.log",
"Name": "/mysql1",
"RestartCount": 0,
"Driver": "overlay",
"ExecDriver": "native-0.2",
"MountLabel": "system_u:object_r:svirt_lxc_file_t:s0:c724,c935",
"ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c724,c935",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/donnees/mysql/mysql1:/var/lib/mysql"
],
"ContainerIDFile": "",
"LxcConf": [],
"Memory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"KernelMemory": 0,
"CpuShares": 0,
"CpuPeriod": 0,
"CpusetCpus": "",
"CpusetMems": "",
"CpuQuota": 0,
"BlkioWeight": 0,
"OomKillDisable": false,
"MemorySwappiness": -1,
"Privileged": false,
"PortBindings": {},
"Links": null,
"PublishAllPorts": false,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"VolumesFrom": null,
"Devices": [],
"NetworkMode": "default",
"IpcMode": "",
"PidMode": "",
"UTSMode": "",
"CapAdd": null,
"CapDrop": null,
"GroupAdd": null,
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"SecurityOpt": null,
"ReadonlyRootfs": false,
"Ulimits": null,
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"CgroupParent": "",
"ConsoleSize": [
0,
0
],
"VolumeDriver": ""
},
"GraphDriver": {
"Name": "overlay",
"Data": {
"LowerDir": "/var/lib/docker/overlay/4e66d61404cc06e3c40c46f5e83bda5a14dda2838b84210c7eb5a3d6e1f7752b/root",
"MergedDir": "/var/lib/docker/overlay/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/merged",
"UpperDir": "/var/lib/docker/overlay/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/upper",
"WorkDir": "/var/lib/docker/overlay/c811d29d866ef977eb0cb93a9697bc4afd2a51e828b7757a5ea8962d784f0166/work"
}
},
"Mounts": [
{
"Source": "/donnees/mysql/mysql1",
"Destination": "/var/lib/mysql",
"Mode": "",
"RW": true
}
],
"Config": {
"Hostname": "c811d29d866e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=mypwd",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.13-1.el7.x86_64.rpm"
],
"Cmd": [
"-p",
"127.0.0.1:33061:3306"
],
"Image": "mysql/mysql-server:latest",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"/entrypoint.sh"
],
"OnBuild": null,
"Labels": {},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": null,
"SandboxKey": "",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": ""
}
}
}
}
]
我已经尝试过给出的IP
ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+'
...而是失败的 127.0.0.1,但不,它不起作用。
看来我有点陷入了 docker 容器通信的第一步。
答案1
嗯,这是一个 Linux/Docker/PuTTy 初学者的问题,所以我花了一段时间才明白该容器的真实 IP 地址在哪里,事实上我只需要多看一点码头工人检查结果。
快捷方式如下所示:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql1
它给出了该容器的IP地址,其他人可以访问该地址。在 docker 检查中也可以看到。
之后,要设置 gogs,我只需要通过以下方式连接 mysql 容器并与之交互:
docker exec -i mysql1 bash
然后给出创建数据库所需的 SQL 命令
mysql -e 'CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' -p 'passwordofdb'
现在一切都很好,gogs 已经设置完毕,并且两个容器正在通信。