Command To Run A Container In Docker

Command To Run A Container In Docker

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

How to Run a Container in Docker: The Essential Command Explained

If you’re just getting started with Docker, one of the first—and most important—things to learn is how to run a container. Containers are the heart of Docker’s magic, allowing you to package and execute applications consistently across environments.

In this post, I’ll break down the fundamental command used to run containers in Docker. By the end, you’ll know how to pull an image and start a container with ease.


Why Knowing How to Run a Container Matters

Docker streamlines deployment by packaging your application along with its dependencies into a portable container. But until you understand how to run that container, you won’t be able to truly harness Docker’s power.

Getting comfortable with this command forms the foundation for everything from local development and testing to deploying scalable microservices.


The Basic Command: docker run

The key command is:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Let’s break down what each part means:

  • docker run — The command that tells Docker you want to create and start a new container.
  • [OPTIONS] — Flags that change the behavior of your container (optional).
  • IMAGE — The name (and optionally tag) of the Docker image you want to use.
  • [COMMAND] — (Optional) Override the default command in the image.
  • [ARG...] — (Optional) Arguments passed to your overriding command.

Running Your First Container

Let’s try out an example by running an Ubuntu container interactively.

docker run -it ubuntu
  • -i keeps STDIN open even if not attached.
  • -t allocates a pseudo-TTY so you get an interactive terminal.

This command will download the Ubuntu image (if it isn’t saved locally) and open a shell inside a new container. You’ll see something like:

root@1a2b3c4d5e6f:/#

You can now run commands inside this isolated environment. To exit, just type exit.


Running a Container in Detached Mode

Sometimes you want your container running in the background—for instance, when running servers or services.

Use -d for detached mode:

docker run -d nginx

This runs an NGINX web server container in the background. You can then check if it’s running using:

docker ps

Mapping Ports: Exposing Your Container’s Service

When running web servers or other networked apps, map ports between your host machine and the container using -p.

Example:

docker run -d -p 8080:80 nginx

This maps port 8080 on your local machine to port 80 inside the NGINX container. You can open http://localhost:8080 in your browser and see NGINX’s welcome page.


Naming Your Container

Assigning names helps when managing multiple containers:

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

Now you can refer to this container as my_nginx instead of using its ID.


Summary Table of Common Options

OptionPurposeExample
-iInteractive modedocker run -i ubuntu
-tAllocate pseudo-TTYdocker run -t ubuntu
-itInteractive + TTY terminaldocker run -it ubuntu
-dDetached mode (background)docker run -d nginx
-pMap ports between host & containerdocker run -p 8080:80 nginx
--nameName your containerdocker run --name my_app ubuntu

Bonus: Running with Custom Commands

You can override what runs inside your container by specifying commands after the image name:

docker run ubuntu echo "Hello from inside my container"

This starts a temporary Ubuntu container that simply outputs “Hello from inside my container” then stops.


Final Tips

  • Always check which images are available locally with:

    docker images
    
  • Clean up stopped containers with:

    docker container prune
    

Knowing how to properly use docker run opens doors into more complex usage—volumes, environment variables, networking, and beyond.

If you're new, keep experimenting by running different images and options. Each command will solidify your understanding and help unleash Docker's full potential.

Happy Dockering!


Feel free to share your experience or questions about running containers below!