Docker Containers

Docker containers are a core feature of Docker, enabling applications to run in isolated and consistent environments across various systems. Containers allow you to build, test, and deploy applications more quickly and consistently, reducing compatibility issues.



What 's a Docker Container?

A Docker container is a small, standalone, executable package that contains everything needed to run an application, including the code, libraries, system tools, and configuration. Containers, unlike virtual machines, share the host system's kernel, making them more efficient and faster while maintaining application isolation.

Why Use Docker Containers?

  • Portability: Your application can run on any system with Docker installed.
  • Consistency: Avoid environment-specific issues during development and deployment.
  • Efficiency: Consume fewer system resources and start quickly.
  • Scalability: Easily replicate and manage containers across environments.

How to Use Docker Containers

You manage Docker containers using the Docker CLI. In this tutorial, we'll use the Nginx web server as an example to demonstrate container operations. Nginx is a popular, lightweight web server that's readily available on Docker Hub.

Creating and Running a Container

To create a container, start by pulling an image from Docker Hub.

docker pull nginx

This command downloads the Nginx image to your system. Then, create and run a container from the image.

docker run -d -p 8080:80 --name webserver nginx

Explanation:

  • -d: Runs the container in detached mode.
  • -p 8080:80: Maps host port 8080 to container port 80.
  • --name webserver: Assigns a custom name to the container.
  • nginx: Uses the Nginx image.

After the container starts, open a web browser and visit http://localhost:8080 to see the Nginx web server in action.

Serving Your Own Files in an Nginx Container

To serve your own HTML files with Nginx, you need to mount a local directory into the container's default web root (/usr/share/nginx/html).

Step 1: Prepare Your HTML Files

Create a directory and add your HTML content:

mkdir my-site
echo "<h1>Hello from Nginx Container</h1>" > my-site/index.html

Step 2: Run the Nginx Container with a Volume

Use the -v flag to mount your local folder to the container's web root:

docker run -d -p 8080:80 --name webserver -v $(pwd)/my-site:/usr/share/nginx/html nginx
  • -v $(pwd)/my-site:/usr/share/nginx/html: Maps your local my-site folder to Nginx's default HTML directory inside the container.
  • Now, open http://localhost:8080 in your browser. You should see your custom HTML page served by the Nginx container.

What Is Volume?

A Docker volume is a way to share data between your host machine and a container. When you use the -v or --volume flag, you're telling Docker to mount a specific directory from your system into the container. This setting is useful when you want the container to access files from your local machine—such as HTML files for an Nginx server.

Common Docker Container Commands

Use the following commands to manage the Nginx container named webserver that you created earlier.

Container Lifecycle

Use these commands to create, start, stop, and manage the state of your container during its lifecycle. Creates and starts a new container:

docker run -d -p 8080:80 --name webserver nginx

Shows all currently running containers:

docker ps

Displays running and stopped containers:

docker ps -a

Gracefully stops the container:

docker stop webserver

Starts a previously stopped container:

docker start webserver

Stops and starts the container:

docker restart webserver

Access & Monitoring

These commands help you interact with the container, view its output, and inspect its internal configuration. Opens an interactive terminal (sh if bash fails):

docker exec -it webserver bash

Displays standard output logs:

docker logs webserver

Continuously streams log output:

docker logs -f webserver

Shows detailed container configuration in JSON:

docker inspect webserver

Cleanup

Use these commands to remove containers and clean up unused resources to free system space. Deletes the specified container

docker rm webserver

Stops and removes the container

docker rm -f webserver

Cleans up all unused containers

docker container prune

Conclusion

Docker containers provide a consistent, isolated, and portable way to run applications. In this tutorial, you learned how to create, run, and manage containers using the Docker CLI.

Key takeaways:

  • Containers simplify deployment by ensuring consistency across environments.
  • You can manage containers easily using Docker commands like run, stop, start, and rm.
  • Docker CLI also allows real-time logging, shell access, and configuration inspection.

Learning how to work with containers is essential for modern, scalable, and efficient application development.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Keep W3schools Growing with Your Support!
❤️ Support W3schools