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

docker container selects different network despite never setting any network

PHPz
PHPzforward
2024-02-09 11:51:17540browse

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",
    ],
)

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

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

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

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete