Skip to content

Instantly share code, notes, and snippets.

@bahmutov
Last active February 9, 2024 07:55
Show Gist options
  • Save bahmutov/1003fa86980dda147ff6 to your computer and use it in GitHub Desktop.
Save bahmutov/1003fa86980dda147ff6 to your computer and use it in GitHub Desktop.

Revisions

  1. bahmutov revised this gist Jun 27, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Extra info.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    ## Tips & tricks

    Setup an alias to start Node container and map the current folder for quick isolated Linux environment.
    Add to `~/.alias` file

    ```
    # Creates Node container with mapped current folder and runs Bash shell
    alias node-box='docker run -v $PWD:/src -w /src -u node -it node /bin/bash'
    ```

    ## Additional information

    * [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
  2. bahmutov revised this gist Mar 13, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions troubleshooting.md
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,10 @@ IDS=$(docker ps -q)
    docker stop $IDS
    # or as a single command
    docker stop $(docker ps -q)
    ```

    If you have a container "demo" and would like to investigate it, start it in the interactive mode with a "bash" as entry

    ```sh
    docker run -it --entrypoint bash demo
    ```
  3. bahmutov revised this gist Oct 10, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,5 @@
    * If you have multiple Docker files, you can pick the one to use during build `docker build -f <name> .`
    * Good video from NodeSource [Need to Node: Getting Started with Node.js, Docker and Kubernetes](https://vimeo.com/171803492)
    * You can run very simple process for managing system signals inside the docker container
    and that can start your actual process. [yelp/dumb-init](https://github.com/yelp/dumb-init)
    and that can start your actual process. [yelp/dumb-init](https://github.com/yelp/dumb-init)
    * [8 Protips to Start Killing It When Dockerizing Node.js](https://nodesource.com/blog/8-protips-to-start-killing-it-when-dockerizing-node-js/) includes how to make separate user, caching, etc.
  4. bahmutov revised this gist Sep 20, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,10 @@ node 5.0 c4f955829812 10 weeks ago
    $ docker run --name demo -p 5000:1337 -d gleb/demo-app
    9f9f3ae62038805504c3c23cce4e9229008ba6bd9ea16b560a7a9e1cfa932e57

    # A good security practice is to run the Docker image (if possible) in read-only mode
    # by adding --read-only flag
    # See "Docker security" talk at mark 22:00 https://www.youtube.com/watch?v=oANurUSaOFs

    # Run docker image with a folder from HOST machine mounted
    $ docker run -v /usr/source:/destination --name demo -d gleb/demo-app
    # inside the container /destination folder will be pointing at /usr/source from the HOST
  5. bahmutov revised this gist Jul 21, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions troubleshooting.md
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,8 @@ To delete all running containers and images (use `-f` option to force)
    #!/bin/bash
    # Delete all containers
    docker rm $(docker ps -a -q)
    # Delete all unused images
    docker rmi $(docker images --filter dangling=true)
    # Delete all images
    docker rmi $(docker images -q)
    ```
  6. bahmutov revised this gist Jul 20, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions troubleshooting.md
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,13 @@ To delete all running containers and images (use `-f` option to force)
    docker rm $(docker ps -a -q)
    # Delete all images
    docker rmi $(docker images -q)
    ```

    To stop all running containers

    ```sh
    IDS=$(docker ps -q)
    docker stop $IDS
    # or as a single command
    docker stop $(docker ps -q)
    ```
  7. bahmutov revised this gist Jul 12, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)
    * If you have multiple Docker files, you can pick the one to use during build `docker build -f <name> .`
    * Good video from NodeSource [Need to Node: Getting Started with Node.js, Docker and Kubernetes](https://vimeo.com/171803492)
    * Good video from NodeSource [Need to Node: Getting Started with Node.js, Docker and Kubernetes](https://vimeo.com/171803492)
    * You can run very simple process for managing system signals inside the docker container
    and that can start your actual process. [yelp/dumb-init](https://github.com/yelp/dumb-init)
  8. bahmutov revised this gist Jul 11, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)
    * If you have multiple Docker files, you can pick the one to use during build `docker build -f <name> .`
    * If you have multiple Docker files, you can pick the one to use during build `docker build -f <name> .`
    * Good video from NodeSource [Need to Node: Getting Started with Node.js, Docker and Kubernetes](https://vimeo.com/171803492)
  9. bahmutov revised this gist Jul 11, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,12 @@
    # start with node 5 base image
    FROM node:5.0

    # run as non-root user inside the docker container
    # see https://vimeo.com/171803492 at 17:20 mark
    RUN groupadd -r nodejs && useradd -m -r -g nodejs nodejs
    # now run as new user nodejs from group nodejs
    USER nodejs

    # Create an app directory (in the Docker container)
    RUN mkdir -p /usr/src/demo-server
    WORKDIR /usr/src/demo-server
  10. bahmutov revised this gist Jun 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)
    *
    * If you have multiple Docker files, you can pick the one to use during build `docker build -f <name> .`
  11. bahmutov revised this gist May 12, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions troubleshooting.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    ## Troubleshooting

    If you open a new terminal, you need the Docker environment variables again. You can set them up by running
    If you open a new terminal, you need the Docker environment variables again. You can set them up in
    the current shell by running (assuming the name `default` for the Docker machine)

    ```sh
    eval "$(docker-machine env -u)"
    eval "$(docker-machine env default)"
    ```

    If the Docker machine becomes unresponsive with status `UNKNOWN` when running `docker-machine ls` try rebuilding it
  12. bahmutov revised this gist May 12, 2016. 2 changed files with 25 additions and 19 deletions.
    21 changes: 2 additions & 19 deletions Extra info.md
    Original file line number Diff line number Diff line change
    @@ -6,22 +6,5 @@
    * [Docker cheat sheet](https://github.com/wsargent/docker-cheat-sheet)
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)

    ## Troubleshooting

    If the Docker machine becomes unresponsive with status `UNKNOWN` when running `docker-machine ls` try rebuilding it

    ```sh
    $ docker-machine rm -y default && docker-machine create -d virtualbox default && eval $(docker-machine env)
    ```

    To delete all running containers and images (use `-f` option to force)

    ```sh
    #!/bin/bash
    # Delete all containers
    docker rm $(docker ps -a -q)
    # Delete all images
    docker rmi $(docker images -q)
    ```
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)
    *
    23 changes: 23 additions & 0 deletions troubleshooting.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ## Troubleshooting

    If you open a new terminal, you need the Docker environment variables again. You can set them up by running

    ```sh
    eval "$(docker-machine env -u)"
    ```

    If the Docker machine becomes unresponsive with status `UNKNOWN` when running `docker-machine ls` try rebuilding it

    ```sh
    $ docker-machine rm -y default && docker-machine create -d virtualbox default && eval $(docker-machine env)
    ```

    To delete all running containers and images (use `-f` option to force)

    ```sh
    #!/bin/bash
    # Delete all containers
    docker rm $(docker ps -a -q)
    # Delete all images
    docker rmi $(docker images -q)
    ```
  13. bahmutov revised this gist May 12, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Extra info.md
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,14 @@ If the Docker machine becomes unresponsive with status `UNKNOWN` when running `d

    ```sh
    $ docker-machine rm -y default && docker-machine create -d virtualbox default && eval $(docker-machine env)
    ```

    To delete all running containers and images (use `-f` option to force)

    ```sh
    #!/bin/bash
    # Delete all containers
    docker rm $(docker ps -a -q)
    # Delete all images
    docker rmi $(docker images -q)
    ```
  14. bahmutov revised this gist May 5, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,7 @@ listening on port 1337 { subdomainOffset: 2, proxy: false, env: 'development' }
    started server
    <-- GET /
    --> GET / 404 6ms -
    # you can follow the logs along using -f (--follow) option

    # Jump into the running container to run any commands
    # -i option means bind STDIO from the current shell
  15. bahmutov revised this gist May 5, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,17 @@ $ docker exec -t demo ls
    docker cp demo:/usr/src/demo-server/file.txt file.txt
    # look at file.txt locally

    # Running Docker container in terminal
    # If you want to see the output / enter commands *inside the container* right from the terminal
    # just skip -d option (-d is for detached mode)
    $ docker run --name demo -it gleb/demo-app

    # if the container stops (like the command terminates due to error)
    # a nice trick is to run the container with dummy infinite command
    # then shell into the container and find the problem
    $ docker run --name demo -d gleb/demo-app tail -f /dev/null
    $ docker exec -it demo bash

    # Nice!
    # To stop the running container
    $ docker stop demo
  16. bahmutov revised this gist May 4, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,10 @@ node 5.0 c4f955829812 10 weeks ago
    $ docker run --name demo -p 5000:1337 -d gleb/demo-app
    9f9f3ae62038805504c3c23cce4e9229008ba6bd9ea16b560a7a9e1cfa932e57

    # Run docker image with a folder from HOST machine mounted
    $ docker run -v /usr/source:/destination --name demo -d gleb/demo-app
    # inside the container /destination folder will be pointing at /usr/source from the HOST

    # Note: you can pass environment variable values to the `docker run` command
    # docker run -e USER=name
    # or, if the USER is already an environment var
  17. bahmutov revised this gist May 2, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,12 @@
    * [Docker cheat sheet](https://github.com/wsargent/docker-cheat-sheet)
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)

    ## Troubleshooting

    If the Docker machine becomes unresponsive with status `UNKNOWN` when running `docker-machine ls` try rebuilding it

    ```sh
    $ docker-machine rm -y default && docker-machine create -d virtualbox default && eval $(docker-machine env)
    ```
  18. bahmutov revised this gist Feb 14, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,5 @@
    * [Node Dockerhub images](https://hub.docker.com/_/node/) - list of Docker images with different Node versions
    * [Docker cheat sheet](https://github.com/wsargent/docker-cheat-sheet)
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
    * [Run Node in a docker container with file access](https://gist.github.com/bahmutov/448f73b49914d1981643)
  19. bahmutov revised this gist Feb 5, 2016. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -51,9 +51,14 @@ started server
    # Jump into the running container to run any commands
    # -i option means bind STDIO from the current shell
    docker exec -it demo bash
    root@9f9f3ae62038:/usr/usr/demo-server# ls
    root@9f9f3ae62038:/usr/src/demo-server# ls
    ... list of files
    root@9f9f3ae62038:/usr/usr/demo-server# exit
    root@9f9f3ae62038:/usr/src/demo-server# exit

    # If you want to quickly list files in the docker container (default folder)
    # you can use `docker exec`
    $ docker exec -t demo ls
    # ... list of files in /usr/src folder

    # We can even copy files from the container back into our system
    docker cp demo:/usr/src/demo-server/file.txt file.txt
  20. bahmutov revised this gist Feb 5, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -26,6 +26,8 @@ $ docker run --name demo -p 5000:1337 -d gleb/demo-app
    # docker run -e USER
    # or put all env variables into a file and pass its name
    # docker run --env-file=<filename>
    # You can check all the options passed into the running container
    # docker inspect demo

    # It prints the long container ID, but we can use our name "demo"
    # We also mapped outside port 5000 to container's exposed port 1337
  21. bahmutov revised this gist Feb 5, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -53,6 +53,10 @@ root@9f9f3ae62038:/usr/usr/demo-server# ls
    ... list of files
    root@9f9f3ae62038:/usr/usr/demo-server# exit

    # We can even copy files from the container back into our system
    docker cp demo:/usr/src/demo-server/file.txt file.txt
    # look at file.txt locally

    # Nice!
    # To stop the running container
    $ docker stop demo
  22. bahmutov revised this gist Feb 5, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,13 @@ started server
    <-- GET /
    --> GET / 404 6ms -

    # Jump into the running container to run any commands
    # -i option means bind STDIO from the current shell
    docker exec -it demo bash
    root@9f9f3ae62038:/usr/usr/demo-server# ls
    ... list of files
    root@9f9f3ae62038:/usr/usr/demo-server# exit

    # Nice!
    # To stop the running container
    $ docker stop demo
  23. bahmutov revised this gist Feb 5, 2016. No changes.
  24. bahmutov revised this gist Feb 4, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Extra info.md
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,6 @@
    * [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
    * [NodeUp Docker podcast episode](http://nodeup.com/ninetyeight)
    * [Node Dockerhub images](https://hub.docker.com/_/node/) - list of Docker images with different Node versions
    * [Docker cheat sheet](https://github.com/wsargent/docker-cheat-sheet)
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
  25. bahmutov revised this gist Feb 4, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Extra info.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    ## Additional information

    * [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
    * [NodeUp Docker podcast episode](http://nodeup.com/ninetyeight)
    * [Node Dockerhub images](https://hub.docker.com/_/node/) - list of Docker images with different Node versions
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
  26. bahmutov revised this gist Feb 4, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,13 @@ node 5.0 c4f955829812 10 weeks ago
    $ docker run --name demo -p 5000:1337 -d gleb/demo-app
    9f9f3ae62038805504c3c23cce4e9229008ba6bd9ea16b560a7a9e1cfa932e57

    # Note: you can pass environment variable values to the `docker run` command
    # docker run -e USER=name
    # or, if the USER is already an environment var
    # docker run -e USER
    # or put all env variables into a file and pass its name
    # docker run --env-file=<filename>

    # It prints the long container ID, but we can use our name "demo"
    # We also mapped outside port 5000 to container's exposed port 1337
    # Let us see running containers
  27. bahmutov revised this gist Feb 4, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Extra info.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ## Additional information

    * [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
    * [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
    * [fuge](https://github.com/apparatus/fuge) - Node microservices and sites with nice development workflow
    - [Microservices with fuge](https://glebbahmutov.com/blog/microservices-with-fuge/) - my blog post showing local development workflow
  28. bahmutov revised this gist Feb 4, 2016. 2 changed files with 4 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion Docker shell commands.sh
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ default * virtualbox Running tcp://192.168.99.100:2376 v1
    # Note the host URL 192.168.99.100 - it will be used later!

    # Build an image from current folder under given image name
    docker build -t gleb/demo-app .
    $ docker build -t gleb/demo-app .

    # see list of build images
    $ docker images
    3 changes: 3 additions & 0 deletions Extra info.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    ## Additional information

    * [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
  29. bahmutov renamed this gist Feb 4, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  30. bahmutov revised this gist Feb 4, 2016. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # simple docker file
    # assumes located in the same folder as the application itself

    # start with node 5 base image
    FROM node:5.0

    # Create an app directory (in the Docker container)
    RUN mkdir -p /usr/src/demo-server
    WORKDIR /usr/src/demo-server

    # Copy .npm settings and package.json into container
    COPY package.json /usr/src/demo-server
    COPY .npmrc /usr/src/demo-server
    # and install dependencies
    RUN npm install

    # Copy our source into container
    COPY src /usr/src/demo-server/src
    COPY server.js /usr/src/demo-server

    # If our server uses 1337 by default
    # expose it from Docker container
    EXPOSE 1337

    # Finally start the container command
    CMD ["node", "server.js"]