Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose port forwarding

I have a website hosted on shared hosting on production. The website connects to the database via localhost in the code. In my docker-compose I have a php:5.6-apache and mysql:5.6 instance.

Is there anyway to tell docker-compose to have port 3306 on the web container port forwarded to 3306 on the db container so that when the web container ties to connect to localhost on 3306 it gets sent to db on 3306 and also share port 80 on the web container to the outside world?

Current docker-compose.yml:

version: "3"

services:
  web:
    build:  .
    #image: php:5.6-apache
    ports:
     - "8080:80"
    environment:
     - "APP_LOG=php://stderr"
     - "LOG_LEVEL=debug"
    volumes:
     - .:/var/www/html
    network_mode: service:db # See https://stackoverflow.com/a/45458460/95195
#    networks:
#     - internal
    working_dir: /var/www
  db:
    image: mysql:5.6
    ports:
     - "3306:3306"
    environment:
      - "MYSQL_XXXXX=*****"
    volumes:
      - ./provision/mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
#    networks:
#     - internal

networks:
  internal:
    driver: bridge

Current error:

ERROR: for web Cannot create container for service web: conflicting options: port publishing and the container type network mode

like image 694
Justin Dearing Avatar asked Aug 02 '17 10:08

Justin Dearing


People also ask

Does Docker compose expose ports?

Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine. Once again, in the PORTS column, we can find all the exposed ports. The value to the left of the arrow shows the host address where we can reach the container externally.

How do I port forward a Docker container?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

Does Docker compose create a network?

Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.

Is Docker compose deprecated?

Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.


Video Answer


1 Answers

Yes it is possible. You need to use the network_mode option. See the below example

version: '2'

services:
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "80:80"
      - "3306:3306"
  app:
    image: ubuntu:16.04
    command: bash -c "apt update && apt install -y telnet && sleep 10 && telnet localhost 3306"
    network_mode: service:db

outputs

app_1  | Trying 127.0.0.1...
app_1  | Connected to localhost.
app_1  | Escape character is '^]'.
app_1  | Connection closed by foreign host.

network_mode: service:db instructs docker to not assign the app services it own private network. Instead let it join the network of db service. So any port mapping that you need to do, needs to happen on the db service itself.

The way I usually use it is different, I create a base service which runs a infinite loop and the db and app service both are launched on base service network. All ports mapping need to happen at the base service.

like image 137
Tarun Lalwani Avatar answered Sep 30 '22 10:09

Tarun Lalwani