Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose up not adding code to remote container

I've run through the initial Overview of Docker Compose exactly as written and it works just fine locally with boot2docker. However, if I try to do a docker-compose up on a remote host, it does not add the code to the remote container.

To reproduce:

  1. Run through the initial Overview of Docker Compose exactly as written.
  2. Install Docker Machine and start a Dockerized VM on any cloud provider.
    1. docker-machine create --driver my-favourite-cloud composetest
    2. eval "$(docker-machine env composetest)"
  3. Now that you're working with a remote host, run docker-compose up on the original code.
    1. composetest $ docker-compose up

Redis runs fine but the Flask app does not.

composetest $ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS                          PORTS               NAMES
794c90928b97        composetest_web     "/bin/sh -c 'python    About a minute ago   Exited (2) About a minute ago                       composetest_web_1
2c70bd687dfc        redis               "/entrypoint.sh redi   About a minute ago   Up About a minute               6379/tcp            composetest_redis_1

What went wrong?

composetest $ docker logs 794c90928b97
python: can't open file 'app.py': [Errno 2] No such file or directory

Can we confirm it?

composetest $ docker-compose run -d  web sleep 60
Starting composetest_redis_1...
composetest_web_run_3

composetest $ docker exec -it a4 /bin/bash
root@a4a73c6dd159:/code# ls -a
.  ..

Nothing there. Can we fix it?

Comment out volumes in docker-compose.yml

web:
  build: .
  ports:
   - "5000:5000"
  # volumes:
  #  - .:/code
  links:
   - redis
redis:
  image: redis

Then just docker-compose up and it works!

Let's try again on boot2docker.

composetest $ eval "$(boot2docker shellinit)"
composetest $ docker-compose up
Recreating composetest_redis_1...
Recreating composetest_web_1...
Attaching to composetest_redis_1, composetest_web_1
...

The Flask app does work but it has a serious problem. If you change app.py, the Flask dev server doesn't reload and those changes aren't automatically seen. Even if you stop the container and docker-compose up again, the changes still aren't seen. I realize we lose this essential feature because the volume is no longer mounted. But not mounting the volume is the only way I've been able to get docker-compose to work with a remote host. We should be able to get both local and remote hosts to work using the same docker-compose.yml and Dockerfile.

How do I develop interactively with a local VM and deploy to a remote VM without having to change the Dockerfile or docker-compose.yml?

Versions:

  • Docker 1.7.0
  • Docker Compose 1.3.0
  • Docker Machine 0.3.0
like image 224
Everett Toews Avatar asked Jun 22 '15 01:06

Everett Toews


1 Answers

Compose just does pretty much the same thing as you can do with the regular command line interface under the hood. So your command is roughly equivalent to:

$ docker run --name web -p 5000:5000 -v $(pwd):/code --link redis:redis web

The issue is that the volume is relative to the docker host, not the client. So it will mount the working directory on the remote VM, not the client. In your case, this directory is empty.

If you want to develop interactively with a remote VM, you will have to check out the source and edit the files on the VM.

UPDATE: It seems that you actually want to develop and test locally, then deploy a production version to a remote VM. (Apologies if I still misunderstand). To do this, I suggest you have a separate Compose file for development where you mount the local volume, then rebuild and deploy the image for production. By rebuilding the image, it will pick up the latest version of the code. Mounting a volume in production breaks because you've hidden the code in the image with an empty directory.

It's also worth pointing out that Docker don't advise using Compose in production currently.

like image 153
Adrian Mouat Avatar answered Sep 23 '22 03:09

Adrian Mouat