0

I installed Docker on my Ubuntu system and made a MySQL container:

version: '3.8'

services:
  MySQL:
    container_name: MySQL
    image: mysql:8.0.19
    volumes:
      - mysql-volume:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: pskPSK258##
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: pskPSK258##
    command: --default-authentication-plugin=mysql_native_password

volumes:
  mysql-volume:
    name: mysql-volume
    driver: local

When the container is running, I can't connect to it using local hosts or 127.0.0.1. But when I get the IP address of the MySQL container with 'Docker inspect MySQL', I can connect to the MySQL. How can I connect to the database using the address 127.0.0.1?

1
  • You're missing ports: that would make it accessible from outside of Docker space. Commented Apr 25, 2020 at 11:17

1 Answer 1

1

In order to be accessible from your host, you need to bind your container's port into your host's port:

services:
  MySQL:
    image: mysql/mysql-server:8.0.19
    ports:
      - "3306:3306"  # or "127.0.0.1:3306:3306" to only bind to localhost

The ports section is in HOST_PORT:CONTAINER_PORT or HOST_IP:HOST_PORT:CONTAINER_PORT format.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.