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

Docker (for developers)

Docker (for developers)

Some cool things about docker and docker-compose for a development environment. Talk at Auckland Node.js Meetup

Dmitry Rocha

May 31, 2018
Tweet

More Decks by Dmitry Rocha

Other Decks in Technology

Transcript

  1. “ Docker is a computer program that performs operating-system-level virtualization

    also known as containerization. It is developed by Docker, Inc. Docker is primarily developed for Linux, where it uses the resource isolation features of the Linux kernel such as cgroups and kernel namespaces, and a union-capable file system such as OverlayFS and others to allow independent ”containers” to run within a single Linux instance, avoiding the overhead of starting and maintaining virtual machines (VMs) ” ([Docker (software)Docker (software), Docker])
  2. 1.85KB 1 $ docker run --rm hello-world 2 3 Hello

    from Docker! 4 This message shows that your installation appears to be working correctly. 5 6 To generate this message, Docker took the following steps: 7 1. The Docker client contacted the Docker daemon. 8 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 9 (amd64) 10 3. The Docker daemon created a new container from that image which runs the 11 executable that produces the output you are currently reading. 12 4. The Docker daemon streamed that output to the Docker client, which sent it 13 to your terminal. 14 15 To try something more ambitious, you can run an Ubuntu container with: 16 $ docker run -it ubuntu bash 17 18 Share images, automate workflows, and more with a free Docker ID: 19 https://hub.docker.com/ 20 21 For more examples and ideas, visit: 22 https://docs.docker.com/engine/userguide/ 23 24 $ Listing 1: docker run --rm hello-world
  3. ±100MB 1 $ docker run --rm -it ubuntu bash 2

    root@8140206e64bc:/# exit 3 $ Listing 2: docker run --rm -it ubuntu bash
  4. 1 $ docker run -it ubuntu bash 2 root@919fe536b5c7:/# mkdir

    /testing 3 root@919fe536b5c7:/# ls -ld /testing 4 drwxr-xr-x 2 root root 4096 May 29 09:04 /testing 5 root@919fe536b5c7:/# exit 6 exit 7 $ docker run -it ubuntu bash 8 root@f78f3e0f874c:/# ls -ld /testing 9 ls: cannot access '/testing': No such file or directory 10 root@f78f3e0f874c:/# Listing 3: docker run -it ubuntu bash
  5. 1 $ docker run --rm -it ubuntu bash 2 root@0f98b2643c36:/#

    wget "https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar. → 3 bash: wget: command not found 4 root@0f98b2643c36:/# apt-get install wget 5 Reading package lists... Done 6 Building dependency tree 7 Reading state information... Done 8 E: Unable to locate package wget 9 root@0f98b2643c36:/# apt-get update 10 ……… 11 Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [1660 B] → 12 Fetched 25.2 MB in 12s (2070 kB/s) 13 Reading package lists... Done 14 root@0f98b2643c36:/# apt-get install -y wget 15 ……… 16 Done Listing 4: Install wget first :/
  6. 1 root@0f98b2643c36:/# wget "https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.xz" 2 --2018-05-29 09:51:32-- https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.xz 3 Resolving

    nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:162e, ... 4 Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected. 5 HTTP request sent, awaiting response... 200 OK 6 Length: 11349944 (11M) [application/x-xz] 7 Saving to: 'node-v8.11.2-linux-x64.tar.xz' 8 9 node-v8.11.2-linux-x64.tar.x 100%[=============================================>] 10.82M 502KB/s in 14s → 10 11 2018-05-29 09:51:47 (778 KB/s) - 'node-v8.11.2-linux-x64.tar.xz' saved [11349944/11349944] Listing 5: Download node
  7. 1 root@0f98b2643c36:/# tar xf node-v8.11.2-linux-x64.tar.xz 2 tar (child): xz: Cannot

    exec: No such file or directory 3 tar (child): Error is not recoverable: exiting now 4 tar: Child returned status 2 5 tar: Error is not recoverable: exiting now 6 root@0f98b2643c36:/# apt-get install -y xz-utils 7 ……… 8 Done 9 root@0f98b2643c36:/# Listing 6: Extract… ops, no extractor?
  8. 1 apt-get update 2 apt-get install -y wget 3 apt-get

    install -y xz-utils 4 tar xf node-v8.11.2-linux-x64.tar.xz 5 /node-v8.11.2-linux-x64/bin/node Listing 8: Everytime
  9. 1 from ubuntu 2 3 run apt-get update && \

    4 apt-get install -y wget && \ 5 apt-get install -y xz-utils && \ 6 wget "https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar. && \ → → 7 tar xf node-v8.11.2-linux-x64.tar.xz 8 9 run ln -s /node-v8.11.2-linux-x64/bin/npm /usr/local/bin/npm && \ → 10 ln -s /node-v8.11.2-linux-x64/bin/node /usr/local/bin/node → 11 12 run apt-get install -y postgresql-client Listing 9: Dockerfile
  10. docker ▶ ‘docker run --rm hello-world’ ▶ ‘docker build -t

    mynode .’ ▶ ‘docker run --rm -it mynode bash’ ▶ ‘docker run --rm -it -v /home/dmitry/myproject:/app mynode bash’ ▶ ‘docker run --rm -it -v /home/dmitry/myproject:/app -w /app mynode bash’ ▶ ‘docker run --rm -it -v /home/dmitry/myproject:/app -w /app -v node_modules:/app/node_modules mynode bash’
  11. docker-compose ▶ ‘docker-compose pull’ ▶ ‘docker-compose build’ ▶ ‘docker-composer run

    --rm web bash’ ▶ ‘docker-composer run --rm web bash -c ”psql -h db -U postgres”’
  12. 1 version: "3.3" 2 3 services: 4 app: 5 build:

    6 context: . 7 volumes: 8 - .:/app 9 - node_modules:/app/node_modules 10 working_dir: /app 11 links: 12 - db 13 14 db: 15 image: postgres 16 17 volumes: 18 node_modules: Listing 10: docker-compose.yml
  13. from ubuntu run apt-get update && \ apt-get install -y

    wget xz-utils && \ wget "https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar. && \ → → tar xf node-v8.11.2-linux-x64.tar.xz -C /opt && \ ln -s /opt/node-v8.11.2-linux-x64/bin/npm /usr/local/bin/npm && \ → ln -s /opt/node-v8.11.2-linux-x64/bin/node /usr/local/bin/node && \ → apt-get install -y postgresql-client && \ rm /node-v8.11.2-linux-x64.tar.xz && \ rm -rf /var/lib/apt/lists/* Listing 11: Dockerfile optimized