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

Docker for Vue.js Developers

Docker for Vue.js Developers

This was a talk given by me at the online VueBLR meetup on 23rd May, 2020.

Docker is a wonderful new technology that's creating shifts in the way we build and deploy applications. Static websites are no exception. We explore how Docker can help us front-end developers, not only to iterate faster by creating images that contain our application and the necessary deployment server, but also, avoiding the "it works on my machine" model of development by ensuring repeatable development environments for all the members in a team. All this, without having to sacrifice the great affordances we enjoy such as hot-reloading.

Sangeeth Sudheer

May 23, 2020
Tweet

More Decks by Sangeeth Sudheer

Other Decks in Programming

Transcript

  1. Cool… but what is it? • Think of it like

    a bundle containing your application code as well as just the right amount system APIs and services—not the whole Operating System.
  2. Advantages • Lightweight • Less system resources required • Faster

    deployment cycle since it’s way easier to swap out old container image with a new one • Can be run using an orchestrator such as Kubernetes to rapidly scale up, run multiple containers at the same time and quickly failover
  3. No thanks, I’ll use Netlify. • Netlify is awesome! •

    What makes it even more awesome… is that it uses containers under-the-hood! https://github.com/netlify/build-image
  4. So… when do you use it? • You can’t use

    cool stuff like Netlify. • You need flexibility. • You need on-premises deployment. • Containers will make your lives a whole lot easier if the above points fit. • But the most exciting bit is: you can use it for development!
  5. We’ll split the use of Docker for two purposes (and

    maybe more): DOCKER FOR PRODUCTION DOCKER FOR DEVELOPMENT
  6. Ingredients • Docker Desktop installed on your machine (https:// www.docker.com/products/docker-desktop)

    • Dockerfile • .dockerignore (optional) • A cloud-provider that can run containers (pretty much everyone does!). We’ll use Heroku.
  7. Dockerfile FROM node:12.16.0 The base image from which you’re going

    to build. WORKDIR /app Set the working directory. Commands like COPY will then respect this setting. COPY <from> <to> Copy files from <from> into the image in the location provided by <to>.
  8. Dockerfile RUN yarn install Run any arbitrary command. This result

    in the creation of a new layer. CMD [“yarn”, “start”] This sets the default command that should be invoked when the container is started. https://docs.docker.com/engine/reference/builder/
  9. docker container ls [#--all] Shows all the running container instances

    in your machine. Pass --all to see even the containers that are stopped. docker image ls Shows all the docker images you have on your machine. docker top <container-name> Shows all the running processes inside a container.
  10. docker run [-it] <image-name> Creates an instance of a container

    from the image name provided. The image has to exist for this command to work. docker build -t <image-name> . Builds a docker image based on the Dockerfile in the current directory. docker exec [-it] <container-name> <command> <args> Runs the given command inside given container instance that’s running at the moment.
  11. docker stop <container-name> Gracefully stops the running container. There’s a

    start command to run a stopped container as well. docker kill <container-name> Tries to gracefully stop and if it doesn’t work, forcefully kills and removes the container instance.
  12. docker container rm <container-name> Removes the stopped container instance. docker

    image rm <image-name> Removes the image from your machine. docker image prune docker container prune Removes all unused images. Removes all stopped container instances. If you’re running low on space, these are your friends.
  13. Some tips • Always include a .dockerignore file and add

    directories like .git and node_modules in it. • Make sure you install your dependencies first and in the end, copy over your application files in order to get best caching. • Don’t include any script to restart the application if it files. i.e, don’t use tools like nodemon/pm2 inside Docker since your orchestration engine takes care of that for you anyway. • Use lightweight images like alpine for production. You can use multi-stage docker builds to achieve this.
  14. I’m pumped! Guide me! • Stephen Grider’s excellent Udemy course

    is a must watch
 https://www.udemy.com/course/docker-and-kubernetes- the-complete-guide/ • Katacoda
 https://www.katacoda.com/ • Docker docs
 https://docs.docker.com/ • Read about docker-compose
 https://docs.docker.com/compose/ • Vue.js Cookbook
 https://vuejs.org/v2/cookbook/dockerize-vuejs-app.html