Skip to content

A quick tutorial to help you get started with containers and Docker.

Notifications You must be signed in to change notification settings

stewartreichling/containers-are-easy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Containers Are Easy

By the end of this tutorial, you will have built an image from a small Node.js app and run that image as a container using Docker. While this tutorial uses a Node.js web app, you don't need to know Node.js to understand the tutorial.

Pre-requisites

  • We'll use docker to build an image for our app. After that, we'll use docker to run that image in a container.
  • We'll use git to get example code.

Before you get started, set both of these up on your development machine and make sure the docker daemon is running.

Tutorial

Open a shell on your development machine. I use iTerm2 on my Mac.

Get the app code and files needed to build your image

Get the app source from this repository:

git clone https://github.com/stewartreichling/containers-are-easy.git

Navigate to the newly-downloaded directory:

cd containers-are-easy
cd src
ls -a
#output: .dockerignore  Dockerfile  package-lock.json  package.json  server.js

The directory contains five files:

  • Dockerfile contains instructions used by docker to build an image of your app.
  • .dockerignore tells docker to skip certain files (learn more) when building your app.
  • server.js, package.json and package-lock.json are the files which constitute your app.

Build the image

Use the docker build command to create an image of your app:

docker build -t node-web-app-image .

The -t ("tag") flag names your image node-web-app-image. This is handy for identifying and running your image in the following steps.

The docker build command follows the steps specified in the Dockerfile to create an image. At a high level, these steps specify:

  • the base image, which is the starting point for your app's image (FROM node:12-slim). In this case, your final image will contain your app as well as everything in the node:12-slim image.
  • the directory where your app will live on the image (WORKDIR /usr/src/app).
  • the port on which the container listens when running the image (EXPOSE 8080).
  • the command which runs when the container starts the image (CMD [ "node", "server.js" ]). In this case, this starts our Node.js app.

Run the image in a container

Use the docker run command to create a container which runs your image:

docker run -p 127.0.0.1:8080:8080 -d --name node-web-app-container node-web-app-image

The docker run command will:

  • start a container named node-web-app-image that runs your image (node-web-app-image)
  • make that container accesible on 127.0.0.1:8080 on your development machine.

The flags used in this example are:

  • -p ("publish") maps the container's port (8080) to the machine on which the container is running (127.0.0.1:8080).
  • -d ("detached") runs the container in the background.
  • --name specifies the container name (node-web-app-container). Note that this is not the same as the image name.
  • The final argument is the image name (node-web-app-image).

Test the app

Now that the container is running at 127.0.0.1:8080, test that it's working. You can do this by visiting 127.0.0.1:8080 in a browser or using a tool like curl:

curl 127.0.0.1:8080
# output: Hello, World!

Clean up your container

Now that you've verified that your app is working, it's time to stop your container and clean up.

Clean up your container using docker rm:

docker rm -f node-web-app-container

The -f ("force") flag removes a running container even if it still running. This shortcut is useful for a simple app like this one but you may want to use docker stop and docker rm (without -f) if your app needs to do additional cleanup before it is terminated.

Conclusion

That's it, you're done!

Additional reading

This was just a first step in mastering containers. Here's additional material to help you build production-ready containerized applications using a streamlined local development flow.

About

A quick tutorial to help you get started with containers and Docker.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published