Advanced Docker Container Management
Now that you have successfully run your web application in a Docker container, let's explore some additional features and techniques for managing Docker containers.
Running Multiple Instances
You can run multiple instances of your web application on different ports. This is useful for testing or running different versions of your app simultaneously.
First, let's stop and remove our previous container:
docker rm -f my-web-container
Now, let's run two instances of our web application on different ports:
docker run -d -p 5000:5000 --name web-app-1 my-flask-app
docker run -d -p 5001:5000 --name web-app-2 my-flask-app
Verify that both containers are running:
docker ps
You should see two containers running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxxxxxxxxxxx my-flask-app "python app.py" xx seconds ago Up xx seconds 0.0.0.0:5001->5000/tcp web-app-2
xxxxxxxxxxxx my-flask-app "python app.py" xx seconds ago Up xx seconds 0.0.0.0:5000->5000/tcp web-app-1
Now you can access the same application on two different ports:
- http://localhost:5000
- http://localhost:5001
Container Resource Limits
Docker allows you to set resource limits for your containers. This is useful for preventing a container from consuming too many system resources.
Let's stop and remove our previous containers:
docker rm -f web-app-1 web-app-2
Now, let's run a container with memory and CPU limits:
docker run -d -p 5000:5000 --name limited-container --memory=512m --cpus=0.5 my-flask-app
This command creates a container with:
- A maximum of 512MB of memory
- A maximum of 0.5 CPU cores
Verify that the container is running:
docker ps
Container Environment Variables
Environment variables are a common way to configure applications in Docker containers. Let's modify our Flask application to use an environment variable:
docker rm -f limited-container
Create a new version of the app.py file:
cd ~/project/my-web-app
nano app_env.py
Add the following code:
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
app_name = os.environ.get('APP_NAME', 'Default App')
return f"<h1>Hello from {app_name}!</h1><p>This is a simple web application running in a Docker container.</p>"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Press Ctrl+O
followed by Enter
to save the file, then Ctrl+X
to exit nano.
Create a new Dockerfile:
nano Dockerfile-env
Add the following content:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app_env.py ./app.py
EXPOSE 5000
CMD ["python", "app.py"]
Press Ctrl+O
followed by Enter
to save the file, then Ctrl+X
to exit nano.
Build a new image:
docker build -t my-flask-app-env -f Dockerfile-env .
Now, run the container with an environment variable:
docker run -d -p 5000:5000 --name env-container -e APP_NAME="Customized App" my-flask-app-env
Access the web application:
curl http://localhost:5000
You should see:
<h1>Hello from Customized App!</h1><p>This is a simple web application running in a Docker container.</p>
This demonstrates how to pass configuration to your containerized applications using environment variables.