Hacks by Ruddra

Docker: Tips on using Volume, Entrypoint and .dockerignore File

Docker: Tips on using Volume, Entrypoint and .dockerignore File

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 -it mysql -v /db-data /var/lib/mysql/data

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

version: "3.7"
  mysql:
    image: mysql
    volumes:
       - 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.

What is 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 docker-entrypoint.sh file:

#!/bin/bash
bash -e

python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --no-input 

exec "[email protected]"

Then use it in Dockerfile:

FROM python:3-alpine
...
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s /usr/local/bin/docker-entrypoint.sh / # backwards compatible
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["python","manage.py", "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:

.git
.ipynb_checkpoints
.DS_Store
.gitignore
README.md
env.*
# To prevent storing dev/temporary container data
*.csv
tmp

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

References

Share Your Thoughts
M ↓   Markdown