Docker Env and Ports

Wire configuration and networking without editing images.

  • docker env
  • env-file
  • publish port
  • docker secrets
  • port conflict

Docker Env and Ports

Wire configuration and networking without editing images.

Commands

CommandWhat it does
docker run -e NODE_ENV=production app:v1Single env var
docker run --env-file .env app:v1Whole env file
docker run -p 8080:80 app:v1Publish a port to the host
docker run -P app:v1Publish all EXPOSEd ports randomly
`docker exec <c> envsort`Verify what the container actually sees

Example

docker run -d --env-file .env -p 8080:8080 --name api app:v1
docker exec api env | sort | head

FAQ

Where should secrets go?

Not in images: use --env-file from a vault-rendered file, or Docker secrets under Swarm; never bake tokens into layers.

Port already allocated?

Another container owns the host port — docker ps shows mappings; pick a free host port or stop the clash.

常见问题

Where should secrets go?
Not in images: use --env-file from a vault-rendered file, or Docker secrets under Swarm; never bake tokens into layers.
Port already allocated?
Another container owns the host port — docker ps shows mappings; pick a free host port or stop the clash.

Docker