How to access the local host from a Docker container?

Asked

Viewed 2,689 times

4

I’m starting now with Docker and I’m lost in a small detail, I mounted an Ubuntu image with Apache, PHP 7 and the extension connection with SQL Server.

How do I access the SQL Server server if it is on my local network and Docker the network is only on my machine?

Example:

Container IP = 172.18.0.2

IP of my SQL Server = 192.168.0.58

Remembering that I use Docker in windows 10.

If to access I already have how to put this information in my Dockerfile or in Docker-Compose?

  • I voted to reopen this question as I consider Docker to be a "common developer tool", which makes it within the scope defined in the help center.

1 answer

1


There are two ways you can do this.

One is you run SQL Server from a container. You can run it from a Linux container including:

Quickstart: Run the SQL Server 2017 container image with Docker

The configuration on your Docker-Compose.yml will look like this:

version: "3.4"

services:

  ubuntu:
    build: .

  mssql-linux:
    image: microsoft/mssql-server-linux
    ports:
      - "1433:1433"
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=MySafePassw0rd!
    volumes:
      - mssql-data:/var/opt/mssql/data

volumes:
  mssql-data:
    driver: local

The advantage of doing so is that it is easier for those who do not have SQL Server installed on the machine to run something.

The second way is to change the network in Docker Compose to instead of running inside NAT using the same host network

version: "3.4"

services:

  ubuntu:
    build: .

networks:
  default:
    external:
      name: host

Or also use the option --net=host when rotating the container

docker container run --net=host minha-imagem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.