Storage and Networking
Table of contents
No headings in the article.
It’s better to create images that result in stateless containers that rely on external data stores. Sometimes though, you need to store your data in a persistent file system.
When this need arises, use the VOLUME instruction as such:
VOLUME path/to/directory
The /path/to/directory is a path to a directory used inside the image. When a container is created using the docker run command, the -v switch can be used to map this directory to an actual volume on the host system.
This is only an indication. By default, if the user doesn’t map this volume to an external store for the container, the data will be stored inside the container.
When your image hosts server software, it listens on one or several ports. For instance, an HTTP server generally listens on the TCP port 80.
You can make this explicit using an EXPOSE instruction:
EXPOSE 80
Using this instruction is purely for documentation purposes. It will NOT open a port to the outside world when a container is created from that image. Anyone who creates a container will need to explicitly bind that port to an actual port of the host machine using the -p switch of the docker run command. In case you forgot, this is what we saw in the “Listening for Incoming Network Connections” section.
Why use the EXPOSE instruction then? Again, it is only for documentation purposes; it enables someone who wants to run a container from your image to know which ports they should redirect to the outside world using the -p switch of the docker run command.