Requirements: Mac OS X (This tutorial assumes you're using a Mac, but you can find installation instructions for Windows or Ubuntu and skip ahead the Setup section)
Docker has just celebrated its 2nd birthday, but it's still a "new" powerful piece of technology. A lot of developer friends that I talk to have either heard or read about it but haven't actually used it. It lets you do really cool things like quickly test your app in development with the exact same environment as in QA/Test/Production, or share that app with other developers for a quick a painless onboarding. A commonly used analogy for Docker is to compare it to actual real-life containers or lego bricks: it provides a fundamental unit, and with it a way for an application to be portable and moveable, regardless of hardware.
In this tutorial, I'll give a quick overview of what Docker is and why you might want to use it, how to install it, and then we'll work on setting up a Node container and creating an express starter app inside it. This is a long tutorial! The official Docker getting started guide gets you up and running quicker, what I aim to do here is explain what's happening on each step along the way.
What we’ll cover:
- Introduction (What's Docker and why use it)
- Installation
- Docker Hub and Dockerfiles
- Docker Pull: Pulling an Ubuntu image
- Docker Run: Running our Ubuntu image and accessing the container
- Docker Commit: Installing node, npm, express and committing the changes
- Docker Push: Pushing our container back so other people can use it
I'll be referring to commands executed in your own terminal with:
- $ command
- $ root: command
You've probably heard of Docker by now. Every day there's some front-page HackerNews mention of it, or you see people on Twitter/IRC talking about it. Its popularity has grown enormously in the past couple years, and most cloud providers already support it. If you are curious about it, but still haven't tried it out, this tutorial is for you. ☺
Okay, so what is Docker? Well, Docker can be a reference to a few things:
- Docker client: this is what's running in our machine. It's the docker binary that we'll be interfacing with whenever we open a terminal and type $ docker pull or $ docker run. It connects to the docker daemon which does all the heavy-lifting, either in the same host (in the case of Linux) or remotely (in our case, interacting with our VirtualBox VM).
- Docker daemon: this is what does the heavy lifting of building, running, and distributing your Docker containers.
- Docker Images: docker images are the blueprints for our applications. Keeping with the container/lego brick analogy, they're our blueprints for actually building a real instance of them. An image can be an OS like Ubuntu, but it can also be an Ubuntu with your web application and all its necessary packages installed.
- Docker Container: containers are created from docker images, and they are the real instances of our containers/lego bricks. They can be started, run, stopped, deleted, and moved.
- Docker Hub (Registry): a Docker Registry is a hosted registry server that can hold Docker Images. Docker (the company) offers a public Docker Registry called the Docker Hub which we'll use in this tutorial, but they offer the whole system open-source for people to run on their own servers and store images privately.
- Simplifying configuration of a development environment
- Quickly testing your app in an environment similar to QA/Test/Production (less overhead compared to VMs)
- Sharing your app+environment with other developers, which allows for fast/reliable onboarding.
- Ability to diff containers (this can be immensely useful in debugging)
Running a container, and therefore Docker, requires a Linux machine. Since we're using a Mac, that means we'll need a VM. To make the installation process easier, we can use Boot2Docker which installs the Boot2Docker management tool, VirtualBox, and sets up a VM inside it with Docker installed.
Head over to this link to download the latest release of Boot2Docker, and install it (Boot2Docker-1.5.0.pkg at the time this was written):
https://github.com/boot2docker/osx-installer/releases/latest
After the installation is done, go to your Applications folder and open Boot2Docker. That's going to open a new terminal and run a few commands which basically start a VM that already has Docker installed, inside VirtualBox, and then sets a few environment variables so we can access the VM from our terminal. If you don't want to always open Boot2Docker to interact with Docker, just run the following commands:
- # Creates a new VM if you don't have one
- $ boot2docker init
- # Starts the VM
- $ boot2docker start
- # Sets the required environment variables
- $ $(boot2docker shellinit)
- $ docker run hello-world
- Hello from Docker.
- This message shows that your installation appears to be working correctly.
(If you have any problems, feel free to ping me or you can find Docker's official installation instructions here)
Dockerfiles and Docker Hub
Before we move forward, I think it's important to understand what happened when we executed $ docker run hello-world so you're not just copy+pasting the next instructions.docker run is the basic command that we use to start a container based on an image while passing commands to it. In this case, we said, "Docker, start a container based on the image hello-world, no extra commands". Then it downloaded the image from Docker Hub and started a container inside the VirtualBox VM based on that image. But where does the hello-world image come from? That's where Docker Hub comes in. The Docker Hub, like we mentioned in the introduction, is the public registry containing container images to be used with Docker, created by Docker, other companies, and individuals. Here you can find the image for hello-world we just executed:
Docker Hub Hello-World Image
Every image is built using a Dockerfile. In the description for the hello-world image, you can find a link to its Dockerfile which only has 3 lines:
- FROM scratch
- COPY hello /
- CMD [“/hello”]
- INSTRUCTION arguments
Docker Pull: Downloading an Ubuntu image
Now that we know our Docker installation is correctly setup, let's start playing with it! Our next step is getting an Ubuntu image. To find an image we can either go to the Docker Hub website or just run in the terminal:
- $ docker search ubuntu
- Official images are images maintained by the docker-library project and accepted by the Docker team. That means they adhere to a few guidelines found here, some of which are living in a git repository and that repository being at least read-only so users can check its contents. You can count on those images for working correctly with Docker. Also, contrary to other images where you need to reference them to pull using USERNAME/IMAGE_NAME, these images can simply be referred to in commands by IMAGE_NAME (such as Ubuntu). All of their Dockerfiles can be found in this organization.
- The automated column refers to Automated Build images. It simply means that the image is being built from a Dockerfile inside a GitHub or BitBucket repository, and it's automatically updated when changes are made to it.
- $ docker pull ubuntu
Written by Heitor Tashiro Sergent
If you found this post interesting, follow and support us.
Suggest for you:
No comments:
Post a Comment