Linux Tactic

Mastering Docker: Ways to Customize and Test Containers

Docker has become one of the most popular platforms for developing, shipping, and running applications. Docker’s benefit is its ability to make your application more portable and self-sufficient by bundling it up into a container.

This container allows you to run your application consistently in any environment. One of the most widely used commands in Docker is the ‘docker run’ command, which makes it easy to start and manage containers.

Ways to Run a Docker Container

There are several ways to run a Docker container. Let’s explore each of them.

Running the Container in the Foreground

The ‘docker run’ command can be used to start a container in the foreground, meaning that the root process of the container will be attached to your terminal session. This allows you to interact with your application’s standard input, output, and error streams.

To start a container in the foreground, use the following command:


docker run [image name]


Running the Container in Detached Mode

If you want to run a container and want it to continue running even after the terminal session is closed, you can start the container in detached mode. This means that the container will be started as a Linux process in the background.

To start a container in detached mode, add the ‘-d’ flag to the ‘docker run’ command:


docker run -d [image name]


Removing the Container After Exit

If you start a container in the foreground using the ‘docker run’ command, and then exit from it, the container’s file system and other associated resources will remain on your system. If you don’t want this behavior, you can use the ‘–rm’ flag to remove the container after it exits.

Here’s an example:


docker run –rm [image name]


Setting the Container Name

By default, when a container is started, Docker assigns a unique name to it. However, you can set a custom name for the container using the ‘–name’ flag:


docker run –name [custom name] [image name]


This custom name can be used to reference the container when working with Docker networks or other Docker commands.

Publishing Container Ports

When you start a container, it runs in an isolated environment. To access the application running inside the container, you need to publish its ports to the host machine.

This is done using the ‘-p’ flag. Here’s an example:


docker run -p [host machine port]:[container port]/[TCP or UDP] [image name]


This will map the container port to the specified host machine port.

The TCP or UDP protocol can be specified based on your application’s requirements.

Sharing Data (Mounting Volumes)

A container’s file system is ephemeral, which means that any data stored inside the container will be lost when the container is stopped or removed. To persist data and share it between containers or with the host machine, you can use Docker volumes.

A volume is a directory that is stored outside the container’s file system, which allows it to be shared with other containers or the host machine. To mount a volume, use the ‘-v’ flag.

Here’s an example:


docker run -v [host directory]:[container directory] [image name]


This will map the specified host directory to the container directory. Any data that is stored inside the container directory will be stored on the host machine.

The host directory’s absolute path should be used to avoid any path-related issues.


In conclusion, Docker is a powerful tool for developing, shipping, and running applications. The ‘docker run’ command is one of the most useful commands when working with Docker containers.

By using the above-listed ways to run a Docker container, you can customize your container’s behavior and make it more portable and self-sufficient. Use these techniques to make your Docker experience more efficient, and your workflows smoother.

3) Testing Docker Volumes

Docker volumes are essential to share data among multiple containers or between containers and the host machine. Testing Docker volumes ensures that the data you want to share is correctly accessible by the containers that need it.

There are several ways to test Docker volumes, and we will explore two of them here: running the container interactively and using an interactive shell command.

Running the Container Interactively

Running a Docker container interactively allows you to enter its file system and run interactive processes. To run a Docker container interactively, we use the ‘-i’ and ‘-t’ options of the ‘docker run’ command.

These options enable standard input and a pseudo-tty respectively, which are necessary to interact with the container’s shell. Here’s the command to run an Ubuntu image interactively:


docker run -it ubuntu


After running this command, you will enter the container’s shell, which you can interact with just like the shell on your computer. If you mount a Docker volume to the container, you can check if the data from the volume is accessible.


docker run -it -v /path/to/host/directory:/path/to/container/directory ubuntu


This command will mount the host machine’s directory at ‘/path/to/host/directory’ to the container’s directory at ‘/path/to/container/directory’. This way, you can save data in the host machine’s directory and access it from within the container.

Interactive Shell Command

Another way to test Docker volumes is to use an interactive shell command that allows you to test the volume without starting a container. Here’s an example of how to test a Docker volume using the ‘bash’ command:


docker run –rm -v /path/to/host/directory:/path/to/container/directory ubuntu bash -c “echo ‘testing volume’ > /path/to/container/directory/test.txt”


This command mounts the host machine’s directory at ‘/path/to/host/directory’ to the container’s directory at ‘/path/to/container/directory’.

The ‘-c’ option sets the command to execute within the container. In this case, it creates a file named ‘test.txt’ in the container’s directory and writes ‘testing volume’ to it.

After running this command, check if the file was created in the host machine’s directory at ‘/path/to/host/directory’. If the file exists, it means that the Docker volume was correctly mounted to the container.



Docker is becoming an essential component of the software development workflow. It has become the standard for packaging and deploying applications.

It provides a portable and self-sufficient environment for applications, and Docker volumes add flexibility to the Docker ecosystem, allowing us to share, persist, and access data that is essential to our applications. The Docker container run command provides several options to customize the behavior of the container, making it more versatile.

By following the techniques discussed in this article, you can test the Docker volumes and ensure the data you need is always accessible by your containers. Docker has become an essential component of the software development workflow, providing a portable and self-sufficient environment for applications.

In this article, we explored the ‘docker run’ command and several ways to customize a Docker container’s behavior, including running it in interactive mode, testing Docker volumes, and more. Docker volumes provide flexibility by allowing data sharing among containers and with the host machine.

By following the techniques discussed in this article, you can efficiently use Docker and create a smooth workflow for developing, shipping, and running applications. The importance of understanding and mastering Docker cannot be overstated, and with the resources available today, it is more accessible than ever.

Popular Posts