WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Docker · Basic · question 11 of 100

How do you map a local directory to a directory inside a Docker container?

📕 Buy this interview preparation book: 100 Docker questions & answers — PDF + EPUB for $5

To map a local directory to a directory inside a Docker container, you need to use the -v option with the docker run command. The -v option specifies the volume to mount, which can be a directory or a file on the host system. Here are the steps to map a local directory to a directory inside a Docker container:

Open a terminal window and navigate to the directory containing the file or directory that you want to map.

Run the docker run command with the following syntax:

    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

The IMAGE argument specifies the name and tag of the Docker image to use for the container. The OPTIONS and COMMAND arguments are optional and can be used to customize the behavior of the container.

Use the -v option to specify the local directory and the directory inside the container that you want to map. The -v option takes two arguments, separated by a colon (:). The first argument is the path to the local directory, and the second argument is the path to the directory inside the container.

For example, to map the /path/to/local/directory directory on the host system to the /app directory inside the container, you would run the following command:

    docker run -v /path/to/local/directory:/app IMAGE

This command starts a new Docker container based on the specified IMAGE, mounts the /path/to/local/directory directory on the host system to the /app directory inside the container, and runs the container.

Wait for the Docker container to start. This may take a few seconds, depending on the size of the image and the resources available on your system.

Verify that the local directory is mapped to the directory inside the container by running the ls command inside the container. This command lists the contents of the current directory inside the container.

    docker exec -it CONTAINER_ID ls /app

This should display a list of files and directories that are stored in the /path/to/local/directory directory on the host system.

Here are some additional options that you can use with the -v option:

:ro: This option specifies that the volume should be mounted in read-only mode, which prevents any changes from being made to the volume inside the container.

:z or :Z: These options specify that the volume should be mounted with the z or Z SELinux context, which is necessary for some systems that use SELinux security.

For example, to mount the /path/to/local/directory directory on the host system to the /app directory inside the container in read-only mode, you would run the following command:

    docker run -v /path/to/local/directory:/app:ro IMAGE

This command starts a new Docker container based on the specified IMAGE, mounts the /path/to/local/directory directory on the host system to the /app directory inside the container in read-only mode, and runs the container.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Docker interview — then scores it.
📞 Practice Docker — free 15 min
📕 Buy this interview preparation book: 100 Docker questions & answers — PDF + EPUB for $5

All 100 Docker questions · All topics