How to set up Docker to Dev Environment
09:30
On this Blog post, I will be using Dockerfile to setup development environment. Dockerfile is a script, composed of different commands and arguments listed successively to automatic performs actions on a base image in order to create a new one. They are use for organizing things and greatly help with deployments by simplifying the process start-to-finish.
As an example, the setup going to use iojs to be the web server running inside the Docker container.
However, I have came across on different kind of errors and found a solution that worked for me. I will update this post as I learn more and drawbacks with this approach.
Installing Docker
If you have not installed Docker head over on my previous blog postSetting up the project
The set up going to use to initialize my development environment:- Windows 8.1
- Docker version 1.8.0, build 0d03096
- Boot2Docker-cli version: v1.8.0rtual
Dockerfile
# Base image stable Ubuntu 14.04 FROM ubuntu:14.04 # Replace shell with bash so we can source files RUN rm /bin/sh && ln -s /bin/bash /bin/sh # Add Author MAINTAINER Ronald San Diego # Install application dependencies RUN apt-get update && apt-get install -yq build-essential \ libssl-dev \ curl \ npm \ ca-certificates \ rsync \ software-properties-common \ wget \ dos2unix \ zlib1g-dev \ gcc \ make \ g++ \ && apt-get clean # Clean out apt-get - So we don't store extra space on the image. # Install NodeJs RUN curl --silent --location https://deb.nodesource.com/setup_0.12 | sudo bash - RUN sudo apt-get install --yes nodejs # Install npm RUN sudo npm install npm -g # Install node v4.0.0 RUN npm cache clean -f RUN npm install -g n RUN n stable # Fixing npm permission - to run globally RUN mkdir ~/npm-global RUN npm config set prefix '~/npm-global' ENV PATH ~/npm-global/bin:$PATH RUN npm install -g nodemon --save # Add this script in /usr/local/bin/ directory ADD start-project.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/start-project.sh \ && dos2unix /usr/local/bin/start-project.sh # Execute the application when container created CMD bash -C '/usr/local/bin/start-project.sh';'bash' # Expose port 8080 of container EXPOSE 8080
To note:
- When using the ADD command in Dockerfile to add files, the files need to be on the same directory where the Dockerfile you are creating. Also, I added the "chmod
+x" command to enable the script to be executable. - The CMD command is used to execute specific command whenever a container is created, on this case we will execute the script called start-project.sh.
Content of start-project.sh
#!/bin/bash
cd /myProject
nodemon hello.js
Example hello-world application
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(8080); console.log('Server started');
- Create a new application name "hello.js" and put the code above on this file.
- Create a new folder name "myProject" (any name if you want) and put hello.js on this folder.
Build Docker image using Dockerfile
Once your Docker running smoothly and your source code application, you can start setting up the docker image.- Open up your "boot2docker" (CLI should pop up)
- Change directory of where your Dockerfile is located
- Run the command below:
docker build -t myproject/dev .
-t stand for tag name which the image name you want to be and . is to execute Dockerfile on the directory you are in and start creating the image.
PS: The Dockerfile execution might take a while
Creating container
One of the cool feature Docker is having a symlink meaning you can link a folder or file from your host machine to the Docker container. This a cool way to have because you don't need to be in the container and edit it.
We also installed nodemon so whenever something changed in a file that nodemon monitoring, it should restart the application automatically.
To do symlink in Docker, when we create a container a flag -v to allow you to mount the directory of your application from host machine into the container.
Example:
In a Windows/Mac OS you need to add "//" when specifying the full path of files/folders you want to mount in the container.
Go to your browser and type your boot2docker IP and the open port on your host. If your unsure of the IP, type in your CLI:
Problem Encountered
If you are using Window/Mac OS (Operating System) you might encountered this problem that Docker seems to be not working with symlink on nodemon meaning if nodemon already monitoring the files and you changed something it would not restart your application so you need to manually restart nodemon for the change to happen. The command below is an example of how you would mount folder to your container.docker run -dit -p 8080:8080 -v //full/path/to/project/folder/://nameoffoldertocontainer --name nameofcontainer namedockerimg
docker run -dit -p 10000:8080 -v //c/Users/Ronald/myProject/://myProject --name localhost localhost/dev
In a Windows/Mac OS you need to add "//" when specifying the full path of files/folders you want to mount in the container.
Proposed solution (for the moment)
- If you are planning to enhance your developing skills I would suggest to start using Linux OS. The previous steps should work with Ubuntu.
- Edit your Dockerfile and add your project folder. The folder need to be on the same directory of your Dockerfile. Rerun the command to build the image and create your docker container.
docker run -dit -p 8080:8080 --name localhost localhost/dev
boot2docker ip
To get in your container you can use the command:
docker exec -it namecontainer bash
Conclusion
The setup mention is not ideal in a Windows/Mac if your planning to set a Development environment with Docker when your environment is based on iojs. It might be a good use if you will be using different kind of setup.Based on experience if your production environment is based on Ubuntu, you should have Ubuntu for developing to make your life easier.
Idea of this post is not initially make the example work but to give you the idea on how you can use Docker in an Development Environment.
0 comments