Error starting Docker container (WSL, docker-ce, U

2019-08-05 15:03发布

问题:

Microsoft Windows [Version 10.0.17134.285], Ubuntu 16.04 (WSL), docker-ce (stable)

I am following the instructions here - https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly. I opted for "stable" rather than "edge". I mounted the c drive mapping manually with

sudo mkdir /c
sudo mount --bind /mnt/c /c

rather than the WSL config file way, because I wasn't sure if I wanted it for ALL my WSL instances. Other than that, I followed the instructions.

I have started the Docker daemon with

sudo cgroupfs-mount
sudo dockerd -H tcp://0.0.0.0:2375 --tls=false

When I try to start my container with

docker run -d -p 27017:27017 --name testDB mongo:3.4

I get

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused \"rootfs_linux.go:46: preparing rootfs caused \\\"invalid argument\\\"\"": unknown.

and I cannot connect to the MongoDB on the container using localhost:27017.

docker ps -a

shows

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
e115d1c409a3        mongo:3.4           "docker-entrypoint.s…"   6 seconds ago       Created             0.0.0.0:27017->27017/tcp   testDB

and

docker info

shows

Containers: 1
 Running: 0
 Paused: 0
 Stopped: 1
Images: 1
Server Version: 18.06.1-ce
Storage Driver: overlay2
 Backing Filesystem: <unknown>
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Kernel Version: 4.4.0-17134-Microsoft
Operating System: Ubuntu 16.04.5 LTS
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.9GiB
Name: DESKTOP-4F100D9
ID: EFH2:O3RT:3OO4:27P5:ZNK7:N5JW:WE5M:4VSK:QREN:YCV4:GSYG:ZDTR
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No memory limit support
WARNING: No swap limit support
WARNING: No kernel memory limit support
WARNING: No oom kill disable support
WARNING: No cpu cfs quota support
WARNING: No cpu cfs period support
WARNING: No cpu shares support
WARNING: No cpuset support

Any ideas what I did wrong and how to fix it?

(I need to run Docker under Linux(WSL) - I cannot use Docker for Windows because we are using VirtualBox, and Hyper-V is disabled)

回答1:

Currently, you cannot use docker daemon directly from WSL. There are several issues, mostly with networking. It works only for simple images like hello world (Reddit topic)

What you can do, is connect from WSL to docker daemon in windows. So following the tutorial, you mentioned is fine, but if you're running it with VirtualBox you have to either start default machine or create and start a new one. This docker machine will be your daemon.

By default docker-machine command is not working correctly in WSL, but you can make it works by putting this code to e.g. ~/.bashrc file

# Ability to run docker-machine command properly from WSL
function docker-machine()
{
    if [ $1 == "env" ]; then
            docker-machine.exe $1 $2 --shell bash | sed 's/C:/\/c/' | sed 's/\\/\//g' | sed 's:#.*$::g' | sed 's/"//g'
            printf "# Run this command to configure your shell:\n"
            printf "# eval \"\$(docker-machine $1 $2)\"\n"
    else
            docker-machine.exe "$@"
    fi
}
export -f docker-machine

After running source ~/.bashrc or reopening the bash you can run:

  • docker-machine start default - will start machine
  • eval $(docker-machine env default) - will connect your bash session to the machine

and then you should be able to run all the docker stuff like

  • docker ps
  • docker run -it alpine sh
  • docker build
  • etc

The docker machine will run until you either stop it or you shut down your PC. If you open a new bash session (window), you have to run just eval $(docker-machine env default) in order to connect your new session to the machine.

Hope it helps. :)



回答2:

This is a simple solution which is to use Docker on windows in WSL instead.

Just add the following to your WSL .bashrc file.

export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export PATH="$PATH:/mnt/c/Program\ Files/Docker/Docker/resources/bin"
alias docker=docker.exe
alias docker-compose=docker-compose.exe

Reference: https://blog.jayway.com/2017/04/19/running-docker-on-bash-on-windows/