June 12, 2018

The Working Directory

We cover some more about using the working_dir option.

We cover some more about working directories, the exec vs run command, and update our docker-compose.yml file.

If our exec command doesn't work with a working directory setting, we can run a command that cd's into a directory and runs the command we want all in one shot:

docker exec -it app bash -c "cd /var/www/html && php artisan list"

We can update our docker-compose.yml file to add a working_dir. This should let us avoid doing the above "hack"!

version: '3'
services:
  app:
    build:
      context: ./docker/app
      dockerfile: Dockerfile
    image: shippingdocker/app:latest
    networks:
     - appnet
    volumes:
     - ./application:/var/www/html
    working_dir: /var/www/html
  cache:
    image: redis:alpine
    networks:
     - appnet
    volumes:
     - cachedata:/data
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
    networks:
     - appnet
    volumes:
     - dbdata:/var/lib/mysql
networks:
  appnet:
    driver: bridge
volumes:
  dbdata:
    driver: local
  cachedata:
    driver: local

Then commands like:

docker-compose exec app php artisan list

...should just work!

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics