Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

Docker containers are isolated environments. However, containers sometimes need to persist and share data. It may happen when a second container needs to access a shared cache or use database data. We may also need to backup or perform operations on user-generated data.

In this short tutorial, we'll see how to share data between Docker containers with an example using Docker Compose.

2. Persist and Share Data with Docker Storage

When containers run, all files get a writable space. However, they no longer exist once we stop the container.

Docker uses Storage with persistent and in-memory options if we need to save our data.

Storing files also improves performance because it writes directly to the host filesystem instead of using the container’s writable layer.

2.1. Docker Volumes

Let's have a quick look at Docker Volumes. For example, let's run an Nginx container with a named volume.

First, let's create our volume:

docker volume create --name volume-data

Then, let's run our container:

docker run -d -v volume-data:/data --name nginx-test nginx:latest

In this case, Docker will mount in the container’s /data folder. It will also copy the directory’s contents into the volume if the container has files or directories in the path to be mounted.

We can also have a look at bind mounts for persistent storage.

2.2. Share Data with Volumes

Multiple containers can run with the same volume when they need access to shared data.

For example, let's start our web application:

docker run -d -v volume-data:/usr/src/app/public --name our-web-app web-app:latest

Docker creates a local volume by default. However, we can use a volume diver to share data across multiple machines.

Finally, Docker also has –volumes-from to link volumes between running containers. It may help for data sharing or more general backup usage.

3. Share Data with Docker Compose

We've seen how to create volumes with Docker. Docker Compose also supports the volumes keyword within the YAML template definition.

Let's create a docker-compose.yml to run both an Nginx container and our web application sharing the same volume:

    container_name: nginx
    build: ./nginx/
      - shared-volume:/usr/src/app

    container_name: web-app
    env_file: .env
      - shared-volume:/usr/src/app/public
      - NODE_ENV=production


Again, in Docker Compose, the default driver will be local. We can also specify a driver to use for this volume:

    driver: some-driver

We may also need to use a volume external to Docker Compose:

    external: true
    name: shared-data

4. Conclusion

In this article, we've seen how to share Docker containers' data using volumes. We have also seen the same concept with a simple example using Docker Compose.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!