Module 4 Lesson 1: Advanced Container Execution
·DevOps

Module 4 Lesson 1: Advanced Container Execution

Go beyond the basic 'run'. Master the advanced flags for port mapping, resource limits, and auto-restart policies to manage production containers effectively.

Module 4 Lesson 1: Advanced Container Execution

In the real world, you don't just "Run" a container. You need to connect it to the internet, limit its memory usage, and ensure it restarts if the server crashes. This is done through advanced flags in the docker run command.

1. Port Mapping (-p)

By default, an app running inside a container (like a web server on port 80) is isolated. You cannot see it from your laptop.

  • The Syntax: -p <Host_Port>:<Container_Port>
  • Example: docker run -p 8080:80 nginx
    • Now, if you go to localhost:8080 on your laptop, you will see the Nginx welcome page from inside the container.

2. Resource Limits

Without limits, one "greedy" container can consume 100% of your RAM and crash the whole server.

  • Memory Limit: --memory="512m"
  • CPU Limit: --cpus=".5" (Limit to half of one CPU core).

3. Restart Policies

What happens if your app crashes at 3:00 AM? You want Docker to fix it for you.

  • --restart no: Don't restart. (Default).
  • --restart always: Always restart, even if you manually stopped it.
  • --restart on-failure: Only restart if it crashed (non-zero exit code).
  • --restart unless-stopped: Restart unless the human explicitly typed docker stop. (Pro Recommended).

4. Environment Variables (-e)

Don't hardcode passwords or database URLs in your image! Inject them when you start the container.

  • Example: docker run -e DB_PASSWORD=Secret123 my-app
  • Your app can then read this variable from the operating system environment.

5. The "Full Power" Run Command

A production-style launch looks like this:

docker run -d \
  --name production-web \
  -p 443:443 \
  -e LOG_LEVEL=info \
  --memory="1g" \
  --restart unless-stopped \
  my-app:v1.2.3

Exercise: The Deployment Plan

Scenario: You are deploying a WordPress site. WordPress likes to run on port 80 and needs to know the database password.

  1. Command: Write the docker run command for an image named wordpress:latest.
    • Map the host port 8000 to the container port 80.
    • Set an environment variable WORDPRESS_DB_PASSWORD to mypassword.
    • Ensure it restarts automatically unless you stop it.
    • Run it in the background.
  2. Resources: How would you limit this container to 256MB of RAM?
  3. Check: After running it, how do you verify the environment variables are actually inside the container? (Hint: check docker inspect).

Summary

docker run is the "Launcher" for your infrastructure. By mastering port mapping, resource limits, and restart policies, you turn a simple process into a resilient, production-grade service.

Next Lesson: Maintenance and cleanup: Stopping, restarting, and removing containers.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn