Connect from one Docker container to the other one

梦想与她 提交于 2019-12-12 13:41:39

问题


I am running a Java app inside a Docker container which is supposed to connect MySQL inside the other container. Trying multiple options suggested in the forms, nothing really works. Here is my Docker Compose file:

version: "3"
services:

  app:
    build:
      context: ./
      dockerfile: /src/main/docker/Dockerfile
    image: app1
    environment:
      - DB_HOST=Imrans-MacBook-Pro.local
      - DB_PORT=3306
    ports:
      - 8080:8080
    networks:
      - backend
    depends_on:
      - mysql



  mysql:
    image: mysql:5.7.20
    hostname: mysql
    environment:
      - MYSQL_USER=root
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_DATABASE=app1
    ports:
      - 3306:3306
    command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8 --explicit_defaults_for_timestamp
    networks:
      - backend

networks:
  backend:
    driver: bridge

Where DB_HOST=Imrans-MacBook-Pro.local is my laptop's name. This did not work. Some suggest that the container name can be used so tried DB_HOST= mysql, never worked.

The only thing works from times to time when I pass the laptop's IP address, which is not I want to do. So, what is a good way to create communication between those containers?


回答1:


I have, in the past, gotten this to work without explicitly setting the host networking part in Docker Compose. Because Docker images inside a Docker Compose File are put into a Docker Network with each other, you really shouldn't have to do anything to get this to work: by default you should be able to attach into the container for your Spring app and be able to ping mysql and have it work out.




回答2:


The mysql is running in the container so there are two things that you should consider here:

  1. If the mysql is running in the container then you will need to link the app container to the mysql container. This will allow them to talk to each other using docker's inter container communication. The containers talk to each other using hostnames to resolve their respective internal IP addresses. See later in my answer I will show you how to get the two containers to communicate with each other using a compose file.

  2. The mysql container should make use of a docker volume to store the database. This will allow you to store the database and related files on the file system of the host (server or machine where the containers are running on). The docker volume will then be mounted as a directory in the container. Thus the container can now read and write to a directory on the machine where the docker containers are running on. This means that even if the containers are all deleted or removed you will still have the database data persist. Here is a nice beginner friendly article on docker volumes and using them with MySQL:

https://severalnines.com/blog/mysql-docker-containers-understanding-basics

Container communication using only docker without compose:

You have container "app" and "mysql", you want to be able to access "app" on localhost and you want "app" to be able to connect to mysql. How are you gonna do this? 1. You need to expose a port for container "app" so we can access it on localhost. The docker containers have their own internal network and it is closed to you unless you expose some ports with docker.

  1. You need to link the "mysql" container to "app" without exposing "mysql" 's ports to the rest of the world.

This config should work for what you want to achieve:

    version: "2"
    services:

      app:
        build:
          context: ./
          dockerfile: /src/main/docker/Dockerfile
        image: app1:latest
        links:
          - mysql
        environment:
          - DB_HOST=mysql
          # This is the hostname that app will reach the mysql container on.
          # If you do with app container:
          # docker exec -it <app container id> bash
          # # apt-get update -y && apt-get install iputils-ping -y
          #
          # Then you should be able to ping mysql container with:
          #
          # # ping -c 2 mysql
          - DB_PORT=3306
        ports:
          - 8080:8080
          # You will access "app" on localhost:8080 in your browser. If this is running on your own machine.



      mysql: #hostname actually gets set here so no need to set it later
        image: mysql:5.7.20
        environment:
          - MYSQL_USER=root
          - MYSQL_ALLOW_EMPTY_PASSWORD=yes
          - MYSQL_DATABASE=app1
          # Remember to use a volume if you would like this container's data to persist or if you would like
          # to restore a database backup.
        command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8 --explicit_defaults_for_timestamp

Now you can just start it up with:

$ docker-compose up

If you ran this before then just make sure to run this first before running docker-compose up:

$ docker-compose down

Let me know if that helps.




回答3:


DB host should be localhost or 127.0.0.1



来源:https://stackoverflow.com/questions/49806177/connect-from-one-docker-container-to-the-other-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!