Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach volumes with docker-compose only when in the development environment?

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?

like image 721
Parham Doustdar Avatar asked Apr 30 '16 15:04

Parham Doustdar


People also ask

Is Docker compose only for development?

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.

How do you attach a volume to docker container?

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.

Can data volumes can be mounted in read only mode?

Data volumes can be shared across containers too. They could be mounted in read-only mode too.

Should I use docker while developing?

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.


1 Answers

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:

  1. You define a docker-compose.yml that attaches the volume. However, in your Dockerfile you still keep the original ADD or COPY instruction.
  2. 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.

like image 179
Parham Doustdar Avatar answered Oct 16 '22 09:10

Parham Doustdar