Create Containers with Host Path
In many cases, we are not used to data volumes but instead. Use the native directory directly for data persistence. In this section, we will use the native directory to create containers.
Target
Your goal is to create a container that uses Host Path
, then copy the /home/labex/project/nginx.txt
file into the container and verify that the files in that container are the same as those in Host Path
.
Result Example
Here's an example of what you should be able to accomplish by the end of this challenge:
- Create an
nginx
directory in the /tmp
directory.
labex:/tmp/ $ ls | grep nginx
nginx
- Start the
nginx-host
container based on the nginx
image that mounts the /tmp/nginx
directory to the /usr/share/nginx/html
directory in the container.
labex:/tmp/ $ docker ps | grep nginx-host
6fc2d40e12df nginx "/docker-entrypoint.โฆ" 44 seconds ago Up 43 seconds 80/tcp nginx-host
- Check the specific mount details with the
docker inspect
command.
"Mounts": [
{
"Type": "bind",
"Source": "/tmp/nginx",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
- Copy the
/home/labex/project/nginx.txt
file to the /usr/share/nginx/html
directory of the nginx-host
container.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat nginx.txt
hello nginx
- Copy the
/home/labex/project/nginx.txt
file to the /usr/share/nginx/html
directory of the nginx-host
container.
labex:nginx/ $ docker exec nginx-host cat /usr/share/nginx/html/nginx.txt
hello nginx
- Check the
/tmp/nginx
directory on the local machine to see if there is a nginx.txt
file.
labex:nginx/ $ pwd
/tmp/nginx
labex:nginx/ $ cat nginx.txt
hello nginx
Requirements
To complete this challenge, you will need:
- Know the
Host Path
path you want to use.
- Know how to use some relevant commands inside the container to verify if the host directory or file can be accessed.
- Know how to start the container using Host Path.
- Be familiar with basic Linux commands, such as
cd
, mkdir
, touch
, etc.