
Welcome back to the Docker Simplified series! In Part 2 — Installing Docker, we took care of the setup and got Docker up and running on your machine. Now, it’s time to dive into the real magic — containers — the heart of Docker.
In this chapter, we’ll walk you through how to run your first container, explore some basic Docker commands, and learn how to manage resources, networking, and even data persistence. By the end of this blog, you’ll feel confident working with containers and ready to apply them in your own projects.

Docker containers are lightweight, standalone, and executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. In this chapter, we’ll explore how to work with Docker containers effectively.
Running Your First Container
Let’s start by running a simple container:
docker run hello-world
This command does the following:
- Checks for the hello-world image locally
- If not found, pulls the image from Docker Hub
- Creates a container from the image
- Runs the container, which prints a hello message
- Exits the container
Basic Docker Commands
Here are some essential Docker commands for working with containers:
Listing Containers
To see all running containers:
docker ps
To see all containers (including stopped ones):
docker ps -a
Starting and Stopping Containers
To stop a running container:
docker stop <container_id_or_name>
To start a stopped container:
docker start <container_id_or_name>
To restart a container:
docker restart <container_id_or_name>
Removing Containers
To remove a stopped container:
docker rm <container_id_or_name>
To force remove a running container:
docker rm -f <container_id_or_name>
Running Containers in Different Modes
Detached Mode
Run a container in the background:
docker run -d <image_name>
Interactive Mode
Run a container and interact with it:
docker run -it <image_name> /bin/bash
Port Mapping
To map a container’s port to the host:
docker run -p <host_port>:<container_port> <image_name>
Example:
docker run -d -p 80:80 nginx
Working with Container Logs
View container logs
docker logs <container_id_or_name>
Follow container logs in real-time:
docker logs -f <container_id_or_name>
Executing Commands in Running Containers
To execute a command in a running container:
docker exec -it <container_id_or_name> <command>
Example:
docker exec -it my_container /bin/bash
Practical Example: Running an Apache Container
Let’s run an Apache web server container:
- Pull the image:
docker pull httpd
2. Run the container:
docker run -d --name my-apache -p 8080:80 httpd
3. Verify it’s running:
docker ps
4. Access the default page by opening a web browser and navigating to http://localhost:8080
5. Modify the default page:
docker exec -it my-apache /bin/bash
echo "<h1>Hello from my Apache container!</h1>" >
/usr/local/apache2/htdocs/index.html
exit
6. Refresh your browser to see the changes
Container Resource Management
Limiting Memory
Run a container with a memory limit:
docker run -d --memory=512m <image_name>
Limiting CPU
Run a container with CPU limit:
docker run -d --cpus=0.5 <image_name>
Container Networking
Listing Networks
docker network ls
Creating a Network
docker network create my_network
Connecting a Container to a Network
docker run -d --network my_network --name my_container <image_name>
Data Persistence with Volumes
Creating a Volume
docker volume create my_volume
Running a Container with a Volume
docker run -d -v my_volume:/path/in/container <image_name>
Container Health Checks
Docker provides built-in health checking capabilities. You can define a health check in your Dockerfile:
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --
retries=3 \
CMD curl -f http://localhost/ || exit 1
Cleaning Up
Remove all stopped containers:
docker container prune
Remove all unused resources (containers, networks, images):
docker system prune
Conclusion
In this blog, we’ve walked through the basics of working with Docker containers. You’ve learned how to run your first container, manage container resources, handle networking, and ensure data persistence with volumes. Now you’re ready to start applying these skills in your projects and make the most of Docker in your development workflow.
In the next part of this series, we’ll dive into Docker Images, where you’ll learn how to create, manage, and optimize Docker images to power your containers.
Thank you for reading! 💚
