Home > Backend Development > Golang > docker container selects different network despite never setting any network

docker container selects different network despite never setting any network

PHPz
Release: 2024-02-09 11:51:17
forward
555 people have browsed it

docker container selects different network despite never setting any network

In Docker, we can provide network connections for containers by selecting different networks. Surprisingly, Docker still assigns a default network to the container even though we never set any network explicitly. This problem bothers many developers as they may encounter some problems related to network configuration. In this article, we will explain in detail why Docker containers have default networks and how to choose between different networks with the help of PHP editor Youzi.

Question content

I'm having trouble setting up the docker configuration for the go service. Here is an overview of my setup

go_binary(
    name = "main_arm64",
    embed = [":server_lib"],
    goarch = "arm64",
    goos = "linux",
    visibility = ["//visibility:public"],
)

container_image(
    name = "ww_server_image",
    base = "@go_image_static_arm64//image",
    entrypoint = ["/main_arm64"],
    files = [":main_arm64"],
    ports = [
        "8080",
        "3306",
    ],
)
Copy after login

I have a graphql playgroud (http) running on http://localhost:8080 and although the port is supposedly exposed, I cannot access the playground ui.

What I want to do is:

  1. Be able to access the graphql playground and any other api running on other ports within the container
  2. Be able to make requests from my dockerized go app to separate mysql containers (I don't know how to put them on the same network as rules_docker).
  3. docker exec -it ... /bin/bash into my docker container (this doesn't work because bash is not installed, but I don't know how to install bash via this container_image command) The error is as follows:
oci runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $path: unknown
Copy after login

If I get the generated docker image id and run docker run -p 8080:8080 image_id, I can access the graphql playground but cannot communicate with the mysql container

If I change the network as follows: docker run --network=host -p 8080:8080 image_id The dockerized go application can successfully communicate with the mysql container, but then the graphql playground becomes inaccessible. The graphql playground is only accessible if I maintain --network=bridge. I'm not sure why mysql doesn't use bridge either, since I never specified the network when starting it. This is how I got the mysql container

docker run -p 3306:3306 --name my-db -e MYSQL_ROOT_PASSWORD=testing -d mysql:8.0.31
Copy after login

Solution

The answer is here: Unable to connect to mysql server using go and docker - dial-up tcp 127.0.0.1:3306: connect: connection refused

It turns out I need to actually access mysql using the following address because docker on mac uses a linux vm:

docker.for.mac.localhost:3306
Copy after login

The above is the detailed content of docker container selects different network despite never setting any network. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template