Docker Series — Part 4: Exposing Containers, Setting Up MySQL, and Managing Environment Variables

Table of contents
- Docker Deep Dive: Container Stats & Management
- Understanding Container Networking
- Exposing a Container to the World (Using Apache Example)
- More Useful Docker Commands
- Bonus Tip: Variables in Linux
- Running MySQL Containers with Environment Variables
- Creating a General MySQL User with Full Config
- Final Thoughts

Welcome to Part 4 of the Docker: Basics to Advance series.
This post is all about moving deeper into container networking, launching services like Apache Server and MySQL, and using environment variables to configure containers in a real-world DevOps setup.
Docker Deep Dive: Container Stats & Management
Let’s start with a few critical Docker commands that help you inspect and manage containers:
Command | Description |
docker info | Shows complete Docker engine info (version, storage driver, etc.) |
docker stats -a | Displays live CPU, memory usage of all containers |
docker ps -a -q | Lists only the container IDs of all containers (quiet mode) |
docker rm -f $(docker ps -a -q) | Force remove all containers at once (super handy!) |
Understanding Container Networking
Docker automatically assigns a private IP to every container.
By default, containers can communicate with the host system.
But for the outside world to access a container (like a website), we need to expose a port.
What is a Port?
Each running service listens on a unique port number (e.g., 80 for HTTP, 3306 for MySQL).
Check ports on your system with:
netstat -tnlp
Exposing a Container to the World (Using Apache Example)
To let the public access your containerized app:
Required:
Expose the port (
-p
flag)Start services inside the container
Use a public IP + port combination to access
Example: Launching Apache in Detached Mode
docker run -dit --name web1 -p 8080:80 httpd
This launches an Apache server container and maps its internal port 80 to host port 8080.
Verify:
Access via browser or:
curl http://<host-ip>:8080
More Useful Docker Commands
Command | Description |
docker inspect <container> | See detailed info about container (network, mounts, etc.) |
docker exec -it <container> bash | Open an interactive terminal session in container |
-d flag in docker run | Run container in background (detached mode) |
Bonus Tip: Variables in Linux
In Linux, we can define user-defined variables using export
or VAR=value
.
This concept is used while passing environment variables to Docker containers too.
Running MySQL Containers with Environment Variables
Some Docker images (like MySQL) require environment variables to configure the database.
Basic MySQL Container with Root Password
docker run -dit --name db1 -e MYSQL_ROOT_PASSWORD=redhat mysql
Enter MySQL Container & Connect:
docker exec -it db1 bash
mysql -u root -p
# Enter password: redhat
Once inside MySQL:
SHOW DATABASES;
Creating a General MySQL User with Full Config
Here’s how to create a non-root user and a database at launch:
docker run -dit --name db123 \
-e MYSQL_ROOT_PASSWORD=redhat \
-e MYSQL_USER=vimal \
-e MYSQL_PASSWORD=redhat \
-e MYSQL_DATABASE=lwstudent \
mysql:latest
Connect with General User:
docker exec -it db123 bash
mysql -u vimal -p
# Enter password: redhat
Now you can manage databases as the general user!
Final Thoughts
In this blog, we unlocked container exposure, volume mounting, port mapping, environment variables, and real-world database deployment using Docker — all crucial pieces of any scalable DevOps infrastructure.
You're not just running containers anymore — you're now building real-world services inside Docker
Subscribe to my newsletter
Read articles from Nitin Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nitin Dhiman
Nitin Dhiman
Self-taught DevOps enthusiast on a journey from beginner to pro. Passionate about demystifying complex tools like Docker, AWS, CI/CD & Kubernetes into clear, actionable insights. Fueled by curiosity, driven by hands-on learning, and committed to sharing the journey. Always building, always growing 🚀