Doubts the configuration of docker-compose and Dockerfile

1

Upload a small development environment. There are 2 containers:

  • mysql (mysql: 5.7)
  • web (php: 7.1-apache)

My question is in a docker-compose.yml and Dockerfile configuration parameter, respectively:

  • ports
  • EXPOSE

What's the difference between them?

docker-compose.yml

version: "3.3"
services:
  mysql:
    container_name: mysql
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: SENHA_AQUI
      MYSQL_DATABASE: webapp
      MYSQL_USER: root
      MYSQL_PASSWORD: senha_aqui
    restart: always
    ports:
      - 3306:3306

  web:
    container_name: web
    image: web_dev
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./htdocs/:/var/www
      - ./apache/:/etc/apache2/sites-available/
    working_dir: /var/www
    depends_on:
      - mysql
    links:
      - mysql
    restart: always
    ports:
      - 80:80

I solved sub

Dockerfile:

FROM php:7.1-apache

MAINTAINER Fabio J L Ferreira <[email protected]>

RUN apt-get update && apt-get install -y curl unzip git npm && curl -sL https://deb.nodesource.com/setup_6.x | bash - && apt-get install -y nodejs

COPY php.ini /usr/local/etc/php/

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer

RUN rm -rf /var/lib/apt/lists/* /tmp/*
    
asked by anonymous 06.10.2017 / 17:50

2 answers

1

As you cite the difference begins at the "scope", where each one is applied. EXPOSE is in image scope, Dockerfile while port in the creation of container , or in compose file through ports , docker-compose run with parameters -p / --publish=[] / --service-ports or without compose using docker create or docker run and the -p / --publish / -P / --publish-all .

So, in spite of the different shapes, we have conceptually the difference in the image and the containers . When creating the image EXPOSE means that the service (s) in container "listen" on a particular port (s), regardless of whether the port in question will be published or not, as well as serve as an instruction to who will use the image, informing which ports can be safely published. / p>

As an example, if I have in Dockerfile to create an XPTO image something like EXPOSE 80 means that regardless of the container > XPTO , this port may be accessible among other containers on the same network.

In summary, regarding port exposure / publishing:

  • without using EXPOSE and nor -p (or other commented variations): the port (s) of the running service will not be accessible from anywhere except inside container in>;
  • using EXPOSE and -p (or other commented variations): the port (s) specified in Dockerfile will be accessible both BETWEEN containers
06.10.2017 / 19:57
0

In Docker each container by default has a network interface, virtual and own. This means that each container gets an internal IP.

EXPOSE is a metadata that explains to consumers that your application or service exposes port (s). Notice, he explains, but does nothing. The only practical function is to allow the DOCKER RUN / CREATE subcommand to use the -P parameter to dynamically port BIND, according to the free ports on your host.

Every container, by default, has all its ports freed which means that in Container IP, ports you have set in EXPOSE or not, can be accessed. Containers on the same bridge network can access other containers and communicate with any port, regardless of the previous existence of an EXPOSE setting.

PORTS, is the Docker-Compose configuration for the PORT BIND, which causes a bridge between the host and the container to be made, for specified ports.

To conclude, being pragmatic: EXPOSE is useless, except to inform which ports your container will use. No process or mechanism is valid or based on this to do anything except the case of -P, as stated in the previous paragraph. But it is a good practice, and should not be ignored when creating images. It is important to make clear which ports will be used, this facilitates, and often helps to understand some coupling problems, for example.

    
20.11.2017 / 03:54