How to Use Volume, EntryPoint and Ignore Files in Docker

How to Use Volume, EntryPoint and Ignore Files in Docker

Have you worked with Docker before? Do you think you have trouble with it, like when you are developing an application, you need to build every time to see the results, or thinking of the best way to load data in DB, or may be you are annoyed because there are lots of markdown files, screenshots etc that are taking a lot of space in your container. Here are some tips about VOLUME, ENTRYPOINT, and .dockerignore file to fix these problems.

Use VOLUME wisely

VOLUME is a fantastic feature where you can share a space from your local machine to container. But there are other usages to that as well. For example:

  • You can put your source code in Docker Container through Volume. Then whenever you change in your source code, it will be immediately reflected inside Docker Container. FYI: this is not recommended for production. In production, you should use COPY to copy the source code in docker file.

  • VOLUME directory is a place where you can change without any permission. I mean, normally after an Image is built, you can’t change inside Container apart from temp directory. If you need to do that, then you need to give permission to the docker container user to allow write. Instead, use volume to do the necessary changes, like installing packages. Even you can install package in local machine and it will automatically synced inside docker container.

Usage of volume

If you want to run it in traditional way, then use a -v tag:

docker run -v /db-data /var/lib/mysql/data -it mysql

Or if you are using Docker Compose, then use the following:

version: "3.7"
    image: mysql
       - db-data:/var/lib/mysql/data

Do heavy lifting by ENTRYPOINT

If you want to load data in your docker container or execute scripts before running the actual command, it is better to do them in ENTRYPOINT.


ENTRYPOINT is the binary which is executing. You can define it in Dockerfile or you can provide like this docker run --entrypoint="/bin/bash/" .... CMD is the default argument to container. Without the ENTRYPOINT, default argument is command that is executed. With ENTRYPOINT, CMD is passed to entrypoint as argument. You can emulate a command with entrypoint.

$ cat Dockerfile
FROM python:3-alpine
ENTRYPOINT ["/bin/sh"]

$ docker run -it your_docker_image your_cmd

Usage of entry point

For that lets write a file:

bash -e

python makemigrations
python migrate
python collectstatic --no-input

exec "$@"

Then use it in Dockerfile:

FROM python:3-alpine
COPY /usr/local/bin/
RUN ln -s /usr/local/bin/ / # backwards compatible
CMD ["python","", "runserver"]

Exclude unnecessary files using .dockerignore

You can ignore unnecessary files like readme files, version controlling, example codes etc when building the image, using .dockerignore file. Here is an example:

# To prevent storing dev/temporary container data

Also, be sure to read .dockerignore file whenever you are using a repository(if there is one with .dockerignore). Because it might ignore necessary files in Docker which are needed by you.

In conclusion

VOLUME, ENTRYPOINT and .dockerignore, they are all powerful features of Docker. They can make your life a breeze when you know how to use them. But If you want to master them, it is better to try them yourself.

Relevent posts


Last updated: July 6, 2020

Share Your Thoughts
M↓ Markdown