docker flags & options
-d, --detach
Run container in the background and print the container ID.
docker run -d nginx
docker compose up -d
-p, --publish
Map a host port to a container port. Format: host:container.
docker run -p 8080:80 nginx
docker run -p 127.0.0.1:3000:3000 myapp
-v, --volume
Mount a host directory or named volume into the container.
docker run -v /host/path:/container/path myapp
docker run -v mydata:/var/lib/data postgres
-e, --env
Set an environment variable inside the container.
docker run -e NODE_ENV=production myapp
docker run -e DB_HOST=localhost -e DB_PORT=5432 myapp
--rm
Automatically remove the container when it exits. Useful for one-off commands.
docker run --rm ubuntu echo "hello"
docker run --rm -it python:3 python
-it
Combines -i (interactive, keep stdin open) and -t (allocate a TTY). Used for interactive shells.
docker run -it ubuntu bash
docker exec -it mycontainer sh
--name
Assign a name to the container instead of a random one.
docker run --name myapp -d nginx
--network
Connect the container to a specific Docker network.
docker run --network mynet myapp
docker run --network host myapp
-f, --file
Specify an alternate Dockerfile or Compose file.
docker build -f Dockerfile.prod -t myapp .
docker compose -f docker-compose.prod.yml up
--build-arg
Pass build-time variables to a Dockerfile ARG instruction.
docker build --build-arg VERSION=1.2.3 -t myapp .
-t, --tag
Name and optionally tag an image during build.
docker build -t myapp:latest .
docker build -t myapp:1.0 -t myapp:latest .
--restart
Set the container restart policy.
docker run --restart unless-stopped -d myapp
docker run --restart on-failure:3 -d myapp