1
I went up a small development environment. There are 2 containers:
- mysql (mysql:5.7)
- web (php:7.1-apache)
My question is in a configuration parameter of Docker-Compose.yml and Dockerfile respectively:
- ports
- EXPOSE
What’s the difference between the two?
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 resolved by
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/*
I understood in my case how I am using ports in Docker-Compose.yml I fall into this scenario: Also, if you use -p but you do not use EXPOSE, Docker implicitly will do an EXPOSE. This is because if you’re opening the door publicly then it can be accessible between containers as well. Right?
– Fábio Jânio
Exactly, checking the status you observe the behavior well. As good practice I like to use the
EXPOSE
, especially when the image will be distributed, it is clear to the user what is exposed– Bruno César
When you say it becomes clear, are you referring that when inspecting the image, even without Dockerfile it is possible to see the exposed doors? Also, if you use EXPOSE, I don’t need the reference (port) in Docker-Compose.yml?
– Fábio Jânio
– Bruno César
I understood. To end this post I have one last question, when adding EXPOSE to Dockerfile I am generating a new layer?
– Fábio Jânio
Yes, any Docker instruction when building an image generates a layer. Use
docker history <sua_imagem>
see all the layers of your images.– Bruno César