1

Using CoreOS (docker preinstalled), I am failing to make two containers communicate together.

I am trying to setup a MySql container called by a Gogs container (Git server).

So here is what I did:

The port is set with 127.0.0.1 so it cannot be accessible from the outside.

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-server=utf8 --collation-server=utf8_general_ci

And then I have installed

docker run --name=go-git-server1 -p 10022:22 -p 10080:3000 -v /data/go-git-server1:/data gogs/gogs

Both of them are running when I check it with docker ps -a and I can access to the install page of "go-git-server1" without any problem, however when I specify the host address:

127.0.0.1:33061

on the setup page of Gogs, after validating I have this error:

Database setting is not correct: dial tcp 127.0.0.1:33061: getsockopt: connection refused

I thought that with the IP and Port given by "docker ps -a" (and already set manually with the initial "docker run") it would have been sufficient for "go-git-server1" container to access the MySql database.

Am I missing a step? Is there something else that we need to do so the MySql container can be accessible from the other container?

Here is what docker inspect mysql1 gives:

 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": ""
            }
        }
    }
}
]

I have tried the ip given by

ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+'

...instead the 127.0.0.1 that failed, but no, it doesn't work.

It seems I am kind of stuck at the first step of docker container communication.

  • 3
    You need to link the container and access it via it's docker address, not 127.0.0.1. See the docs on container linking. – jordanm Jul 26 '16 at 23:01
  • 1
    @jordanm not quite accurate. If you link containers you access the containers directly by each's IP address (or hostname). You only use the docker host's address when accessing published ports. – Patrick Jul 27 '16 at 12:23
0

Well it was a Linux/Docker/PuTTy beginner's question, so it took me a while to understand where was the real IP address of that container, in fact it turned out I only needed to look a little more at the docker inspect result.

A shortcut would look like that :

docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql1

It gives the IP adress of this container, which is accessible from others. It was also visible in docker inspect.

After that, to setup gogs I only needed to connect and interact with the mysql container with:

docker exec -i mysql1 bash

Then give SQL orders necessary to create a database

mysql -e 'CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' -p 'passwordofdb'

All is fine now, gogs has been setup, and the two container are communicating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.