Recently, it has become more common to develop in various languages depending on the purpose such as go, python, node, so I decided to create an execution environment on the VM.
Build boot2docker (core Linux) in VirtualBox and build the following execution environment ・ Go lang ・ Because. js (py)
[Prepared environment] ・ VirtualBox ・ Vagrant v1.7.1
I also have VMware Fusion, so I wanted to check that as well, A license is required, so this time it's a pass. Reference: https://www.vagrantup.com/vmware $ 79 (≒ 9,384 yen) A little expensive ...
Download and install dmg from official site .
Download and install dmg for OS X from official site .
$ mkdir -p /Users/<USER NAME>/Documents/Vagrant/boot2docker
$ cd /Users/<USER NAME>/Documents/Vagrant/boot2docker
#boot2 docker boot
$ vagrant init mitchellh/boot2docker
(If you don't need ISO, go to boot)
Install Packer from here
Run the following from the terminal after installation
$ cd /Users/<USER NAME>/Documents/Vagrant
$ git clone https://github.com/mitchellh/boot2docker-vagrant-box.git
$ cd boot2docker-vagrant-box.git
$ vagrant up
$ vagrant ssh -c 'cd /vagrant && sudo ./build-iso.sh'
$ vagrant destroy --force
$ packer build -only=virtualbox-iso template.json
When you do the above ・ B2d.iso ・boot2docker_virtualbox.box -Boot2docker_virtualbox.iso will be created.
Set up port forwarding
$vi Vagrantfile
: Omitted
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Add port forwarding
config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
: Omitted
boot2 docker
$ vagrant up
$ vagrant ssh
Next, persist the data created by boot2docker.
Execute the following command on boot2docker
$ sudo su -
# cat > /var/lib/boot2docker/bootlocal.sh <<EOF
> echo "tar cf /var/lib/boot2docker/userdata.tar . -C /home/docker/" >> /opt/shutdown.sh
> EOF
# chmod +x /var/lib/boot2docker/bootlocal.sh
# sudo reboot
【Verification】 Ssh from mac
$ vagrant ssh
# pssword : tcuser
#Working with boot2docker
$ touch test
$ ls -l
-rw-r--r-- 1 docker staff 0 Dec 13 13:22 test
$ sudo reboot
Ssh again from mac
$ vagrant ssh
# pssword : tcuser
boot2docker
$ ls -l
-rw-r--r-- 1 docker staff 0 Dec 13 13:22 test
#Confirm that it exists
Since it's a big deal, I created an image from the Dockerfile
$ mkdir golang
$ cd golang
$ vi Dockerfile
#
# Go Dockerfile
#
# https://github.com/dockerfile/go
#
# Pull base image.
FROM dockerfile/ubuntu
# Install Go
RUN \
mkdir -p /goroot && \
curl https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz | tar xvzf - -C /goroot --strip-components=1
# Set environment variables.
ENV GOROOT /goroot
ENV GOPATH /gopath
ENV PATH $GOROOT/bin:$GOPATH/bin:$PATH
# Define working directory.
WORKDIR /gopath
# Define default command.
CMD ["bash"]
Create docker image
$ docker build -t ubuntu/golang .
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu/golang latest ff635337559a 3 minutes ago 614 MB
dockerfile/ubuntu latest c8f87bf54eb2 8 days ago 414.6 MB
Try Hello World with go on docker.
$ docker run -i -p 80:80 -t ubuntu/golang /bin/bash
$ go version
go version go1.3.1 linux/amd64
$ vi testserver.go
package main
import (
"fmt"
"net/http"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello Go lang")
}
func main() {
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":80", nil)
}
#Since it is a test, it is executed without compilation.
$ go run testserver.go
Access with a browser from Mac and check the execution
Since the python environment is the base of nodejs, create only the Nodejs environment
$ mkdir ~/nodejs
$ cd ~/nodejs
$ vi Dockerfile
#
# Node.js Dockerfile
#
# https://github.com/dockerfile/nodejs
#
# Pull base image.
FROM dockerfile/python
# Install Node.js
RUN \
cd /tmp && \
wget http://nodejs.org/dist/node-latest.tar.gz && \
tar xvzf node-latest.tar.gz && \
rm -f node-latest.tar.gz && \
cd node-v* && \
./configure && \
CXX="g++ -Wno-unused-local-typedefs" make && \
CXX="g++ -Wno-unused-local-typedefs" make install && \
cd /tmp && \
rm -rf /tmp/node-v* && \
npm install -g npm && \
echo -e '\n# Node.js\nexport PATH="node_modules/.bin:$PATH"' >> /root/.bashrc
# Define working directory.
WORKDIR /data
# Define default command.
CMD ["bash"]
Create docker image
$ docker build -t ubuntu/nodejs .
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu/nodejs latest 60d7f5969d7a 30 seconds ago 496.6 MB
ubuntu/golang latest 42a919d3c0ea 23 minutes ago 614 MB
dockerfile/python latest f08c82e36872 8 days ago 471 MB
dockerfile/ubuntu latest c8f87bf54eb2 8 days ago 414.6 MB
$ docker run -i -p 80:80 -t ubuntu/nodejs /bin/bash
$ npm version
{ http_parser: '1.0',
node: '0.10.33',
v8: '3.14.5.9',
ares: '1.9.0-DEV',
uv: '0.10.29',
zlib: '1.2.3',
modules: '11',
openssl: '1.0.1j',
npm: '2.1.12' }
#Create server
$ vi app.js
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World node.js');
});
server.listen(80);
Test python in the same environment
# ubuntu/Run with nodejs
$ python --version
Python 2.7.6
$ vi index.html
<html>
<body>
Hello Python
</body>
</html>
$ python -m SimpleHTTPServer 80
Since docker keeps the difference file after startup, delete it when it is no longer needed.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8675a8897319 ubuntu/nodejs:latest "/bin/bash" 8 seconds ago Exited (0) 7 seconds ago hungry_carson
f71e1d22b5e6 ubuntu/golang:latest "/bin/bash" 56 seconds ago Exited (0) 54 seconds ago prickly_shockley
#Specify the ID of the unnecessary container
$ docker rm 8675a8897319 f71e1d22b5e6
#When deleting an image
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu/nodejs latest 60d7f5969d7a About an hour ago 496.6 MB
ubuntu/golang latest 42a919d3c0ea About an hour ago 614 MB
dockerfile/python latest f08c82e36872 8 days ago 471 MB
dockerfile/ubuntu latest c8f87bf54eb2 8 days ago 414.6 MB
$ docker rmi < IMAGE ID >
When deleting containers and images at once
# remove container
$ docker rm `docker ps -a -q`
# remove images
$ docker rmi $(docker images | awk '/^<none>/ { print $3 }')
Source control on GitHub for multi-location development and agile development If you manage the environment with Dockerfile, you can develop suitable for mobility.
It's about time for Mac to be the limit, so I'd like to build boot2docker with bare metal.
Recommended Posts