Image showing Explore your container with docker run

Explore your container with docker run

affiliate best offer

I. Introduction

Docker is a popular tool used for containerization, allowing developers to package an application and its dependencies into a portable container. Once a Docker container is created, it can be run on any system with Docker installed, making it an ideal solution for building and deploying applications across different environments.

In this article, we’ll cover the basics of running Docker containers and the purpose of the <a class="wiki-link" href="/blog/en/docker/top-5-questions-from-how-to-become-a-docker-power-user-session-at-dockercon-2020">docker</a> run command. We’ll also explore the -it flag and explain why it’s commonly used with the <a class="wiki-link" href="/blog/en/docker/docker-tip-inspect-and-grep">docker</a> run command.

Let’s start by looking at how to run a Docker container.

Example Code:

To get started, make sure you have Docker installed on your system. Once Docker is installed, you can run a container using the following command:

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

This command will download the specified image (if it’s not already available on your system) and run a container using that image. The OPTIONS argument allows you to specify additional options for running the container, such as port mappings or volume mounts. The IMAGE argument specifies the name of the Docker image you want to use to create the container.

For example, the following command will run a container using the official nginx image:

docker run nginx

II. Running a Docker Container

Docker containers can be run using the docker run command. This command has a basic syntax that looks like the following:

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

Where:

  • OPTIONS are the various options that can be used with the command
  • IMAGE is the name of the Docker image to use
  • COMMAND (optional) is the command to run inside the container
  • ARG (optional) is any arguments to the command

Here are some commonly used options with the docker run command:

  • -d: Run the container in detached mode (i.e., in the background)
  • -p: Publish a container’s port to the host
  • -v: Mount a host directory or a named volume as a data volume inside the container

To run a simple container, we can use the following command:

docker run hello-world

This will download and run the “hello-world” Docker image, which is a simple example image that prints a message to the console.

In the next section, we’ll discuss the -it option, which is commonly used with the docker run command. I have prepared a post where you can explore the difference between docker exec and docker attach.

III. Understanding the docker run Command

The docker run command is used to create and run a Docker container. It has a number of different options that allow you to customize how the container is created and how it runs. In this section, we will explore the different parts of the docker run command and their use cases.

The docker run Command Structure

The basic syntax of the docker run command is as follows:

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

Here’s a breakdown of the different parts of the command:

  • docker run: This is the basic command to run a Docker container.

  • [OPTIONS]: These are optional flags that can be used to customize how the container is created and how it runs.

  • IMAGE: This is the name of the Docker image that you want to run.

  • [COMMAND] [ARG...]: These are optional commands and arguments that will be executed inside the container.

docker run Options

There are many options available with the docker run command. Here are some of the most commonly used ones:

  • -d: Run the container in the background (detached mode).

  • -it: Start an interactive shell inside the container.

  • -p: Publish a container’s port(s) to the host.

  • -v: Mount a volume(s) from the host to the container.

  • --name: Assign a name to the container.

  • --rm: Automatically remove the container when it stops running.

docker run Examples

Example 1: Running a Container in Detached Mode

docker run -d nginx

This command will start a container running the nginx image in the background (detached mode).

Example 2: Running a Container with a Specific Name

docker run --name my-nginx nginx

This command will start a container running the nginx image with the name my-nginx.

Example 3: Running a Container with Port Mapping

docker run -p 8080:80 nginx

This command will start a container running the nginx image and map port 80 in the container to port 8080 on the host.

Example 4: Running a Container with Volume Mounting

docker run -v /host/path:/container/path nginx

This command will start a container running the nginx image and mount the /host/path directory on the host to the /container/path directory in the container.

Example 5: Running a Container in Interactive Mode

docker run -it ubuntu bash

This command will start an interactive shell inside a new container running the ubuntu image with the bash command.

Conclusion

The docker run command is a fundamental part of using Docker. Understanding the different options available with this command will help you customize your Docker containers to meet your needs. In the next section, we will explore the -it option in more detail.

Once the container is running, you can get more information on it by inspecting it.

IV. Using the -it Flag with docker run

The -it flag is a commonly used option with the docker run command. It allows users to run a container in an interactive mode with a terminal.

Purpose of the -it flag:

  • -i: This option stands for “interactive” and tells Docker to keep STDIN open even if not attached. It allows you to interact with the container’s command prompt.

  • -t: This option stands for “terminal” and tells Docker to allocate a pseudo-TTY. It provides a terminal or console to the container.

Difference between running a container with and without the -it flag:

  • Without -it flag: When a container is run without the -it flag, it runs in the background or detached mode. The user can view container logs but cannot interact with it.
  • With -it flag: When a container is run with the -it flag, it runs in interactive mode, and the user can interact with it as if it were a local terminal.

Examples of using the -it flag with different types of containers:

  • Running a bash shell inside an Ubuntu container interactively with -it flag:
docker run -it ubuntu:latest /bin/bash
  • Running a container for an interactive Python session:
docker run -it python:3.9-slim
  • Running an interactive MongoDB shell with the latest image:
docker run -it mongo:latest mongo

Using the -it flag with the docker run command is useful when you want to interact with a container’s command prompt or run a container in interactive mode.

V. Why Use the -it Flag with docker run

The -it flag is a commonly used option with the docker run command. It stands for “interactive” and “tty,” and when used, it enables an interactive session with the container. Here are some of the benefits of using the -it flag with docker run:

Enable interactive sessions

The -it flag allows you to interact with the container and run commands inside the container. This is useful for debugging, testing, or running an application interactively.

Allocate a pseudo-TTY

The -it flag allocates a pseudo-TTY, which simulates a real terminal device. This makes it possible to use tools like bash or sh to run commands inside the container.

Keep STDIN open

The -it flag keeps STDIN open, which allows you to send input to the container. This is useful for providing input to interactive applications or scripts.

Start a shell session

The -it flag can be used to start a shell session inside the container. This allows you to explore the container’s file system, install packages, or configure the container.

Here is an example of how to use the -it flag with the docker run command:

$ docker run -it ubuntu /bin/bash

In this example, we are running an interactive session with an Ubuntu container and starting a shell session inside the container using the /bin/bash command.

Running the python interactive inside a container

Another example is running an interactive Python script inside a container:

$ docker run -it python:3.9-slim python -i

This command starts an interactive Python interpreter inside the container, allowing you to run Python code and test different Python libraries.

Overall, using the -it flag with the docker run command can greatly enhance your experience with Docker containers by enabling interactive sessions, allocating a pseudo-TTY, keeping STDIN open, and starting a shell session.

VI. Conclusion

In conclusion, running Docker containers is an essential part of modern software development and deployment. The docker run command is a powerful tool that allows you to create and run containers quickly and easily. Using the -it flag with the docker run command enhances the usability of containers, providing an interactive session with the container.

To summarize, in this article, we have covered the basics of running a Docker container, the docker run command, and the -it flag. We have explained how the -it flag works and its benefits. We hope this article has been informative and helpful in understanding the basics of running Docker containers.

If you’re interested in learning more about Docker and containerization, check out these additional resources:

Full Bright

Full Bright

A professional and sympathic business man.

Contact

Contact Us

To order one of our services, navigate to the order service page

Address

10 rue de Penthièvre,
75008 Paris

Email Us

hello at bright-softwares dot com

Open Hours

Monday - Friday
9:00AM - 05:00PM