Create execution environment for each language with boot2docker

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 ...

Install Vagrant

Download and install dmg from official site .

VirtualBox installation

Download and install dmg for OS X from official site .

Preparing boot2docker

$ mkdir -p /Users/<USER NAME>/Documents/Vagrant/boot2docker
$ cd /Users/<USER NAME>/Documents/Vagrant/boot2docker

#boot2 docker boot
$ vagrant init mitchellh/boot2docker

When creating an ISO image of 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.


boot2 docker boot

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

スクリーンショット 2014-12-13 22.11.15.png

boot2docker data persistence

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

Create docker image (Golang)

Since it's a big deal, I created an image from the Dockerfile

go lang

$ 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

スクリーンショット 2014-12-14 0.10.18.png


Create docker image (nodejs, python)

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);

スクリーンショット 2014-12-14 0.27.25.png

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

スクリーンショット 2014-12-14 0.25.21.png


Delete unnecessary containers for docker

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

Create execution environment for each language with boot2docker
Create a Python execution environment for Windows with VScode + Remote WSL
Build a python environment for each directory with pyenv-virtualenv
Use Python installed with pyenv for PL / Python execution environment
Building an environment for natural language processing with Python
Create an environment for test automation with AirtestIDE (Tips)
Create an environment with virtualenv
Create an environment for "Deep Learning from scratch" with Docker
Behavior in each language when coroutines are reused with for
Environment construction, simple confirmation and skill test for each language
Create a virtual environment with Python!
Build PyPy execution environment with Docker
Build IPython Notebook environment with boot2docker
Switch the package to be installed for each environment with poetry
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a USB boot Ubuntu with a Python environment for data analysis
Execution environment test after Go language installation
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Create Python + uWSGI + Nginx environment with Docker
Create a virtual environment with Python_Mac version
Limit ssh with iptables for each user
Frequently used syntax memorandum for each language
Switch the module to be loaded for each execution environment in Python
Creating an environment for OSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant
Build a C language development environment with a container
Prepare the execution environment of Python3 with Docker
Prepare a programming language environment for data analysis
Create a LINE BOT with Minette for Python
Building an Anaconda environment for Python with pyenv
Create the strongest calculator environment with Sympy + Jupyter
Create a virtual environment with conda in Python
Create a python3 build environment with Sublime Text3
Various commands for building an environment with Apache
Create a dashboard for Network devices with Django!
Create Nginx + uWSGI + Python (Django) environment with docker
Image Processing with Python Environment Setup for Windows
[Python] Create an asynchronous task execution environment + monitoring environment
Commands for creating a python3 environment with virtualenv
Rollback DB for each test with Flask + SQLAlchemy
Try building an environment for MayaPython with VisualStudioCode
Create a Python execution environment on IBM i
Build PyPy and Python execution environment with Docker
Build a python execution environment with VS Code
I tried to create a reinforcement learning environment for Othello with Open AI gym
How to set the development environment for each project with VSCode + Python extension + Miniconda
Set up a development environment for natural language processing
Extract N samples for each group with Pandas DataFrame
Python 3.4 Create Windows7-64bit environment (for financial time series analysis)
Create a virtual environment with Anaconda installed via Pyenv
Display the integrated temperature for each field with Z-GIS
Create a python development environment with vagrant + ansible + fabric
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
code-server Online environment (2) Create a virtual network with Boto3
3. Natural language processing with Python 4-1. Analysis for words with KWIC
Create an environment for MkDocs on Amazon Linux (attempted)
Create a Layer for AWS Lambda Python with Docker
Preparing the execution environment of PyTorch with Docker November 2019
Create an OpenAI Gym environment with bash on Windows 10