Docker-Day-3
### Project: Managing Docker Volumes
This project demonstrates how to use Docker volumes for persistent storage and data sharing between the container and the host, along with validation steps.
1. List the Volumes
Explanation: Check all Docker volumes available on the system.
docker volume ls
Validation: Ensure the volume list displays correctly without errors.
2. Create a Volume
Explanation: Create a named volume (`my_volume`) for persistent storage.
docker volume create my_volume
Validation:
docker volume ls
Confirm that `my_volume` appears in the list.
3. Attach the Volume to a Container
Explanation: Run a container (`ubuntu`) with the volume attached to `/data`.
docker run -it --name my_container -v my_volume:/data ubuntu
Validation: Inside the container, check if the `/data` directory exists:
ls /data
CTRL + p CTRL + q ( To exit container with out stop)
4. Inspect the Volume
Explanation: Find volume details, including the mount point on the host.
docker volume inspect my_volume
Validation: Verify the mount point path in the JSON output, typically `/var/lib/docker/volumes/my_volume/_data`.
5. Go Inside the Container and Create a File
Explanation: Create a file (`container_file.txt`) in the mounted volume with sample text.
docker exec -it my_container bash
echo "This file is created inside the container" > /data/container_file.txt
exit
Validation: Check the file inside the container:
docker exec my_container cat /data/container_file.txt
6. Check the File on the Host Node
Explanation: Verify that the file exists in the volume's mount point on the host.
ls /var/lib/docker/volumes/my_volume/_data
cat /var/lib/docker/volumes/my_volume/_data/container_file.txt
7. Create a File on the Host Node and Check Inside the Container
Explanation: Create a file (`host_file.txt`) on the host and check it inside the container.
Commands:
echo "This file is created on the host" > /var/lib/docker/volumes/my_volume/_data/host_file.txt
docker exec my_container cat /data/host_file.txt
8. Delete the Container and Reuse the Volume
Explanation: Delete the container, start a new container, and check if the data persists.
docker rm -f my_container
docker run -it --name new_container -v my_volume:/data ubuntu
ls /data
cat /data/container_file.txt
9. Difference Between Volume and `docker cp`
Explanation:
- Volume: Persistent, synchronized storage automatically managed by Docker. Useful for data sharing and retaining data even if the container is deleted.
- `docker cp`: A manual, one-time operation for copying files between the host and the container. It doesn't provide persistence.
Copy Files Between Node (Host) and Docker Container
Scenario: Copy a sample file from the host to a Docker container, verify the content, then copy a file from the container back to the host.
#### 1. Create a Sample File on the Host (Node)
Explanation: Create a text file on the host machine to copy into the container.
echo "This is a sample file created on the host." > /tmp/local_file.txt
#### 2. Check the container new_container is running
docker ps
#### 3. Copy a File from Node to Container
Explanation: Use `docker cp` to copy the file created on the host into the container's `/data` directory.
docker cp /tmp/local_file.txt new_container:/data/copied_from_host.txt
#### 4. Verify the File Inside the Container
Explanation: Log into the container and check if the file is copied successfully.
docker exec -it new_container bash
# Inside the container:
cat /data/copied_from_host.txt
exit
#### 5. Create a File Inside the Container
Explanation: Create a new file inside the container to later copy back to the host.
docker exec -it new_container bash
# Inside the container:
echo "This is a file created inside the container." > /data/container_file.txt
exit
#### 6. Copy a File from Container to Node
Explanation: Use `docker cp` to copy the file from the container to the host machine.
docker cp new_container:/data/container_file.txt /tmp/container_file.txt
#### 7. Validate the Copied File on the Host
Explanation: Verify the file exists and contains the expected content.
cat /tmp/container_file.txt
### Project: Persistent Jenkins Setup in Docker with Volume Management and Port Configuration
This project involves setting up a persistent Jenkins server in Docker using a volume for data storage and ensuring that data remains intact even when the container is replaced.
#### Step 1: Create a Jenkins container
Explanation: Pull and run the Jenkins container in detached mode.
- Command:
docker run -d --name jenkins-container jenkins/jenkins:lts
- `-d`: Run in detached mode.
- `--name`: Name the container.
- `jenkins/jenkins:lts`: Use the latest stable Jenkins image.
Validation: Use `docker ps` to verify that the container is running.
#### Step 2: Inspect the container for IP address and ports
Explanation: Inspect the container to retrieve IP and port information.
- Command:
docker inspect jenkins-container
- Look for `IPAddress` and `Ports` in the output.
Validation: Check connectivity using `curl <IP>:<PORT>`.
#### Step 3: Create a Docker volume for Jenkins
Explanation: Create a named volume to store Jenkins data persistently.
- Command:
docker volume create jenkins-volume
Validation: Use `docker volume ls` to verify volume creation.
#### Step 4: Inspect the volume
Explanation: Check volume details, including the mount point on the host.
- Command:
docker volume inspect jenkins-volume
- Note the `Mountpoint` value for the volume.
#### Step 5: Start Jenkins container with volume and specific port
Explanation: Start Jenkins interactively, attach the volume to `/var/jenkins_home`, and expose port `9999`.
- Command:
docker run -itd --name jenkins-container-1 -p 9999:8080 -v jenkins-volume:/var/jenkins_home jenkins/jenkins:lts
- `-itd`: Interactive, detached mode.
- `-p`: Map container port `8080` to host port `9999`.
- `-v`: Mount volume to `/var/jenkins_home`.
Validation: Use `docker ps` to verify the new container.
#### Step 6: Check the volume data on the host
Explanation: Go to the volume mount point on the host and check if Jenkins data is being written.
- Command:
ls <volume_mount_point>
#### Step 7: Attach to the container with `exec`
Explanation: Use `exec` to access the container and verify its file system.
- Command:
docker exec -it jenkins-container-1 bash
Validation: Check `/var/jenkins_home` inside the container for expected files.
#### Step 8: Add port `9999` to AWS security group
Explanation: Ensure AWS security group allows inbound traffic on port `9999`.
#### Step 9: Open Jenkins in a browser and complete the setup
Explanation:
1. Access Jenkins at `<EC2-Public-IP>:9999`.
2. Complete the initial setup and install default plugins.
3. Create a sample job named `job1`.
#### Step 10: Compare container data with host volume data
Explanation: Verify that `/var/jenkins_home` data matches the host volume data.
- Commands:
- Inside container:
ls /var/jenkins_home
- On host:
ls <volume_mount_point>
#### Step 11: Stop and delete the container
Explanation: Stop and remove the Jenkins container.
- Command:
docker stop jenkins-container-1
docker rm jenkins-container-1
Validation: Use `docker ps -a` to confirm container removal.
#### Step 12: Start a new Jenkins container with the same volume
Explanation: Run a new Jenkins container with the same volume on port `9991`.
- Command:
docker run -itd --name jenkins-container-2 -p 9991:8080 -v jenkins-volume:/var/jenkins_home jenkins/jenkins:lts
#### Step 13: Verify `job1` exists on the new Jenkins container
Explanation: Access Jenkins at `<EC2-Public-IP>:9991` and confirm that `job1` is still present, demonstrating persistence of data.
This sequence ensures a robust Jenkins setup with persistent data, showcasing Docker volumes' ability to maintain state across container lifecycles.