
Docker runs applications inside containers, which are lightweight, isolated environments that bundle software with its dependencies and configuration. Unlike virtual machines, containers do not require a full guest operating system and instead share the host's kernel. This makes containers faster to start and more resource-efficient. The docker container run command is the primary method for starting containers from images, applying runtime options, and creating isolated processes.
This article explains how to use docker container run to launch containers with custom settings, such as naming containers, running them in detached mode, publishing ports, mounting volumes, and enabling automatic cleanup with --rm. It also includes reference tables that describe important Docker run flags for managing resources, networks, storage, and container health.
Each command demonstrates a core usage of docker container run, including naming, networking, volumes, interactive mode, and cleanup.
Use the --name flag with docker container run to assign a custom name to your container. This helps with logging, management, and automation. Without this flag, Docker generates a random name like frosty_morse.
Container names must begin with an alphanumeric character and may only contain letters, numbers, underscores (_), periods (.), and hyphens (-).
--name <container-name>: Assigns a specific name to the container.[IMAGE]: The Docker image to use (e.g., nginx, ubuntu, busybox).Create a BusyBox container named busybox_test.
Create an Nginx container named web-server.
Create an Ubuntu container named linux.
Create a named container my-nginx and run it in the background.
Verify that the container is running.
Display logs for the my-nginx container.
Rename my-nginx to my-nginx-2.
List containers to confirm the name change.
Output:
By default, Docker runs containers in the foreground and displays their output in your terminal. To run containers in the background, use the -d (detached) flag. This is especially useful for services that need to run continuously, such as web servers and background workers.
-d: Runs the container in detached mode.[IMAGE]: The image to use for the container (e.g., nginx, busybox).Run an Nginx container in background mode.
View logs in real-time.
Open a shell session inside the container.
Run a command inside the container.
Output:
Exit the shell.
Use the -p or --publish flag with docker container run to make a container’s internal ports accessible from the host. Port publishing enables external clients to reach containerized applications like web servers or APIs.
Publishing ports are required only for incoming connections. Containers can initiate outgoing connections without published ports.
-p: Publishes a container port to the host system.[IP:]: (Optional) Bind the port to a specific interface like 127.0.0.1.<host_port>: The port exposed on the host machine.<container_port>: The internal port in the container.[IMAGE]: The Docker image to run.Map container port 80 to host port 8080.
Map host port 8080 to container port 1234 for UDP traffic.
Limit access to localhost only.
Map container port 80 to the same host port.
Expose multiple ports (HTTP and HTTPS).
The following examples demonstrate how port publishing behaves in practice, including local and remote accessibility.
Run an Nginx container and expose port 8080 to the public.
Access the container from an external system (replace with your instance IP).
Run a container restricted to localhost access only.
Verify localhost access works.
Verify remote access is blocked (expect timeout or error).
By default, Docker containers are isolated from the host filesystem. To share files between the host and the container, such as application configs, website content, or database data, use the -v flag to mount a host directory into the container.
-v: Mounts a directory from the host system.<host_path>: Absolute path to a directory on the host.<container_path>: Target directory inside the container.[:options]: Optional settings such as ro (read-only).[IMAGE]: Docker image to run, such as nginx or busybox.Mount a host directory into a BusyBox container with read-write access.
Mount the same directory as read-only.
Use this example to build a real-world containerized web server by serving static files from the host system using Nginx. This demonstrates how volume mounting enables persistent content sharing between host and container.
Create a directory on the host for your static website.
Navigate into the directory.
Create an HTML page.
Run an Nginx container that mounts the website directory and serves it.
-d: Detached mode.-p 8082:80: Maps host port 8082 to container port 80.-v ...:ro: Mounts the host directory as read-only inside the container.Confirm the container is serving the HTML page.
Output:
Mounted directories persist even after the container is removed. Changes made to files in /usr/share/nginx/html are reflected in /home/linuxuser/webdata on the host.
By default, stopped containers remain on the system in an exited state. Use the --rm flag with docker container run to automatically remove the container after it finishes. This is ideal for short-lived containers like scripts, tests, or one-time tasks.
--rm: Removes the container immediately after it exits.[IMAGE]: The Docker image to run.[COMMAND]: The command to execute inside the container.Run a container that prints Hello, world and self-deletes.
Run a container that sleeps for 5 seconds, then exits and removes itself.
Run an Ubuntu container that prints the current date and removes itself.
This demonstration shows how the --rm flag prevents accumulation of exited containers.
Run a temporary BusyBox container with --rm.
Confirm it no longer exists.
The container does not appear in the list. Docker deleted it after execution.
Run the same container without --rm:
List all containers again.
Output:
This container remains on the system in Exited state and must be manually removed using docker container rm.
In this article, you learned how to run containers using the docker container run command. You named containers, launched them in detached mode, mapped ports, mounted host directories, and configured automatic cleanup with --rm. You also explored key flags for managing container networking, resource limits, security options, and health checks.
These capabilities form the foundation for building portable and reproducible container workflows. For more options and advanced configurations, refer to the official Docker documentation.
0 Comments
Be the first to comment and share your perspective with the community.