DEV Community

Ankit Kumar
Ankit Kumar

Posted on

Understanding Docker: Containers, Volumes, and Dockerfile by Example

In this blog, we'll walk through some core Docker concepts — including containers, volume mounts, persistent data, and building custom Docker images using Dockerfile. If you're getting into DevOps or containerization, this is the hands-on practical you're looking for!

Automatically Clean Up a Docker Container

If you're running short-lived containers (like Alpine Linux), and want to clean them up automatically once they exit, use:

docker run -it --rm alpine
Enter fullscreen mode Exit fullscreen mode

The --rm flag removes the container once it exits, saving disk space and cleanup time.

Mounting the Host File System in a Container

Containers are isolated, so if you create a file in Container1, it’s not available in Container2. But what if you want to share files between the host and your container?

That’s where bind mounts come in.

Why Share Files?

Instead of rebuilding container images every time, you can mount host files/folders into the container using Docker volumes.

Syntax: Bind Mount using -v

docker run -v <host_path>:<container_path> image_name
Enter fullscreen mode Exit fullscreen mode

Example: Mount a Host HTML Directory to Nginx

docker run -v ~/html:/var/www/html nginx
Enter fullscreen mode Exit fullscreen mode

Example: Multiple Volumes

docker run -v ~/pgdata:/opt/data -v pg.conf:/etc/pg.conf postgres
Enter fullscreen mode Exit fullscreen mode

Be sure to use absolute paths. Docker doesn't understand shortcuts like ~.

Task: Passing a File to a Container

Your app crane reads a file named customerdata.json during runtime. Previously, you had to bake that file into every container image. Now, you can pass it at runtime using volume mounts!

Step-by-step:

  • Modify the file:
vim ~/workspace/customerdata.json
Enter fullscreen mode Exit fullscreen mode

Update the Company Name to: The File Store

  • Run the container:
docker run -v ~/workspace/customerdata.json:/customerdata.json crane
Enter fullscreen mode Exit fullscreen mode

What happens under the hood?

When you mount a file to /customerdata.json, it overrides the internal default file in the image.

The container still works if you don’t pass the file — it just falls back to the default.

Persistent Volumes

Bind mounts are one-time mounts, but Docker also supports persistent named volumes using docker volume or Docker Compose. These help preserve data across container restarts or recreations.

Building a Custom Jenkins Image with Dockerfile

Let’s build a custom Docker image for Jenkins using a Dockerfile.

  • Dockerfile (Example):
FROM openjdk:11-jdk

LABEL maintainer="Ankit"
LABEL env=production

ENV apparea=/home/ankit/Practice

RUN mkdir -p $apparea

ADD https://get.jenkins.io/war/2.397/jenkins.war $apparea

WORKDIR $apparea

EXPOSE 8080

CMD ["java", "-jar", "jenkins.war"]
Enter fullscreen mode Exit fullscreen mode
  • Build the Docker Image:
sudo docker build -t jenkins-custom .
Enter fullscreen mode Exit fullscreen mode
  • Run the Jenkins Container:
sudo docker run -d -p 8080:8080 jenkins-custom
Enter fullscreen mode Exit fullscreen mode
  • Check Running Containers:
sudo docker ps -a
Enter fullscreen mode Exit fullscreen mode

Docker vs Dockerfile

Feature Dockerfile Docker Compose
Focus Build a single image Run multiple containers
File Name Dockerfile (no extension) docker-compose.yml
Commands FROM, RUN, CMD, COPY, etc. services, volumes,etc
Use Case Single-container builds Multi-container applications

What is docker build?

The docker build command creates Docker images from a Dockerfile.

Syntax:

docker build -t <image-name> .
Enter fullscreen mode Exit fullscreen mode

-t lets you tag the image.

. is the build context (typically the current directory).

Common Dockerfile Instructions

Instruction Purpose
FROM Base image (e.g., Ubuntu, Alpine)
RUN Execute commands (e.g., install tools)
COPY Copy files from host to image
ADD Like COPY but supports remote URLs
CMD Default command for container
ENTRYPOINT Similar to CMD but allows arguments
EXPOSE Document container ports

Final Thoughts

With just a few Docker commands and a solid Dockerfile, you can:

  • Mount files from your host system

  • Avoid rebuilding images repeatedly

  • Build production-ready images like Jenkins

  • Simplify container deployment

Have questions or want a full docker-compose.yml example in the next post? Drop a comment or follow me!


Thanks for reading! 🙌

🔗 Follow my work:


Docker #DevOps #Containers #Jenkins #Dockerfile #Volumes #BindMount #OpenSource #Learning #DevOps #Docker #CI_CD #Kubernetes #HandsOnDevOps #DockerLearning #OpenToWork #BuildInPublic #TechLearning

Top comments (0)