Docker for Windows setup

what

While developing on Windows, sometimes I need to switch to a *nix-like environment. Right now Docker has released the Docker for Windows. But the documents are lacking and I did not use Docker too much before, so sometimes I got confused, and here are some notes.

docker installation

We can download the stable or edge version.

Moreover, we need a Windows 10 pro + with the Hyper-V virtual machine engine enabled. Virtualbox is unusable while using Docker based on Hyper-V. And we may need to configure the Virtual Switch before or after installing Docker, unless we do not have any configuration changed before.

After installation, we get a MobyLinuxVM virtual machine in Hyper-V Manager. And also the disk is defaultly called MobyLinuxVM.vhdx, located at:

%PUBLIC%\Documents\Hyper-V\Virtual hard disks

The virtual disk file is always increasing and could become really huge.

docker mirrors / docker 镜像

[email protected]C or 网易蜂巢的 DockerHub 镜像.

{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

start Docker and manage file sharing

If we want to mount a directory / folder, we need to manage the local drive(s) available to the containers. Only after that we can share the directories on that drive to containers. It’s confused at first time, but after figuring out the concepts it’s easy to understand.

  1. Double-click the “Docker for Windows” in the start menu.

  2. Right-click on the Docker mascot on the notification area on the taskbar, then “Settings…” then “Manage shared drives…” then select the drives then “Ok”. User password is needed.

start, commit, shutdown the images

On Windows, PowerShell (or Git Bash) (not cmd though) is the (better) terminal in *nix replacement. After double-clicking the “Docker for Windows” in the start menu, we could run this inside PowerShell to check the status.

docker info
docker ps
docker images

jupyter/datascience-notebook is one of the official docker image by Python Jupyter Project. We could (should) use it if we are doing Python / R / data science development. But pay attention that the image is huge (5 ~ 10 GB).

docker pull jupyter/datascience-notebook

After pulling the images, we could start the images. The simpliest way is

docker run -d -p 8888:8888 jupyter/datascience-notebook

But if we want to mount the directory, we should use -v from-dir:to-dir, and if the directory has the white spaces, we should use -v 'from dir:to dir'. This really confused me in the beginning, because the quotation marks should wrap the whole parameter string, instead of the substring before the colon! Moreover, if using jupyter/datascience-notebook, we should mount the folder as /home/jovyan/work. For example,

docker run -d -p 8888:8888 -v 'C:/all projects:/home/jovyan/work' jupyter/datascience-notebook
# abcde... # container id

According to the #211, if we want to grant the sudo privilege, we could add --user root -e GRANT_SUDO=yes:

docker run -d -p 8888:8888 --user root -e GRANT_SUDO=yes jupyter/datascience-notebook

Attention that in this case we could login the system as jovyan and run sudo apt-get update inside the terminal opened in jupyter notebook in the browser, but if we login the system directly using docker exec -it, we would become the user root.

After some manipulations, we could use commit to save a snapshot of the container.

docker commit $(docker ps -aq) testcontainer

If we want to shut down the container, use ‘rm’, optionally with -f:

docker rm -f $(docker ps -aq)

After running rm in the PowerShell, we could exit Docker for Windows.

running

While running jupyter/datascience-notebook, we could “log in” to the bash inside:

docker exec -it $(docker ps -aq) /bin/bash

Or access the jupyter notebook using browser using one of the addresses:

http://docker:8888
http://docker.local:8888

Attention: localhost is not the right anwser at least on the Windows! It is another confusion at the first time.

after multiple docker pull

docker images may return some <none> unused images. As a result, we need to remove them:

docker rmi $( docker images -q -f dangling=true)

in the r kernel notebooks

We have to set something to make it working properly:

options(repos = c(CRAN = "https://cran.rstudio.com")) # CRAN
Sys.setenv(TAR = '/bin/tar') # tar command

End

So please start happy hacking, using the Linux developement enviroment on Windows.