In the quickstart: Compose and Django page in the docker-compose documentation, both the Dockerfile
and docker-compose.yml
files add .
as /code
, like this:
ADD . /code/
And a few lines later:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
I'm guessing this is done so that when in development, you wouldn't constantly have to rebuild the image, and when using docker build
the image is built with the source code already inside, so that in production, you could simply do docker run
to get a container up. Is that correct?
If so, the question is, how can I create a docker-compose.yml
to either attach a volume or don't do it based on whether I'm creating a production or a development stack?
Docker Compose is an excellent tool for optimizing the process of creating development, testing, staging, and production environments. With Docker Compose, you'll use a single file to build your environment instead of several files with complex scripts with a lot of branching logic.
To mount a data volume to a container add the --mount flag to the docker run command. It adds the volume to the specified container, where it stores the data produced inside the virtual environment. Replace [path_in_container] with the path where you want to place the data volume in the container.
Data volumes can be shared across containers too. They could be mounted in read-only mode too.
Docker may speed up your development process significantly, but not necessarily your app itself. Although it helps with making your application scalable, so more users will be able to use it, the single instance of your app will usually be just a hint slower than without Docker.
Minutes after posting this question, I came upon a page that had a few tips on how to do this. Here is how I think it must be done:
docker-compose.yml
that attaches the volume. However, in your Dockerfile
you still keep the original ADD
or COPY
instruction.You also define a production.yml
file which doesn't include the similarities between your development and production systems. However, it does include the changes you want to make in your production environment. For example, the volumes
section would probably be like this:
volumes: {}
Now, when you run docker-compose
in production, you do it like this:
$ docker-compose -f docker-compose.yml -f production.yml up
Here is the link to Using Compose in Production for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With