1st chunk of `content/get-started/docker-concepts/running-containers/overriding-container-defaults.md`
a2aeda09021e85a5b074ea7054560bf65eff145877bc1cc70000000100000fe6
---
title: Overriding container defaults
weight: 2
keywords: concepts, build, images, container, docker desktop
description: This concept page will teach you how to override the container defaults using the `docker run` command.
aliases:
- /guides/docker-concepts/running-containers/overriding-container-defaults/
---
{{< youtube-embed PFszWK3BB8I >}}
## Explanation
When a Docker container starts, it executes an application or command. The container gets this executable (script or file) from its image’s configuration. Containers come with default settings that usually work well, but you can change them if needed. These adjustments help the container's program run exactly how you want it to.
For example, if you have an existing database container that listens on the standard port and you want to run a new instance of the same database container, then you might want to change the port settings the new container listens on so that it doesn’t conflict with the existing container. Sometimes you might want to increase the memory available to the container if the program needs more resources to handle a heavy workload or set the environment variables to provide specific configuration details the program needs to function properly.
The `docker run` command offers a powerful way to override these defaults and tailor the container's behavior to your liking. The command offers several flags that let you to customize container behavior on the fly.
Here's a few ways you can achieve this.
### Overriding the network ports
Sometimes you might want to use separate database instances for development and testing purposes. Running these database instances on the same port might conflict. You can use the `-p` option in `docker run` to map container ports to host ports, allowing you to run the multiple instances of the container without any conflict.
```console
$ docker run -d -p HOST_PORT:CONTAINER_PORT postgres
```
### Setting environment variables
This option sets an environment variable `foo` inside the container with the value `bar`.
```console
$ docker run -e foo=bar postgres env
```
You will see output like the following:
```console
HOSTNAME=2042f2e6ebe4
foo=bar
```
> [!TIP]
>
> The `.env` file acts as a convenient way to set environment variables for your Docker containers without cluttering your command line with numerous `-e` flags. To use a `.env` file, you can pass `--env-file` option with the `docker run` command.
> ```console
> $ docker run --env-file .env postgres env
> ```
### Restricting the container to consume the resources
You can use the `--memory` and `--cpus` flags with the `docker run` command to restrict how much CPU and memory a container can use. For example, you can set a memory limit for the Python API container, preventing it from consuming excessive resources on your host. Here's the command:
```console
$ docker run -e POSTGRES_PASSWORD=secret --memory="512m" --cpus="0.5" postgres
```
This command limits container memory usage to 512 MB and defines the CPU quota of 0.5 for half a core.
> **Monitor the real-time resource usage**
>
> You can use the `docker stats` command to monitor the real-time resource usage of running containers. This helps you understand whether the allocated resources are sufficient or need adjustment.
By effectively using these `docker run` flags, you can tailor your containerized application's behavior to fit your specific requirements.
## Try it out
In this hands-on guide, you'll see how to use the `docker run` command to override the container defaults.
1. [Download and install](/get-started/get-docker/) Docker Desktop.
### Run multiple instances of the Postgres database
1. Start a container using the [Postgres image](https://hub.docker.com/_/postgres) with the following command:
```console
$ docker run -d -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres
```
This will start the Postgres database in the background, listening on the standard container port `5432` and mapped to port `5432` on the host machine.