Command To Create Container In Docker

Command To Create Container In Docker

Reading time1 min
#Docker#Containers#DevOps#DockerRun#Containerization#CLI

Mastering Docker: The Command-Line Blueprint to Create Containers Efficiently

Forget vague tutorials. Let's cut through the noise with a no-nonsense guide that demystifies the Docker container creation command — empowering you to build reliable environments from the ground up.


If you're a developer or IT pro aiming to streamline deployment workflows, enhance automation, and maintain scalable infrastructure, understanding the core Docker command to create containers is crucial. Docker containers have revolutionized how we package and deploy applications, but often tutorials skim over how to exactly start a container from the command line in an efficient way.

Today, we'll deep dive into the single most important Docker CLI command: docker run. By mastering this command, you'll create containers efficiently and confidently, with clear awareness of all the options that truly matter.


The Heart of Container Creation: docker run

At its essence, docker run does two things for you:

  1. It creates a new container from a specified image.
  2. It starts (runs) that container immediately.

Basic Syntax

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • OPTIONS: Flags and parameters customizing how your container runs.
  • IMAGE: The Docker image to use (e.g., nginx, python:3.9, myapp:latest).
  • COMMAND & ARG: Override default commands inside the container if needed.

Breaking Down Common & Essential Options

Let's look at some must-know flags that help you create containers tailored for development or production environments:

OptionPurposeExample
-dRun container detached (in background)docker run -d nginx
--nameAssign a human-readable namedocker run --name mynginx nginx
-pPublish container’s port(s) to hostdocker run -p 8080:80 nginx
-vMount volume or directory between host and containerdocker run -v $(pwd)/data:/app/data myapp
-eSet environment variables within the containerdocker run -e ENV=production myapp
--rmAutomatically remove container on exitdocker run --rm ubuntu echo "hello"
-itRun interactively with terminal attacheddocker run -it ubuntu bash

Step-by-Step Example: Create a Nginx Container Exposed at Port 8080

Let’s put this into practice by creating a simple nginx web server container. Here’s the command:

docker run -d --name mynginx -p 8080:80 nginx

What this does:

  • Runs a new container named mynginx.
  • Runs it detached (-d) so your terminal is free.
  • Maps host port 8080 to container port 80 (the default nginx port).
  • Starts an nginx instance from the official image.

Now open your browser and visit http://localhost:8080 — you should see the default nginx welcome page!


Creating Containers with Persistent Data Using Volumes

Containers are ephemeral by design, so if you want your data to survive container restarts or deletions, mount volumes.

Example:

docker run -d --name myapp \
  -v /home/user/data:/app/data \
  myapp-image

This mounts your host’s /home/user/data directory inside the container at /app/data, ensuring changes are saved outside the container.


Interactive Containers for Development & Debugging

Sometimes you need to shell into a running environment:

docker run -it ubuntu bash

This creates an interactive terminal session where you can execute commands inside an Ubuntu environment — great for testing or debugging.


Bonus Tip: Automatically Remove Containers After Exit

When running short-lived jobs or one-off tasks, use:

docker run --rm alpine echo "Job done!"

This runs an Alpine Linux container, prints "Job done!" then cleans up automatically.


Summary & Best Practices

  • Start with simple runs: Use just image names (docker run ubuntu) before adding complexity.
  • Name your containers: makes managing multiple easier (--name app1).
  • Use detached mode for services (-d) but interactive mode (-it) when debugging.
  • Map ports wisely depending on what ports your application uses.
  • Persist data with volumes if needed (-v host_path:container_path).
  • Clean up automatically with --rm for temporary containers.

Mastering these facets of the core Docker create-and-run command will seriously boost your productivity and confidence in managing Docker workloads on any system.


Ready to start building efficient Docker containers? Grab a terminal — it’s as simple as:

docker run hello-world

From there, experiment using flags highlighted here. Mastery begins with one well-understood command — and that command is clear now.

Happy Dockering! 🚢🐳