Dockerfile Reference – EXPOSE & ENV Instructions

Hello Everyone,
Hope you’re doing good and utilizing your present in learning new things. In this article we’ll understand two more instructions used in Dockerfile – EXPOSE and ENV. If youre interested in other similar articles, please refer index page.

EXPOSE Instruction :
The EXPOSE instruction informs Docker that container will be listening on the given port. Supported Format,

EXPOSE <port>[<port>/<protocol>]
  • Protocol can be tcp or udp, if not mention, by default it considers tcp.
  • If we want to expose tcp and udp, we’ll require two different instructions.
  • EXPOSE instruction doesn’t actually exposes port to external environment, it acts as documentation between person who’s writing Dockerfile and person who’s running Dockerfile.
  • To expose actual ports, user will need to use -p flag while running the image. Ports need not tobe same once they are mapped to the system.
  • EXPOSE instruction is used to expose any port to external network, if two docker images want to communicate, they can communicate directly once they’re in same network.
  • Regardless of EXPOSE instruction, you can expose any port runtime by using -p flag.
# Exposing tcp port

# Exposing tcp and udp port
EXPOSE 80/tcp
EXPOSE 80/udp

ENV Instruction :
ENV instruction sets the environment variable while building the image as well as for running container. Syntax,

# Single variable
ENV <key>=<value>

# Multiple variables
ENV <key>=<value> \

# Alternate Syntax - it is not advised to use this format as it may depricate in future.
Env <key> <value>

# Variable for single command
RUN <key>=<value> ...
  • ENV instruction can be updated by ADD / COPY commands.
  • ENV instruction is supported by ADD / COPY / ENV / EXPOSE / FROM / LABEL / STOPSINGNAL / USER / VOLUME / USER / ONBUILD instructions.
  • The environment variable created using ENV instruction will persist when container is running from the image, you can check the same using docker inspect command.
  • Sometimes variables created for docker build environment causes issue with container like DEBIAN_FRONTEND=noninteractive changes the behaviour of apt-get in container, so it’s suggested to use single command variable for such variables.
  • User can use ARG instruction if variable is not required in container runtime environment.

That’s it for this article, please let me know your feedback / suggestion / query in comments. If you want to check other Docker and Containerization related articles please visit index page. Stay Safe and Happy Learning !

Reference :

Sanket Modi

Working in Information Technology since 2012. Started my career as Java Developer and now working in multiple different technologies like nodejs, Python, Docker, Kubernetes, Azure etc. I like to explore new technologies and read books.

You may also like...

1 Response

  1. February 18, 2021

    […] Dockerfile Reference – EXPOSE & CMD Instructions […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: