Great question! The docker run command has many flags to control how a container behaves. Since you are exploring the Docker maze, here are some of the most common and useful ones you will encounter:
1. -d (Detached Mode)
This is the opposite of -it. It runs the container in the background. You won't see the output or be inside the container, but it will keep running.
docker run -d --name my_background_container alpine sleep 1000
2. --rm (Auto-Remove)
This is very helpful for testing. It tells Docker to automatically delete the container as soon as it stops. This keeps your environment clean.
docker run --rm alpine ls /
3. -p (Publish/Port Mapping)
This maps a port on your LabEx VM to a port inside the container. It's essential for web servers.
- Format:
-p <host_port>:<container_port>
docker run -p 8080:80 nginx
4. -v (Volumes)
This mounts a directory from your VM into the container. It’s used to persist data or share files between your host and the container.
- Format:
-v <host_path>:<container_path>
docker run -v /home/labex/project:/data alpine ls /data
5. -e (Environment Variables)
This allows you to pass configuration settings (like passwords or API keys) into the container.
docker run -e MY_VAR=hello alpine printenv MY_VAR
Summary Table
| Flag | Purpose |
|---|---|
-i |
Interactive mode |
-t |
Allocate a terminal (TTY) |
-d |
Run in background (Detached) |
--rm |
Delete container when it exits |
-p |
Map network ports |
-v |
Mount a folder (Volume) |
-e |
Set environment variables |
You are currently using -it to explore the "corridors" of the Alpine container. Once you finish this step, you might find yourself using these other flags to build more complex "rooms" in your Docker architecture!