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
.dockerignore file to fix these problems.
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
COPYto copy the source code in docker file.
VOLUMEdirectory is a place where you can change without any permission. I mean, normally after an Image is built, you can’t change inside
tempdirectory. 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.
If you want to run it in traditional way, then use a
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" mysql: image: mysql volumes: - db-data:/var/lib/mysql/data
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 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
For that lets write a
#!/bin/bash bash -e python manage.py makemigrations python manage.py migrate python manage.py collectstatic --no-input exec "$@"
Then use it in
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"]
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.
.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.
- Tips on Writing Dockerfile, Reduce Size and Build Time of an Image
- Tips on using Docker Compose, Build Argument and Environment Variable
- Official Documentation on Builder: https://docs.docker.com/engine/reference/builder/
- Official Documentation on Volume: https://docs.docker.com/storage/volumes/
Last updated: Apr 30, 2021