Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker Compose for Rails Developer

offz
April 20, 2017

Docker Compose for Rails Developer

A cheat sheet for RoR developers who want to head start using Docker for their development.

offz

April 20, 2017
Tweet

More Decks by offz

Other Decks in Programming

Transcript

  1. docker-compose.yml version: '2' services: web: build: . depends_on: - db

    volumes: - .:/usr/local/src ports: - 3000:3000 environment: - AWS_S3_KEY=6t87yagsdifhj db: image: postgres volumes: - ./data:/var/lib/postgresql/data ports: - 5432:5432
  2. docker-compose.yml version: '2' services: <SERVICE_1_NAME>: build: <DOCKERFILE_PATH> depends_on: - <SERVICE_X_NAME>

    volumes: - <HOST_PATH>:<CONTAINER_PAHT> ports: - <HOST_PORT>:<CONTAINER_PORT> environment: - <ENV_VAR_NAME>=<ENV_VAR_VALUE> <SERVICE_X_NAME>: image: <IMAGE_NAME> volumes: - <HOST_PATH>:<CONTAINER_PAHT> ports: - <HOST_PORT>:<CONTAINER_PORT>
  3. build • create new image • use cached image when

    possible • relative path from the docker-compose.yml • require Dockerfile at the path
  4. depends_on • all services in depends_on will be starting before

    the particular service • auto create dns record for the dependent service (http://db:5432)
  5. volumes • link host folder into container • any file

    change on host will effect file inside the container • any file change inside the container will change the file on the host • files will be persisted even the container is stopped
  6. ports • allow the container’s port being access via host

    port • could be different port number • could not be the port being used on host • could be range of port
  7. ports vs. expose • expose allows the container communicate between

    containers • port mapping allows outside traffic to communicate with the container
  8. image • pull image from docker hub • use cached

    image when possible • auto start the container with CMD or ENTRYPOINT specified in that image • use all configurations from that image • configurations will be overwritten by commands specified in docker-compose.yml
  9. db • image name = postgres • database default username

    = postgres • data directory = /var/lib/postgresql/data • there is no default password • port 5432 is exposed automatically
  10. web • built locally • base on some version of

    ruby image • depends on db, so we have db started before the Rails app • map host port 3000 into container port 3000, so we can access the web app from browser
  11. worker • use the same image as web • depends

    on web, so we let the web image builds first, then also start the worker container from it
  12. es • image name = elasticsearch • ports 9200 and

    9300 are exposed automatically • no need to mount volume, we can reindex the data easily
  13. redis • image name = redis • ports 6379 is

    exposed automatically • no need to mount volume, everything is in the RAM anyway
  14. run • start a new container with the command •

    using the service’s configurations • attach the terminal to that container • the container will be stopped on exit (not destroy)
  15. logs • show logs from all services • supply -f

    option to show logs live streaming • supply the service name to show logs from that service only
  16. up • pull/build images and start all services in docker-compose.yml

    • use cached images when possible • supply -d option to make it run in background
  17. start • start the stopped containers • does not load

    images • does not create the containers from images • container state will be the same
  18. down • stop and remove all running containers • also

    remove containers started from run command • does not remove cached images
  19. stop • stop all running containers • does not remove

    containers • container state will stay untouched • does not effect images
  20. ZSH Plugin • plugin name: docker-compose • use dco instead

    of docker-compose • add autocomplete for the command