Last active
February 9, 2024 07:55
-
Star
(200)
You must be signed in to star a gist -
Fork
(64)
You must be signed in to fork a gist
-
-
Save bahmutov/1003fa86980dda147ff6 to your computer and use it in GitHub Desktop.
A personal cheat sheet for running local Node project in a Docker container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # See list of docker virtual machines on the local box | |
| $ docker-machine ls | |
| NAME ACTIVE URL STATE URL SWARM DOCKER ERRORS | |
| default * virtualbox Running tcp://192.168.99.100:2376 v1.9.1 | |
| # 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 . | |
| # see list of build images | |
| $ docker images | |
| REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE | |
| gleb/demo-app latest 506bf31537d4 17 minutes ago 904.5 MB | |
| node 5.0 c4f955829812 10 weeks ago 642.2 MB | |
| # Note both the original Node:5 and the built images are there | |
| # Let us run the image under name 'demo' | |
| $ docker run --name demo -p 5000:1337 -d gleb/demo-app | |
| 9f9f3ae62038805504c3c23cce4e9229008ba6bd9ea16b560a7a9e1cfa932e57 | |
| # 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 | |
| $ docker ps | |
| CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |
| 9f9f3ae62038 gleb/demo-app "node server.js" About a minute ago Up About a minute 0.0.0.0:5000->1337/tcp demo | |
| # Let us make a dummy request to the app running inside the container | |
| # We will use the virtual machine's IP and outside port 5000 | |
| $ curl 192.168.99.100:5000 | |
| Not Found | |
| # Let us see the app's console log to confirm that it has received our request | |
| $ docker logs demo | |
| listening on port 1337 { subdomainOffset: 2, proxy: false, env: 'development' } | |
| started server | |
| <-- GET / | |
| --> GET / 404 6ms - | |
| # Nice! | |
| # To stop the running container | |
| $ docker stop demo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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"] |
thanks man, really helpful 👍
Is it possible to enter a docker container and interact with it through a command-line node application?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just great.! thanks a lot!