<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by J.P. on Medium]]></title>
        <description><![CDATA[Stories by J.P. on Medium]]></description>
        <link>https://medium.com/@linkwithdanmarcos?source=rss-d328e1df77ed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yL8yotv7NNy3IPC7vCEa6A.jpeg</url>
            <title>Stories by J.P. on Medium</title>
            <link>https://medium.com/@linkwithdanmarcos?source=rss-d328e1df77ed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:30:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@linkwithdanmarcos/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Getting To Know Docker | Part 4: The Database]]></title>
            <link>https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-4-the-database-3dbc25b1c320?source=rss-d328e1df77ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dbc25b1c320</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[J.P.]]></dc:creator>
            <pubDate>Wed, 20 Dec 2017 17:18:51 GMT</pubDate>
            <atom:updated>2017-12-22T16:30:34.425Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052">Part 2</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-3-introduction-to-docker-compose-and-volumes-b87f563e9550">Part 3</a> | You are about to read Part 4</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*kqFIAhHZ5cUSY97K_r5rgg.jpeg" /></figure><p>I am going to reset Docker and start fresh. You can do this in your Docker preferences. You don’t have to, but if you want a clean workspace here are <a href="https://docs.docker.com/docker-for-mac/#uninstall-or-reset">directions for resetting Docker</a>. It will remove all images, containers and volumes you have. So, just be sure you are okay with that.</p><p>After resetting Docker you should not see any images when running:</p><p>docker image ls</p><p>and no containers when running:</p><p>docker ps</p><p>Our <a href="https://github.com/Jp-Rivera/boogaloo">boogaloo</a> app environment is looking pretty good up to this point. We could build plenty of apps like this, but eventually we will need to store information in a database.</p><p>If this were a Node, Express and Postgres tutorial, now would be the time to install Postgres locally. But let’s not do that and use Docker instead.</p><p>We’ll connect a PostgreSQL database to our web app. To do this, we need to make some additions to our docker-compose.yml file. Go ahead and replace your docker-compose.yml file with this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0debda0141a00f5c55993756e15d5724/href">https://medium.com/media/0debda0141a00f5c55993756e15d5724/href</a></iframe><p>You can see that we added another service called db that uses a postgres image from Docker Hub. If you do not know about Docker Hub, here is <a href="https://docs.docker.com/docker-hub/">some info</a>.</p><p>The db service also has a volume declared. It creates a /data folder in your project directory and maps it to a data folder inside of the container located at /var/lib/postgres/data .</p><p>We also added a depends_on tag to our web service. This tag lets Docker know that we want our database to start before starting our web server. More on that <a href="https://docs.docker.com/compose/compose-file/#depends_on">here</a>.</p><p>With your updated docker-compose.yml file, run the following command in your boogaloo app directory:</p><p>docker-compose up</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*thv_vxzbX1X4cKh3og8k1w.png" /><figcaption>The tail end of our logs. Database ready to accept connections.</figcaption></figure><p>Great! That’s it, we’re done. Docker has created a network for us and our web app can now communicate with our database.</p><p>Let’s prove that our docker-compose up did what we wanted and that our web app is really connected to our db.</p><p>If we check the contents of our app in another terminal window, we see the new /data folder in our local project directory. This means that our volume is set up. Great!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZBYhuj4YlYDdmRKEz_nx1w.png" /><figcaption>Proof that the`docker-compose` up command created a data folder for our volume.</figcaption></figure><p>Now, we need to communicate with our database from inside of our boogaloo app.</p><p>To do this we will use a node package called Sequelize that makes it easy to write some Postgres queries in JavaScript. If you are not familiar with any of this node/javascript stuff, it is okay. The instructions will be copy and paste. Let’s go through a workflow that would happened naturally.</p><p>First, we need to install some necessary packages using npm in order to start using Sequelize. There are a few ways that we could do this. We could docker-compose stop to stop our running containers. Then, we could add our packages to our package.json file manually and rebuild everything with docker-compose up --build . Instead, let’s use docker exec .</p><p>In your terminal, use the familiar command:</p><p>docker ps</p><p>to list our running containers and to get the name of our container. With the container name boogaloo_web_1 we can run:</p><p>docker exec -it boogaloo_web_1 npm install --save sequelize</p><p>then run:</p><p>docker exec -it boogaloo_web_1 npm install --save pg@6 pg-hstore</p><p>These commands will install Sequelize and Postgres(pg) inside of our ‘boogaloo_web_1’ container.</p><p>We could also combine those two commands in a few ways. One way is shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2pUI_rUntNOc8Zn3_hYQNg.png" /><figcaption>Running a command inside of a running container.</figcaption></figure><h3>Docker Exec</h3><p>The docker exec command is simple. It just runs a command (in our case npm install) inside of a running container. Dig deeper into Docker’s exec command <a href="https://docs.docker.com/engine/reference/commandline/exec/">here</a>.</p><p>We used our container name boogaloo_web_1 . Note that if we wanted to run command in our database container we would use boogaloo_db_1 .</p><h3>MORE PROOF</h3><p>To prove that we can use this database in our web app we need to open up the routes/index.js file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*qBLnd4ioFynGUaYr5vY2SA.png" /><figcaption>Where to find your index.js file.</figcaption></figure><p>Replace it with this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d55f411b2ffbdfd9eb7f959d251288f7/href">https://medium.com/media/d55f411b2ffbdfd9eb7f959d251288f7/href</a></iframe><p>On line 10, I have a host defined. This is how I connect to my database. Your host will probably be different than the one I have defined. You can find it by inspecting the network.</p><h3>Network</h3><p>Docker makes a network for us out of our docker-compose.yml that includes our web and our db services. We can inspect the network to find the host address we need. Use:</p><p>docker network ls</p><p>to get the name of the target network. Then, use:</p><p>docker network inspect boogaloo_default</p><p>where boogaloo_default is the name of our network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a5fWYTh6Alxfpu9kyABolg.png" /><figcaption>Get network name and find the IPv4Address in our boogaloo_db_1.</figcaption></figure><p>You will see the services created by our docker-compose.yml file. Find the IPV4Address of your boogaloo_db_1 service and use it as your line 10 or host in your routes/index.js file.</p><p>When you are ready, we need to restart our docker containers to reflect our changes. Run:</p><p>docker-compose stop</p><p>to stop the containers and restart them with:</p><p>docker-compose up</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yUHo6rK4dz3D0DnrRNESWA.png" /></figure><p>If we weed through the last yellow web_1 block we should see a SQL query. At the second to last line, our code executed an Executing (default): INSERT INTO “users”...</p><p>Awesome! We made an entry in our database. We can check the number of entries we have inside the database with a simple change in our code.</p><p>Replace your routes/index.js with this, and don’t forget to change your HOST:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6affad5c287c7b103729717c8c0a99a4/href">https://medium.com/media/6affad5c287c7b103729717c8c0a99a4/href</a></iframe><p>Restart your Docker containers in order to reflect these changes. Use:</p><p>docker-compose stop</p><p>then…</p><p>docker-compose up</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83G0dL5uv-0yKsp8oAFQow.png" /></figure><p>Our new code logs the length or amount of users we have in our database on the last line in our logs.</p><h3>Docker Compose Down/Stop</h3><p>I want to make the point that if we use docker-compose down to shut down our containers instead of docker-compose stop, we will get an error when restarting the containers. Let’s go through that for fun by running:</p><p>docker-compose down</p><p>then:</p><p>docker-compose up</p><p>You should run into an error, somewhere in your logs, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VMrEj7qgIDRhx6CoyG1mwA.png" /><figcaption>A `cannot find module` error telling us that our install of Sequelize did not survive the `docker-compose down` command.</figcaption></figure><p>The docker exec commands we ran earlier (to install sequelize and pg ) did not survive the docker-compose down command. It did add our dependencies to our package.json file and thanks to our volume, this change did survive the docker-compose down command.</p><p>More on the difference between the stop and down commands <a href="https://stackoverflow.com/questions/46428420/docker-compose-up-down-stop-start-difference">here</a>.</p><p>We should now rebuild our network, forcing Docker to run our instructions again. Our Dockerfile has the RUN npm install command that will install all of the dependencies listed in our package.json file that are not found in Docker’s cache.</p><p>So, let’s rebuild by first shutting down. Run:</p><p>docker-compose down</p><p>again, to shut down any running containers. Then run:</p><p>docker-compose up --build</p><p>When Docker rebuilds it uses cache when available. So, running the command should only install what hasn’t been installed yet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IaDd227rXa7AVJjXjOlutg.png" /><figcaption>Proof that Docker uses cache. ‘Using cache’</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nUA3a2juGXF5V0h8xu9H8g.png" /><figcaption>docker-compose.yml file showing volume tracking out node_modules.</figcaption></figure><p>More on cache <a href="https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#build-cache">here</a>.</p><h3>RECAP</h3><p>At the moment, we have a web app running on localhost:3000 that is connected to a Postgres database. We used Docker’s docker exec command to execute a command inside of a running container. We also saw how to inspect our network with docker network inspect and the differences when using docker-compose down and docker-compose stop.</p><p><a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052">Part 2</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-3-introduction-to-docker-compose-and-volumes-b87f563e9550">Part 3</a> | You just finished Part 4 | Part 5 Coming Soon…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dbc25b1c320" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting To Know Docker | Part 3: Introduction to Docker Compose and Volumes]]></title>
            <link>https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-3-introduction-to-docker-compose-and-volumes-b87f563e9550?source=rss-d328e1df77ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/b87f563e9550</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[J.P.]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 20:39:29 GMT</pubDate>
            <atom:updated>2017-12-20T17:22:40.428Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052">Part 2</a>| You are about to read Part 3| <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-4-the-database-3dbc25b1c320">Part 4</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2WJXqnXn1-l1d1Z41EGyIg.jpeg" /></figure><p>Here, we will continue to build on what we’ve learned about Docker in <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> and <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052">Part 2</a>. You will see that we are going to abandon long commands. Our goal is to create a project that another developer can get up and running with minimal work. Let’s talk about docker-compose.</p><p>Here is the docker-compose.yml file we will use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd3e99ef5e7b28b4e795232735cc8a20/href">https://medium.com/media/fd3e99ef5e7b28b4e795232735cc8a20/href</a></iframe><p>If you have the boogaloo app from Part 2, you should have an empty docker-compose.yml in the root directory. If you don’t, get it <a href="https://github.com/Jp-Rivera/boogaloo">here</a>. Place the contents of the docker-compose file above, into your blank docker-compose file.</p><p>At your command line, inside of the boogaloo app directory run:</p><p>docker-compose up</p><p>If you do not have the docker-compose command, try <a href="https://docs.docker.com/compose/install/">this</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*93Sr_uhCbxC48foL7msMFA.png" /><figcaption>The tail end of logs given by running the `docker-compose up` command.</figcaption></figure><p>That is our web server running. Checking out localhost:3000 should result in some satisfaction… and a gif.</p><p>You should recognize the output as it is the same as we have seen before. This time, we just needed one command. It did the steps defined in our Dockerfile and the steps in our docker-compose.yml file. For now, just note that we do need a Dockerfile in order for docker-compose up to work.</p><h3>docker-compose</h3><p>The top line in our docker-compose file:</p><p>version: 3</p><p>specifies the version of docker-compose-file we will be using. If you are interested, more on versions <a href="https://docs.docker.com/compose/compose-file/">here</a>. Here is our services block:</p><pre>services:                         <br>  web:                           <br>    build:                              <br>      context: .<br>      image: boogaloo_image                           <br>    command: [&quot;npm&quot;, &quot;start&quot;]                           <br>    ports: <br>      - 3000:3000                           <br>    volumes: <br>      - .:/boogaloo                              <br>      - /boogaloo/node_modules</pre><p>The services block allows us to define our containers. We have one named “web” that has some options that should look familiar. They are our command line arguments. Under the build option we specify our local directory as our current one.</p><p>This may help as well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*KlUGL98za9TorIomOElMPg.png" /><figcaption>Map of Docker commands and docker-compose file options.</figcaption></figure><h3>Volumes</h3><p>Volumes are what we replace our bind mount with. They are technically being used when we usedocker runwith a -v command. But, we are ditching that for the docker-compose up command. Much easier. More on the difference between bind mounts and volumes <a href="https://forums.docker.com/t/whats-the-difference-between-adding-volume-in-a-dockerfile-and-running-an-image-with-volume/17480">here</a>.</p><p>In our docker-compose volume block we see:</p><p>.:/boogaloo</p><p>is the same as our previous command $(pwd):/boogaloo . Our docker-compose.yml file knows we want to use $(pwd) if we use . .</p><p>Next, the</p><p>/boogaloo/node_modules</p><p>is specific to using Node’s package manager, npm. If you are into npm, you may want to refer to <a href="http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html">this article</a> for some specific node tips. The only purpose of leaving this part in the docker-compose file is to show you that we can set multiple volumes. That would be insane to do through the docker run command.</p><p>Let’s run:</p><p>docker volumes ls</p><p>to list our volumes, and</p><p>docker inspect &lt;volume ID&gt;</p><p>to inspect our volume.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AmS2FdaZAimiCgLg6FKahg.png" /></figure><p>Our volume’s “Mountpoint” is what we are interested in at the moment. It tells us the location of our volume in the container. You should have your local directory and Docker container linked for changes.</p><p>There is a lot to volumes. <a href="http://container-solutions.com/understanding-volumes-docker/">Here is a great article</a>, if you want to dig deeper.</p><h3>RECAP</h3><p>We have replaced our long docker run command with our docker-compose up command. This uses our Dockerfile and our docker-compose.yml file. In part 4 we will make some changes to our docker-compose.yml and see a few helpful commands that we will need to know when working with docker-compose .</p><p><a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052">Part 2</a>| You just finished Part 3| <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-4-the-database-3dbc25b1c320">Part 4</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b87f563e9550" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting To Know Docker | Part 2: Web App Boogaloo]]></title>
            <link>https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052?source=rss-d328e1df77ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/604631d9052</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[J.P.]]></dc:creator>
            <pubDate>Thu, 07 Dec 2017 18:44:08 GMT</pubDate>
            <atom:updated>2017-12-20T17:21:05.230Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> | You are about to read Part 2| <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-3-introduction-to-docker-compose-and-volumes-b87f563e9550">Part 3</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-4-the-database-3dbc25b1c320">Part 4</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NH_Sd3Kd9lRrdEA80nQvjA.jpeg" /></figure><p>In <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">GTKD Part 1</a> we learned how to get up and running with Docker quickly. We will do nearly the same workflow here, with some important differences. Last time, we ran docker build and were thrown into a command line inside of a container. This can work for us, but we will learn about Dockers ability to run containers in the background. We will also see more options in our Dockerfile. Please note that we will purposely hit errors in order to slowly unpack what we are doing.</p><p>First, you can get the Boogaloo web app that I will use <a href="https://github.com/Jp-Rivera/boogaloo/">here</a>. This is an app generated by the <a href="https://expressjs.com/en/starter/generator.html">express generator</a>. No changes have been made to it. Not to worry if you are not familiar with any of the app, we will spend our time at the command line, our Dockerfile and a browser. Let’s clone and ‘cd’ into our /boogaloo directory.</p><pre>git clone <a href="https://github.com/Jp-Rivera/boogaloo/">https://github.com/Jp-Rivera/boogaloo/</a></pre><pre>cd boogaloo</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zUtc9jLn1YT8WyOd0Dj79A.png" /><figcaption>Cloned repository and change directory into the cloned directory. [master] is my current git branch. If you do not have that, you don’t need it.</figcaption></figure><p>Cool, now run:</p><p>npm start</p><p>This should result in an ERROR like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*m2rBccXN15HjbDrNOW6k0w.png" /><figcaption>Some npm errors that tell us we need to npm install.</figcaption></figure><p>npm startlooks for a start command in our package.json file. Our express-generator has set the command to run our web server. The reason for our error is just that we forgot to install our dependencies. Normally, we would do a quick npm install and re-run npm start . Let’s not do that here though. Let’s use Docker instead.</p><p>To use Docker, we need an image. Our boogaloo app is not ready for Docker so we need to make a Dockerfile in the root directory. We did this in <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">GTKD Part 1</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*8i3VvvBw8RnU3nmZA_vY8g.png" /><figcaption>A reflection of the addition of a Dockerfile to the root directory of the boogaloo app.</figcaption></figure><p>Put this inside of your empty Dockerfile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5d48fd98a638ec031acc8ecaa9f25b0/href">https://medium.com/media/e5d48fd98a638ec031acc8ecaa9f25b0/href</a></iframe><p>We will go over the Dockerfile along the way, but first, build the image with the familiar command:</p><p>docker build -t boogaloo_image .</p><p>We covered this command in Part 1. We are building an image we called boogaloo_image using our . or current directory as <a href="https://docs.docker.com/engine/reference/builder/#usage">context</a>. Our instructions for the build are provided by our Dockerfile.</p><p>I am using names like “boogaloo_image” just so we understand what we are building. In the future, you should name your images according to your liking. If all went well, we should see some familiar output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VqtrflGgMMmGYYBohgVnaQ.png" /><figcaption>A slightly chopped image of logs spit out during the build.</figcaption></figure><p>Confirm your image is built with:</p><p>docker image ls</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NynQ0VX17AZdgI4vOAYpsg.png" /><figcaption>Proof our new image was built.</figcaption></figure><p>I have two node images at the moment. If yours looks different, no biggie. Just make sure you have your “boogaloo_image” and your “node -Alpine” image. Alpine is our base image, defined in our Dockerfile with FROM .</p><p>Alpine is a popular Linux distribution because it is small. You will run into it plenty while learning about Docker.</p><p>Let’s jump into our container using our new image. This command should be familiar.</p><p>docker run -it boogaloo_image bash</p><p>Again, we are running our boogaloo_image to get a container and inside the container running the bash program that will give us a shell.</p><p>This will result in an ERROR.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xN4Z8YlGK5SPwKxcnaPV7Q.png" /><figcaption>Fancy way Docker tells us that the program we specified (bash in this case) was not found.</figcaption></figure><p>Here, we learn something important about base images that are smaller. When we pass the bash command we expect bash to be run inside of our container. The famously tiny Alpine does not have bash installed. We could install it, but I happen to know that Alpine has sh . This is like bash in that it can give us our shell.</p><p>Let’s adjust our run command slightly to run sh instead of bash :</p><p>docker run -it boogaloo_image sh</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07meLjz0xzOk3yHr1nLF1Q.png" /><figcaption>Inside of our Alpine based container using sh.</figcaption></figure><p>We are now inside of our docker container, inside of the /boogaloo directory that we specified in our Dockerfile with the WORKDIR command. Let’s start our server inside of our Docker container:</p><p>npm start</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*_5SUSimb-9sBrHZXZqi_LA.png" /><figcaption>Running npm start inside of Docker container.</figcaption></figure><p>Before, we got an error when we ran npm start and I told you that we needed to install our dependencies. That is where our Dockerfile command RUN came into play. We had Docker run the command when building the image. When we run our image, we already have our dependencies installed. You should see at this point the value of the RUN command.</p><h3>Ports</h3><p>According to the Express docs, with our server running inside of our container. We should now be able to head over to localhost:3000 and check out our running application. But, this will not work and it is important to know why.</p><p>Put simply, Docker needs to be told what port to map the container port to. To put it another way, Docker is currently using port 3000, we need to tell it that we want to connect to it locally. Let’s do that by killing the terminal process with ctrl-c to exit Docker.</p><p>Run this command:</p><p>docker run -it -p 3000:3000 boogaloo_image sh</p><p>You should be back inside of the Docker container at this point and should also be able to run:</p><p>npm start</p><p>and in your browser, go to localhost:3000 and you should see our web app running. When you visit the localhost, you should see some logs coming through your Docker container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*kAxZLsD_vPeWAyahDLx9hw.png" /><figcaption>Opening a port on the command line with -p</figcaption></figure><h3>-p</h3><p>Let’s talk about the -p 3000:3000 we see in the command above. The left side of the colon is the port that we want use in our browser. So if in our browser we wanted to use localhost:8080, the command would be -p 8080:3000 and we would navigate to localhost:8080 in our browser.</p><p>An odd thing about the Dockerfile is that we specified our port using the EXPOSE command, but still had to use the command line flag -p for it to work. The EXPOSE command actually does nothing in the Dockerfile. To expose ports we must use the command line (just like bind mounts that we saw in part 1). Eventually, we won’t need the long command at the command line. We’re not there yet.</p><p>So, why even use EXPOSE in the Dockerfile? The answer is that it serves as documentation for the developer who needs to expose the port on the command line. So, if I need to know what port Docker is using and I am not familiar with what express generator uses (or whatever the project uses), I can check the Dockerfile for an EXPOSE command. A little odd, but it can be helpful.</p><p>Docker docs for -p <a href="https://docs.docker.com/engine/reference/run/#expose-incoming-ports">here</a>.</p><h3>Oh yea, we need persistence… and Docker’s detach</h3><p>Up until now we have been running a container in order to access its command line. We don’t really need to jump into the command line if all we want to do is start our web server. Since we can run a program with our docker run command, we can do this:</p><p>docker run -it -p 3000:3000 boogaloo_image npm start</p><p>Notice, npm start instead of sh at the end of the command.</p><p>BAM! Web server is running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*nvRV3UMIvVZ4DnbMovnpNQ.png" /><figcaption>Running express web server using a horribly long command.</figcaption></figure><p>We are almost ready to do some developing, but something is missing. We don’t have the persistence that we had in part 1, but we can easily implement this by exiting our container and rerunning our run command like this:</p><p>docker run -it -p 3000:3000 -v $(pwd):/boogaloo boogaloo_image npm start</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*jkspqQQGdr0BgMyqtqhHng.png" /><figcaption>Running express web server using a more horribly long command.</figcaption></figure><p>Additionally,the -d flag detaches the running container and runs it in the background.</p><p>docker run -it -d -p 3000:3000 -v $(pwd):/boogaloo boogaloo_image npm start</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1br60mHVIm2oDlTgU6a5iQ.png" /><figcaption>Running with -d flag prints an ID number and spits us back out into our local terminal. Using ‘docker ps’, we can see our running container.</figcaption></figure><p>We now have our local and Docker directories mapped, our container running in the background and our web server running on localhost:3000. Nice… sort of.</p><h3>RECAP</h3><p>At this point in our journey through Docker, it would be fair to say that the command we are using is long. It is super annoying to have to remember all that and other developers won’t like it either. Luckily, Docker knows this and provides us with a way to do all of this much easier.</p><p>Next, we will cover volumes to replace our bind mounts and we will use docker-compose to replace our long command.</p><p><a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f">Part 1</a> | You just finished Part 2| <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-3-introduction-to-docker-compose-and-volumes-b87f563e9550">Part 3</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-4-the-database-3dbc25b1c320">Part 4</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=604631d9052" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting To Know Docker | Part 1: Your First Image and Container]]></title>
            <link>https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-1-your-first-image-and-container-50c539a3bd5f?source=rss-d328e1df77ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/50c539a3bd5f</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[J.P.]]></dc:creator>
            <pubDate>Wed, 06 Dec 2017 15:35:14 GMT</pubDate>
            <atom:updated>2017-12-20T17:19:45.061Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NH_Sd3Kd9lRrdEA80nQvjA.jpeg" /></figure><p>This is a simple workflow to help developers get to know Docker.</p><p>Part 1 focuses on building an image and creating a container with it. For this example, I will use a Node image, but the image can be replaced in order to create a sandbox in any environment you want. I will assume that you have some minor command line experience.</p><p>First, install <a href="https://www.docker.com/get-docker">Docker</a>.</p><h3>Docker Image</h3><p>Open up a terminal and use the command docker image ls . If you have not yet created an image in Docker, you should just see some headers. If you have an image and don’t know why, ignore it for now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*57pg-3CNDhfZkrE8yAM73g.png" /><figcaption>A fresh copy of Docker with no images yet.</figcaption></figure><p>Let’s build an image.</p><p>To build an image, we are going to use a Dockerfile. The purpose of the Dockerfile is to list our image build instructions. These instructions can be passed as command line arguments, but it is much easier to understand with a Dockerfile. Make a Dockerfile inside of a new directory called ‘app’.</p><p>We will not talk about making efficient images here, but you can (and you should) watch <a href="https://www.youtube.com/watch?v=pPsREQbf3PA">this talk</a> by Abby Fuller or check out the <a href="https://docs.docker.com/engine/reference/builder/">Docker Docs</a> in the future. For now, let’s keep it simple.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*QO3liAx8S2QJq8KL_hDA8g.png" /><figcaption>My lone Dockerfile inside of my app directory.</figcaption></figure><p>As you can see, I am in my ‘app’ directory and I only have a Dockerfile. Fill your Dockerfile with these simple instructions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88348a4cb49fb305504b51f9b0e0bf59/href">https://medium.com/media/88348a4cb49fb305504b51f9b0e0bf59/href</a></iframe><p>FROM pulls a Docker image from Docker Hub. In this case, we want the Node 8 image.</p><p>WORKDIR sets the working directory inside of our container. We want it to be just like our local directory.</p><p>COPY takes all of our files in our local directory and copies them into our /app container directory. Again, Docker has <a href="https://docs.docker.com/engine/reference/builder/">great documentation</a> if you want a deeper explanation.</p><p>To run the instructions inside of the Dockerfile use…</p><p>docker build -t my_image .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gikv4JFHS9ep43Gm3RH44g.png" /></figure><p>The -t flag allows us to name or tag our image for easy reference. We named our image “my_image”. The .(dot) tells Docker that our current directory is the context of our build. For now, not too important.</p><p>After the build, running docker image ls should show you two images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*ThHUgFDMk5-kzYCYdSUV8Q.png" /></figure><p>The “node” image was pulled from Docker Hub because of our FROM line in our Dockerfile. It is our base image.</p><p>The image named “my_image” is dependent on the base image. Trying to delete the Node image will result in an error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TFOS2OoNvOwS39Tywaap8w.png" /><figcaption>Attempt to remove an image that has children. You can usually remove an image by using the command above. Notice that passing the first 3 characters of the image ID is good enough to reference the image.</figcaption></figure><h3>Docker Container</h3><p>A container is just a running instance of an image. Let’s run the “my_image” image.</p><p>docker run -it my_image bash</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RRA1Pg7KWVNcRbNnTGzDYg.png" /><figcaption>Running a Docker image and accessing the shell. We are in our shell because of the “bash” command used here. The container and changes inside the container will expire when the command is terminated. That is, when the exit command is used.</figcaption></figure><p>COMMAND BREAKDOWN</p><p>docker run — command given by Docker</p><p>-i — Keep STDIN open even if not attached</p><p>-t — Allocate a pseudo-TTY</p><blockquote>For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process. -i -t is often written -it</blockquote><blockquote>- <a href="https://docs.docker.com/engine/reference/run/#foreground">Docker</a></blockquote><p>bash — Program to run inside the container. This is how we get into the shell of the container.</p><p>Great! We are now inside of a running image or a Docker container. We used a Node 8 image to build our image, so lets check what version of Node is running inside of this container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oMaCXJkAjT07J2AYQRGNZg.png" /><figcaption>Latest Node LTS version at the time of writing this article.</figcaption></figure><p>Our Node image gives us NPM out of the box, so let’s install Cowsay in our Docker container.</p><p>npm install -g cowsay</p><p>You should now be able to do this…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yqawyYqQx32A5XqNSoE-uQ.png" /><figcaption>Cowsay running inside of a Docker container.</figcaption></figure><p>We can exit the container with the exit command. Since I do not have Cowsay installed locally on my local system, I will no longer be able to access the package. Notice the terminal name change when exiting a container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SaWpx45oU6GTcwa6_3PW7A.png" /><figcaption>Exiting a Docker container and finding that I don’t have access to packages installed inside of the container anymore.</figcaption></figure><p>Let’s jump back into our Docker container with a familiar command.</p><p>docker run -it my_image bash</p><p>and now let’s Cowsay something again…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mhGSrFJMqMsD3ajMtQsE5Q.png" /><figcaption>Proof that our Docker container does not remember our last session where we installed Cowsay.</figcaption></figure><p>Whoa! Wha?! …. uh?</p><p>Ok, what is going on here is that because we are using the bashcommand to enter the shell, the container changes only last as long as the bash command does. So, when we exit the shell with the exit command the container is stopped and our changes lost. We can prove this with the following command.</p><p>docker container ls -a</p><p>or</p><p>docker ps -a</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b5LpbbkWyDL04wB1hFEXWg.png" /><figcaption>An “Exited” container.</figcaption></figure><p>The two commands are the same. It shows us all of our containers instances. Without the -a tag, Docker will show us only our running containers. Soon, we will see a running container using this command. Right now, our once running container has been stopped.</p><h4>A bit of a re-cap</h4><p>What we can currently do with Docker is spin up a container that we can dabble in with no consequence. This can be cool if we want to try a technology like Node without installing it locally.</p><p>However, there is no way that I am reinstalling Cowsay every time I run the container. Why doesn’t this container keep our changes?</p><p>The answer is that the contents of the container are determined by the containers lifespan. To put it simply, stopping a container removes any changes made in that container. It is unrealistic to keep the container running forever, so we need a different option to make our container persist. Let’s go through a workflow to do that.</p><h3>Persistent Docker with Bind Mounts</h3><p>Bind mounting is one way Docker handles persistence. A bind mount is a mapping of a local file or directory to a container file or directory. Put simply, if I make a change inside my container it should reflect locally and vice-versa.</p><p>One thing to note about bind mounts is that they must be specified on the Docker command. They cannot be declared in the Dockerfile. Why bind mounts can’t be done in the Dockerfile is not very important at the moment and it will make sense once we are introduced to volumes. More on that in the future. For now, let’s bind mount.</p><p>docker run -it -v $(pwd):/app my_image bash</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mp9ZJqtD-p2ndYQ6zDYdPw.png" /></figure><p>BAM! Bind Mounted.</p><p>We see the bind mount here as …</p><p>-v $(pwd):/app</p><p>The -v tag allows us to pass a variable where on the left side of the colon we use our current working directory and on the right side of the colon we use the location in the container. These are the two directories we are binding. You can find more on -v <a href="https://docs.docker.com/engine/admin/volumes/volumes/#choose-the--v-or-mount-flag">here</a>.</p><p>For proof, we can open up another terminal tab and navigate to the ‘app’ directory on your local machine and run</p><p>docker ps</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G8i9XoHqSMfAQe0hjGnYcg.png" /><figcaption>Showing our running container. Notice that the name given is random.</figcaption></figure><p>Now, in the local directory run npm init</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a8AEajCFP6oDNvJmnH2AKw.png" /><figcaption>npm init logs</figcaption></figure><p>If you have never used NPM, just press enter on all the default options. If you have used NPM, you know to just press enter on all the default options. More importantly, we should now have a new package.json file that was created locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pikRRSrPpekhHl4xmNZRMA.png" /><figcaption>Proof of local package.json file</figcaption></figure><p>If we go back to our terminal with our running Docker container and check the contents in our directory, we see that we also have that package.json file. Now, from inside your Docker container, make an index.js file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oUZWZ-GP0Cw1f-w9b2AlHg.png" /><figcaption>Proving the existence of our package.json file and creating an index.jsd file inside of our Docker container.</figcaption></figure><p>Navigate back to your local terminal and inside your directory you should have that index.js file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gM_uUQZ7AWRH9PtTclRnKw.png" /><figcaption>Proof that our index.js file (made in our Docker container) is created locally as well.</figcaption></figure><h3>RECAP</h3><p>Now, with what we know, we can have a completely isolated development environment. We can install dependencies here and not have to worry about installing them in our local machine. Docker has a super amount of images that we can use. All of the techniques here are great when you use them for your small projects. When we get to using Docker on a larger scale there is more optimization needed in order to really make Docker whistle.</p><p>Personally, this workflow gave me some real clarity into the reason I hear so much praise around Docker.</p><p>Here are some helpful commands to help you navigate and explore Docker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d738d3480e4cfeb09de3b2f28f6d59e/href">https://medium.com/media/8d738d3480e4cfeb09de3b2f28f6d59e/href</a></iframe><p>You just finished Part 1| <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-2-web-app-boogaloo-604631d9052">Part 2</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-3-introduction-to-docker-compose-and-volumes-b87f563e9550">Part 3</a> | <a href="https://medium.com/@linkwithdanmarcos/getting-to-know-docker-part-4-the-database-3dbc25b1c320">Part 4</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50c539a3bd5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>