Docker: Difference between revisions

From 太極
Jump to navigation Jump to search
Line 8: Line 8:
* [https://docs.docker.com/installation/windows/#windows Windows]. It will install Boot2Docker management tool with ISO, Virtualbox and MYSYS-git UNIX tools.
* [https://docs.docker.com/installation/windows/#windows Windows]. It will install Boot2Docker management tool with ISO, Virtualbox and MYSYS-git UNIX tools.
* Docker needs Admin right to be installed. However, Virtualbox can be installed by user's account.
* Docker needs Admin right to be installed. However, Virtualbox can be installed by user's account.
* If the installer detects a version of VirtualBox installed, the VirtualBox checkbox will not be checked by default (Windows OS). The VirtualBox cannot be used anymore after updating my VB from 4.3.18 to 4.3.20.
* If the installer detects a version of VirtualBox installed, the VirtualBox checkbox will not be checked by default (Windows OS). The VirtualBox cannot be used anymore after updating my VB from 4.3.18 to 4.3.20. The error may be related to Windows update according to Virtualbox forum.
<pre>
<pre>
Error in supR3HardenedWinReSpawn
Error in supR3HardenedWinReSpawn
</pre>
</pre>
The error may be related to Windows update.
The VB installation messed up. At the end I switch to VMware player. I cannot test Windows's Docker on my Windows machine.
* Note that <span style="color: red">boot2docker cannot be installed/run inside a virtual machine</span>. See [https://github.com/boot2docker/windows-installer/issues/14 this post] and my Virtualbox wiki [[Virtualbox#Install_on_a_Windows_machine|here]]. If we try to launch boot2docker-vm from Virtualbox, we will see a message "This kernel requires an x86-64 CPU, but only detected an i686 CPU."
* Note that <span style="color: red">boot2docker cannot be installed/run inside a virtual machine</span>. See [https://github.com/boot2docker/windows-installer/issues/14 this post] and my Virtualbox wiki [[Virtualbox#Install_on_a_Windows_machine|here]]. If we try to launch boot2docker-vm from Virtualbox, we will see a message "This kernel requires an x86-64 CPU, but only detected an i686 CPU."
* After I switch back to an old version of virtualbox, every thing works again. I can even install Docker successfully.
** Boot2Docker Start icon cannot be run directly because Notepad++ will automatically open it. A possible solution is to go to control panel and change default program for .sh file from Notepad++ to C:\Program Files (x86)\Git\bin\bash.exe.
** The above step does not work well since a terminal appears and disappears quickly.
** A working approach is to open Git Bash from Start menu. And run /c/Program Files/Boot2DockerforWindows/start.sh
** A new VM called 'boot2docker-vm' will be created (we can open VirtualBox Manager to check). The VM has an error on Network>Adapter2>VirtualBox Host-Only Ethernet Adapter #2. So I change it to VirtualBox Host-Only Ethernet Adapter.
** Now it works either I directly click boot2docker-vm VM from VB Manager or use the command start.sh from Git Bash.


= Usage =
= Usage =

Revision as of 15:21, 27 January 2015

Official web page http://docker.io

Installation

Ubuntu

https://docs.docker.com/installation/ubuntulinux

Windows

  • Windows. It will install Boot2Docker management tool with ISO, Virtualbox and MYSYS-git UNIX tools.
  • Docker needs Admin right to be installed. However, Virtualbox can be installed by user's account.
  • If the installer detects a version of VirtualBox installed, the VirtualBox checkbox will not be checked by default (Windows OS). The VirtualBox cannot be used anymore after updating my VB from 4.3.18 to 4.3.20. The error may be related to Windows update according to Virtualbox forum.
Error in supR3HardenedWinReSpawn
  • Note that boot2docker cannot be installed/run inside a virtual machine. See this post and my Virtualbox wiki here. If we try to launch boot2docker-vm from Virtualbox, we will see a message "This kernel requires an x86-64 CPU, but only detected an i686 CPU."
  • After I switch back to an old version of virtualbox, every thing works again. I can even install Docker successfully.
    • Boot2Docker Start icon cannot be run directly because Notepad++ will automatically open it. A possible solution is to go to control panel and change default program for .sh file from Notepad++ to C:\Program Files (x86)\Git\bin\bash.exe.
    • The above step does not work well since a terminal appears and disappears quickly.
    • A working approach is to open Git Bash from Start menu. And run /c/Program Files/Boot2DockerforWindows/start.sh
    • A new VM called 'boot2docker-vm' will be created (we can open VirtualBox Manager to check). The VM has an error on Network>Adapter2>VirtualBox Host-Only Ethernet Adapter #2. So I change it to VirtualBox Host-Only Ethernet Adapter.
    • Now it works either I directly click boot2docker-vm VM from VB Manager or use the command start.sh from Git Bash.

Usage

Basics and docs

https://docs.docker.com/articles/basics/

Note that we need sudo is needed unless it is on a Mac OS.

If docker cannot find an image, it will try to pull it from its repository.

$ sudo docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu' locally
Pulling repository ubuntu
04c5d3b7b065: Download complete 
511136ea3c5a: Download complete 
c7b7c6419568: Download complete 
70c8faa62a44: Download complete 
d735006ad9c1: Download complete 
root@ec83b3ac878d:/# 

A brief intro to docker virtualization

docker search --help
docker search redis
docker search -s 100 redis
docker pull --help
docker pull ubuntu # download all versions of ubuntu
docker images    # available local container images
docker pull centos:latest
docker run --help
cat /etc/issue   # look at the current distr name before running docker
docker run -it centos:latest /bin/bash
                 # create a container & execute as a sudo

cat /etc/redhat-release
yum
cd /home
touch temp.txt
ls
exit

docker ps   # current running processes
docker ps -a # show all processes including closed
docker restart c85850ed0e13
docker ps   # container c85850ed0e13 is running
docker attach c85850ed0e13 # log into the system

ls /home
exit

docker ps -a
docker rm c85850ed0e13 # delete the container

Dockerizing Applications

$ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
$ sudo docker ps -l
$ sudo docker logs insane_babbage
$ sudo docker stop insane_babbage
$ sudo docker ps

The -d flag tells Docker to run the container and put it in the background, to daemonize it.

Web Application/Work with containers

https://docs.docker.com/userguide/usingdocker/

$ sudo docker run -d -P training/webapp python app.py
$ sudo docker run -d -p 5000:5000 training/webapp python app.py
$ sudo docker ps   # find the NAME from the last column
                   # In this case, it is called 'nostalgic_morse'
$ sudo docker port nostalgic_morse 5000
$ sudo docker logs -f nostalgic_morse
$ sudo docker top nostalgic_morse
$ sudo docker stop nostalgic_morse
$ sudo docker ps -l
$ sudo docker start nostalgic_morse
$ sudo docker stop nostalgic_morse
$ sudo docker rm nostalgic_morse

Note: Always remember that deleting a container is final!

Work with container images

https://docs.docker.com/userguide/dockerimages/

$ sudo docker search sinatra
$ sudo docker pull training/sinatra
$ sudo docker run -t -i training/sinatra /bin/bash
$ sudo docker commit -m="Added json gem" -a="Kate Smith" 0b2616b0e5a8 ouruser/sinatra:v2
$ sudo docker images

$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile
$ sudo docker build -t="ouruser/sinatra:v2" .
$ sudo docker push ouruser/sinatra
$ sudo docker rmi training/sinatra
  • The above exercise works on my home computer (64-bit Ubuntu 12.04). However when I want to remove an image, I need to do something extra.
$ sudo docker rmi training/sinatra
Error response from daemon: Conflict, cannot delete f0f4ab557f95 because the container bc5175998df3 is using it, use -f to force
FATA[0000] Error: failed to remove one or more images 
$ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ 
$ sudo docker rm $(sudo docker ps -aq)
bc5175998df3
b97cb467529c
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ouruser/sinatra     v2                  bd6395724621        9 minutes ago       316.3 MB
ubuntu              14.04               ed5a78b7b42b        4 days ago          188.3 MB
ubuntu              latest              ed5a78b7b42b        4 days ago          188.3 MB
training/sinatra    latest              f0f4ab557f95        6 months ago        447 MB
$ sudo docker rmi training/sinatra
Untagged: training/sinatra:latest
Deleted: f0f4ab557f954f3e04177663a3af90e88641bcdcce1f02ac900dbd9768ef4945
...
Deleted: 3e76c0a80540a0d36493ae7110796fc92f559a191454e3ac19c1d4c650bdd9e0
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ouruser/sinatra     v2                  bd6395724621        10 minutes ago      316.3 MB
ubuntu              latest              ed5a78b7b42b        4 days ago          188.3 MB
ubuntu              14.04               ed5a78b7b42b        4 days ago          188.3 MB
$ sudo docker rmi ouruser/sinatra
Error response from daemon: No such image: ouruser/sinatra:latest
FATA[0000] Error: failed to remove one or more images   
$ sudo docker rmi ouruser/sinatra:v2
Untagged: ouruser/sinatra:v2
Deleted: bd6395724621a89384ec58c116ce113ae8371f31f20f0adc540bbc76c6049d12
...
Deleted: 41ce107b0a0bb6a70834477e3b550386fc453de363d8a20f0579df055e9ece50
  • I get an error when I try to launch sinatra on my 32-bit ubuntu (Docker can only be installed through apt-get on 32-bit)
$ sudo docker run -t -i training/sinatra /bin/bash
2014/12/31 02:43:26 exec format error
  • I get lots of errors when I run docker build command on my 64-bit Ubuntu.
$ sudo docker build -t="ouruser/sinatra:v2" .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:14.04
Pulling repository ubuntu
ed5a78b7b42b: Download complete 
511136ea3c5a: Download complete 
fe95bf7d5f50: Download complete 
9a4594fe74ea: Download complete 
8c4b1edcceea: Download complete 
 ---> ed5a78b7b42b
Step 1 : MAINTAINER Kate Smith <[email protected]>
 ---> Running in 63614919cafd
 ---> 5bac5869eb36
Removing intermediate container 63614919cafd
Step 2 : RUN apt-get update && apt-get install -y ruby ruby-dev
 ---> Running in 68e8ccfa5f7c
Err http://archive.ubuntu.com trusty InRelease
  
Err http://archive.ubuntu.com trusty-updates InRelease
  
Err http://archive.ubuntu.com trusty-security InRelease
  
Err http://archive.ubuntu.com trusty Release.gpg
  Could not resolve 'archive.ubuntu.com'
Err http://archive.ubuntu.com trusty-updates Release.gpg
  Could not resolve 'archive.ubuntu.com'
Err http://archive.ubuntu.com trusty-security Release.gpg
  Could not resolve 'archive.ubuntu.com'
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/InRelease  

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/InRelease  

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/InRelease  

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/Release.gpg  Could not resolve 'archive.ubuntu.com'

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/Release.gpg  Could not resolve 'archive.ubuntu.com'

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/Release.gpg  Could not resolve 'archive.ubuntu.com'

W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists...
Building dependency tree...
Reading state information...
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 ruby-dev : Depends: ruby1.9.1-dev (>= 1.9.3.194-1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
2014/12/30 16:03:21 The command [/bin/sh -c apt-get update && apt-get install -y ruby ruby-dev] returned a non-zero code: 100

hub.docker.com

Link containers together

https://docs.docker.com/userguide/dockerlinks/

Manage data in containers

https://docs.docker.com/userguide/dockervolumes/

Working with Docker hub

https://docs.docker.com/userguide/dockerrepos/

Misc

Vagrant vs Docker

Access the internet from the container

Run the container with the '--net=host' option

sudo docker run --net=host -it ubuntu /bin/bash

Use with R

Create a new directory and a new file 'Dockerfile' with the content

FROM debian:testing
MAINTAINER Dirk Eddelbuettel [email protected]
## Remain current
RUN apt-get update -qq
RUN apt-get dist-upgrade -y
RUN apt-get install -y --no-install-recommends r-base r-base-dev r-recommended littler
RUN ln -s /usr/share/doc/littler/examples/install.r /usr/local/bin/install.r

Then run the following to do some exercise.

sudo docker build -t debian:testing-add-r . # create an image based on the above Dockerfile
wget http://cran.r-project.org/src/contrib/sanitizers_0.1.0.tar.gz
sudo docker run -v `pwd`:/mytmp -t 21b6a9e8b9e8 R CMD check --no-manual --no-build-vignettes /mytmp/sanitizers_0.1.0.tar.gz
sudo docker run -v `pwd`:/mytmp -t 21b6a9e8b9e8 Rdevel CMD check --no-manual --no-build-vignettes /mytmp/sanitizers_0.1.0.tar.gz

sudo docker search eddelbuettel
sudo docker pull eddelbuettel/docker-ubuntu-r   # default tag is 'latest'; actually older than the other tags
sudo docker images eddelbuettel/docker-ubuntu-r # see the tag column
sudo docker pull eddelbuettel/docker-ubuntu-r:add-r # the tag name can only be obtained from hub.docker.com
sudo docker images eddelbuettel/docker-ubuntu-r # see the tag column
sudo docker pull eddelbuettel/docker-ubuntu-r:add-r-devel
sudo docker images eddelbuettel/docker-ubuntu-r # see the tag column
sudo docker run -v `pwd`:/mytmp -t 54d865dbd2c9 R CMD check --no-manual --no-build-vignettes /mytmp/sanitizers_0.1.0.tar.gz

sudo docker run -t -i eddelbuettel/docker-ubuntu-r /bin/bash
$ sudo docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
debian                         testing-add-r       21b6a9e8b9e8        28 minutes ago      572.2 MB
ubuntu                         14.04               ed5a78b7b42b        4 days ago          188.3 MB
ubuntu                         latest              ed5a78b7b42b        4 days ago          188.3 MB
debian                         testing             88ba2870bfbe        7 weeks ago         154.7 MB
eddelbuettel/docker-ubuntu-r   add-r-devel         c998a74a1fb4        11 weeks ago        460.4 MB
eddelbuettel/docker-ubuntu-r   add-r               54d865dbd2c9        11 weeks ago        460.4 MB
eddelbuettel/docker-ubuntu-r   latest              a7cd5ddeb98e        5 months ago        515.4 MB