{"componentChunkName":"component---src-templates-blog-js","path":"/blog/docker-quick-start","result":{"data":{"contentfulBlogPost":{"title":"Docker Quick Start","publishedDate":"December 25th, 2020","body":{"json":{"nodeType":"document","data":{},"content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"heading-1","content":[{"nodeType":"text","value":"What is Docker","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Docker is a tool that allows developers to create, deploy, and run their applications by using the power of containers. Containers provide developers all the packages and dependencies to make their application run on a sandboxed environment. Unlike a VM a container doesn't contain its own kernel, it uses the host OS's resources. ","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Further information about the can be found ","marks":[],"data":{}},{"nodeType":"hyperlink","content":[{"nodeType":"text","value":"here","marks":[],"data":{}}],"data":{"uri":"https://www.docker.com/why-docker"}},{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"heading-3","content":[{"nodeType":"text","value":"Quick Start with a Dockerfile","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Even though it's a little bit top to bottom I'll explain docker cli commands that I found useful later.","marks":[],"data":{}}],"data":{}},{"nodeType":"hr","content":[],"data":{}},{"nodeType":"heading-3","content":[{"nodeType":"text","value":"Dependencies","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"I'm using the Docker Desktop application on MacOS but all the files and commands are independent of your way of getting Docker. You can find how can you start ","marks":[],"data":{}},{"nodeType":"hyperlink","content":[{"nodeType":"text","value":"here","marks":[],"data":{}}],"data":{"uri":"https://www.docker.com/get-started"}},{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Another dependency we need is the Node on our machine.","marks":[],"data":{}}],"data":{}},{"nodeType":"hr","content":[],"data":{}},{"nodeType":"heading-3","content":[{"nodeType":"text","value":"Base Files","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"We should create a folder and navigate into it. After the following commands need to be executed one by one.","marks":[],"data":{}}],"data":{}},{"nodeType":"unordered-list","content":[{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"npm init -y","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"npm install express","marks":[],"data":{}}],"data":{}}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Later we should add that line to \"package.json\" to go along with this tutorial","marks":[],"data":{}}],"data":{}},{"nodeType":"unordered-list","content":[{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"  \"scripts\": {    \"start\": \"node src/index.js\"  }","marks":[{"type":"code"}],"data":{}}],"data":{}}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"We also need to create an index.js file in the root directory with the following content:","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"const express = require(\"express\");\nconst app = express();","marks":[{"type":"code"}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"\napp.get(\"/\", (req, res) => {  res.json({ message: \"Dockerized basic NodeApp\" });});","marks":[{"type":"code"}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"\nconst port = process.env.PORT || 8000;\napp.listen(port, () => {  console.log(`The app is up at PORT ${port}`);});","marks":[{"type":"code"}],"data":{}},{"nodeType":"text","value":"\n","marks":[],"data":{}}],"data":{}},{"nodeType":"hr","content":[],"data":{}},{"nodeType":"heading-3","content":[{"nodeType":"text","value":"Dockerfile","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"In the root directory, we should create a \"Dockerfile\". The \"Dockerfile\" is a special file that our \"docker run\" commands will take a look into. We also need to create a file called \".dockerignore\" which is familiar to the \".gitignore\" file. Add the line \"","marks":[],"data":{}},{"nodeType":"text","value":"node_modules","marks":[{"type":"code"}],"data":{}},{"nodeType":"text","value":"\" into \".dockerignore\" file. ","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Docker file is the most critical file for our application. And the content should look like that.","marks":[],"data":{}}],"data":{}},{"nodeType":"embedded-asset-block","content":[],"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c53EEjiYK264O7eHuCMZ826","type":"Asset","createdAt":"2020-12-25T21:28:19.183Z","updatedAt":"2020-12-25T21:28:19.183Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"53EEjiYK264O7eHuCMZ826"},"fields":{"title":{"en-US":"Dockerfilebasic"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/53EEjiYK264O7eHuCMZ826/cf20ea8a1d3c6d3766f25c975b0ad89c/Screen_Shot_2020-12-25_at_22.27.38.png","details":{"size":35513,"image":{"width":338,"height":462}},"fileName":"Screen Shot 2020-12-25 at 22.27.38.png","contentType":"image/png"}}}}}},{"nodeType":"unordered-list","content":[{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"FROM command indicates what our base image is, if we don't have the specified node version in our computer \"docker run\" command will fire a \"docker pull\" command under the hood and pull it from the Docker Hub. If we have it the layer will be just pointed to the actual node image.","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"WORKDIR is where we specify our working directory.","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"With COPY command we can copy any file from our system to our container image to be. In this example package*.json from our system and ./ indicates the location that we want our package*.json file in.","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"RUN command runs npm install command.","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"COPY command again copies everything from \".\"(source) to \".\"(destination)","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"ENV command lets us define environment variables, in this case we define our port for our webserver.","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"EXPOSE lets us use that port 8080 to interract with program's interface.","marks":[],"data":{}}],"data":{}}],"data":{}},{"nodeType":"list-item","content":[{"nodeType":"paragraph","content":[{"nodeType":"text","value":"CMD command executes a command with the given parameters in the string array next to it.","marks":[],"data":{}}],"data":{}}],"data":{}}],"data":{}},{"nodeType":"hr","content":[],"data":{}},{"nodeType":"heading-3","content":[{"nodeType":"text","value":"Docker run","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"After everything is set we can create our docker image by running:","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"docker build -t <ourdesirednameforourimage> .","marks":[{"type":"code"}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"After this command is successful we can check our images by running the command ","marks":[],"data":{}},{"nodeType":"text","value":"docker images ","marks":[{"type":"code"}],"data":{}},{"nodeType":"text","value":"and the output should be something like this:","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"REPOSITORY           TAG       IMAGE ID            CREATED        SIZE\n<ourtag>             1.0       randomimageid      2 hours ago    971MB","marks":[{"type":"code"}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"We can finally run our container by running the command:","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"docker run -p 5000:8080 <image id or the tag we set here>","marks":[{"type":"code"}],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"and that's it we can interact with our app from the port we indicated with the -p flag above.  ","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"Given our index.js code output should be something like that when we open the page \"http://localhost:5000/\"","marks":[],"data":{}}],"data":{}},{"nodeType":"embedded-asset-block","content":[],"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"LggbC93yc7pObaHxNrr42","type":"Asset","createdAt":"2020-12-25T22:02:47.828Z","updatedAt":"2020-12-25T22:02:47.828Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"LggbC93yc7pObaHxNrr42"},"fields":{"title":{"en-US":"docker-basic-live"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/LggbC93yc7pObaHxNrr42/b3426b94298879712e4938eefec910e2/Screen_Shot_2020-12-25_at_23.00.12.png","details":{"size":17869,"image":{"width":738,"height":142}},"fileName":"Screen Shot 2020-12-25 at 23.00.12.png","contentType":"image/png"}}}}}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"","marks":[],"data":{}}],"data":{}},{"nodeType":"paragraph","content":[{"nodeType":"text","value":"I hope you all enjoyed this tutorial as much as I did, see you in the more depth tutorials in the future...","marks":[],"data":{}}],"data":{}}]}}}},"pageContext":{"slug":"docker-quick-start"}}}