Using Vagrant and Docker to Build a Dev Environment

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

In this Live Environment, you are a new developer working at XYZ Company. It’s your first day on the job and you have been provided with a new computer that has Docker and Vagrant installed. In the root directory is the code you will be working with. Your team lead has asked you to write a Dockerfile and Vagrantfile to setup your local development environment.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Vagrant and Docker to Build a Dev Environment

Introduction

In this lab, we're going to learn how to set up a development environment using Vagrant and Docker. To do this, we'll create a Dockerfile that will run our code and a Vagrantfile that will create and configure our environment.

Create the Dockerfile

We'll be working in the /root/docker directory as the root user. Sign in and elevate to root privileges using the password provided on the lab instructions page.

sudo su -

We need to create a Dockerfile with the following characteristics:

  • A base image of node:alpine
  • The working directory should be /code
  • Copy the code directory to /code
  • Port 3000 should be exposed
  • Run an npm install
  • Start the app by running node app.js

First, we need to change to the docker directory.

cd docker

Next, let's create the Dockerfile.

vi Dockerfile

In the vi editor, enter the following:

FROM node:alpine
COPY code /code
WORKDIR /code
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

We have now successfully created the Dockerfile we need to configure our development environment.

Create a Vagrantfile

The next step is to create a Vagrantfile with the following characteristics:

  • Docker should be set as the default Vagrant provider.
  • Vagrant should use the Dockerfile we created earlier.
  • Port 3000 should be exposed.

Run the following command to create the Vagrantfile:

vi Vagrantfile

Now we need to configure the Vagrantfile to use Docker as the default provider. In the vi editor, enter the following:

ENV['VAGRANT_DEFAULT_PROVIDER'] = "docker"

Next, enter the following in the Vagrantfile to configure Vagrant:

Vagrant.configure("2") do |config|
  config.vm.provider "docker" do |d|
    d.build_dir = "."
    d.ports = ["80:3000"]
  end
end

Now let's use the Vagrantfile to create and configure our Docker image.

vagrant up

Once this is complete, run the docker images command to verify that our image was successfully created. Then run the docker ps command to check that our application is running.

The next step is to spin up and test our dev environment.

curl http://localhost

This should return the "Hello World!" application.

Switch to the /code directory, and open the app.js file.

cd code/
vi app.js

Next, change the Hello World! message to Hello <YOUR_NAME>!. Then change back to your home directory.

cd ../

Now let's reload the container so our changes will take effect.

vagrant reload

Now when we run curl http://localhost, we should see a message that says Hello <YOUR_NAME>!.

Conclusion

Congratulations, you've successfully completed this lab!