Running Containers
Now that you have an image, you can run containers from it. This chapter dives deeper into the
docker run command and how to control container behavior.Basic Container Run
The simplest form:
docker run my-python-appThis creates a container from the image, runs the default command, and then stops the container.Running in the Background (Detached)
For long‑running applications like web servers, use the
-d flag to run in detached mode:docker run -d --name my-nginx nginxThe container runs in the background, and you get a container ID printed.Interactive Mode
To run a container and interact with its shell:
docker run -it ubuntu bashYou'll get a bash prompt inside the container. Type exit to leave.Publishing Ports
To access a container from your host, map a host port to the container port:
docker run -d -p 8080:80 nginxNow you can open http://localhost:8080 to see Nginx.Setting Environment Variables
Pass environment variables with
-e or --env:docker run -e MY_VAR=hello alpine envMounting Volumes
To persist data or share files between host and container, use volumes or bind mounts:
docker run -v /host/path:/container/path myappWe'll cover volumes in detail later.Container Naming
Give your container a name for easier management:
docker run -d --name webserver nginxTwo Minute Drill
docker runcreates and starts a container.-druns in background;-itfor interactive.-p host:containerpublishes ports.-esets environment variables.--nameassigns a custom name.
Need more clarification?
Drop us an email at career@quipoinfotech.com
