Introduction
In this blogpost, i will walk you through the process of setting up an Nginx container using Docker, with both a Docker-managed volume and bind mount. This allows you to manage data persistence and easily share files between the host system and the container.
Containers are ephemeral, meaning they short-lived so i will guide you through a better practice of using a volume or bind mounts to persist data outside of the container, so that it can be used by other containers or even outside of Docker and also retains modifications made to files even if the container is stopped or removed. To know more on containers click link to read https://hashnode.com/post/clw833r0y000c0al4bbu3dfe8
What Is A Volume?
Docker creates and maintains controlled storage areas called volumes. Because volumes are made to be independent of a container's lifecycle, even after they are created, attached to, and removed from a container, other containers will still be able to use them. Volumes may be backed up and restored, and they can be shared within containers.
What Is Bind Mounts?
Bind mounts allow you to mount a file or directory from the host computer into a container using Docker. This implies that processes running inside the container can access and modify files on the host machine—in this case, our Ubuntu machine—just as if the files were stored inside the container itself.
Prerequisites
Before you begin, ensure you have the following:
A machine running Ubuntu (instructions are based on Ubuntu 22.04 LTS).
Install Docker on your system.
Step-by-Step Guide
Step 1: Enter Root and Update Package - Authenticate user by running the command sudo su
and input password.
Step 1a: Update your package lists using command apt update
to ensure you have the latest information on the newest versions of packages and their dependencies.
Step 2: Enter Default Directory - Run command cd /var/lib/docker
to enter directory - ls
view the default files within Docker. This is where he containers, volumes etc will be saved
Step 3: Create A Docker Volume - Use command docker volume create <name your volume>
Step 3a: Verify The Creation Of The Volume - Use command docker volume ls
Step 4: Come out of Default Directory Use Command cd --
Step 4a: Enter Root Directory, Run Command cd ~
Step 5: Run an Nginx Container With The Docker Volume & Bind Mount - Run an Nginx container and mount the created volume to the /app
directory inside the container - -d
means detach so we are running the volume in a detached mode
Step 5a: Check The Running Container
Step 5b: To Inspect The Container Run Command docker inspect <container Id>
NOTE : Get your container Id when you check as seen in step 5a
Step 5b continuation : View The Volume Mounted
Step 5c: Inspect Your Volume
Step 6: Create a Directory and a File on the Host - Create a directory on the host system using the command mkdir <name your directory>
Step 7: Create A Text File & Input A Message Within This Directory - Echo the text/message you put into your file - cd <name of directory>
Enter into your directory
step 7a: Run command ls
to view your Text File "eunicedata" within your directory
Step 7b: To view the Message/Text In The Txt.File "eunicedata.txt"
Step 7c: Run Command `cd ~` To Come Out of This Directory ("eunicedata") & Return To Home
Step 8: Run an Nginx Container with a Bind Mount - Run a new Nginx container and use a bind mount to link the host directory to the /app/data
directory inside the container - " Nginx:latest" is to differentiate the the current Image from the previous
Step 8a: Verify The Running Containers
Step 9: To Open An Interactive Terminal - Docker exec -it <container ID> /bin/sh
-To access the container, use docker exec. This will generate an absolute path to the container (mention the container ID) . The absolute path gives you the exact location of your file or directory - -it
open the container in an interactive terminal - ,
is the command line interface that you use to communicate with the operating system - The "#" indicates that we are within the container when you click the Enter key.
Step 9a: List Of Default Directories
Step 9b: Navigate to the bind-mounted directory and verify the file - cd app/data
to view what is in the app directory - cat <txt.file>
To see the content "Hello, World!" of your text file within the container
Step 9c: Modify The File Inside The Container - Modified text to " Hello,Hgital" - Run command cat <name directory.txt>
to see the changes in your text file - exit
to exit the container
Step 10: Verify Changes On The Host - You should see the updated content Hello, Hagital
NOTE: This is Mount binding because changes was made in the container and has been updated on our local host succesfully
Conclusion
You have successfully set up an Nginx container with both a Docker volume and a bind mount. This setup ensures data persistence and allows you to share files between your host system and Docker containers efficiently. Docker volumes are ideal for data that should persist regardless of the container lifecycle, while bind mounts are perfect for scenarios where you need to share files between the host and the container.