原创

在Mac上开始使用Docker

一、教程网址


二、一键安装docker环境

下载DockerToolbox 工具包:http://get.daocloud.io,下载完成后,点击安装即可。


退出容器:CTRL+Q+P

查看容器:docker ps -a

删除容器:docker rm  容器id

启动容器:docker start [names] 容器名字

删除image:docker rmi [IMAGE ID]


三、创建一个名称为auto-test的docker虚拟机

命令:docker-machine create --driver virtualbox auto-test
chinadeiMac:~ $ docker-machine create --driver virtualbox auto-test
Running pre-create checks...
Creating machine...
(auto-test) Creating VirtualBox VM...
(auto-test) Creating SSH key...
(auto-test) Starting VM...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect Docker to this machine, run: docker-machine env auto-test

四、常见docker命令

         1、查看容器
          命令:docker images,如果需要查看所有容器,加上-a命令

2、打开命令行:创建一个别名为appium的容器,并同时打开/bin/bash
命令:docker run -i -t --name appium ubuntu:13.10  /bin/bash

3、如果已存在shell容器,那么可以去掉--name appium,如下:若退出到host主机中,点击键入exit命令即可。
docker run -i -t ubuntu  /bin/bash

启动一个名称为shell的容器:docker start  appium
进入到已启动的shell容器中:docker attach appium

3、查看当前系统中存在的虚拟机
wanghaifengdeiMac:~ wanghaifeng$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376

4、启动状态为stop的虚拟机
wanghaifengdeiMac:~ wanghaifeng$ docker-machine start default
(default) Starting VM...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.

5、查看设置docker虚拟机的命令
wanghaifengdeiMac:~ wanghaifeng$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/chinaskin/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval "$(docker-machine env default)"

6、查看当前系统中存在的容器
wanghaifengdeiMac:~ wanghaifeng$ eval "$(docker-machine env default)"

7、查看当前系统中存在的容器
wanghaifengdeiMac:~ wanghaifeng$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nginx               latest                9fab4090484a        3 weeks ago         132.8 MB
ubuntu              latest              ca4d7b1b9a51        5 weeks ago         187.9 MB

五、访问容器端口


  1. DOCKER_HOST上启动一个NGINX容器
    $ docker run -d -P --name web nginx
    Normally, the docker run commands starts a container, runs it, and then exits.
    The -d flag keeps the container running in the background after the docker runcommand completes.
    The -P flag publishes exposed ports from the container to your local host; this lets you access them from your Mac.
  2. 通过docker ps命令来查看你运行的容器
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                  NAMES
    5fb65ff765e9        nginx:latest        "nginx -g 'daemon of   3 minutes ago       Up 3 minutes 0.0.0.0:49156->443/tcp, 0.0.0.0:49157->80/tcp   web
    这时候,你就可以看到nginx作为一个进程在运行
  3. 查看容器的端口
    $ docker port web
    443/tcp -> 0.0.0.0:49156
    80/tcp -> 0.0.0.0:49157
    This tells you that the web container’s port 80 is mapped to port 49157 on your Docker host.
  4. Enter the http://localhost:49157 address (localhost is 0.0.0.0) in your browser:

  1. This didn’t work. The reason it doesn’t work is your DOCKER_HOST address is not the localhost address (0.0.0.0) but is instead the address of the your Docker VM.
  2. 获取default虚拟机上的地址
    $ docker-machine ip default
    192.168.59.103
  3. 在你的浏览器中输入http://192.168.59.103:49157
\
  1. 停止或者删除正在运行的nginx容器,通过以下命令
    docker stop web
    docker rm web

七、在容器上挂载卷

下面的一些操作演示说明了这一点
  1. Change to your user $HOME directory.
    $ cd $HOME
  2. Make a new site directory.
    mkdir site
  3. Change into the site directory.
    cd site
  4. Create a new index.html file.
    echo "my new site" > index.html
  5. Start a new nginx container and replace the html folder with your sitedirectory.
    $ docker run -d -P -v $HOME/site:/usr/share/nginx/html \
    --name mysite nginx
  6. Get the mysite container’s port.
    $ docker port mysite
    80/tcp -> 0.0.0.0:49166
    443/tcp -> 0.0.0.0:49165
  7. Open the site in a browser:
  1. Try adding a page to your $HOME/site in real time.
    $ echo "This is cool" > cool.html
  2. 在浏览器中打开一个新的页面
  1. Stop and then remove your running mysite container.
    docker stop mysite
    docker rm mysite


八、卸载 Docker Toolbox

根据以下步骤来下载Docker Toolbox

  1. 查看所有的虚拟机

    $ docker-machine ls
    NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
    dev                 *        virtualbox   Running tcp://192.168.99.100:2376
    my-docker-machine            virtualbox   Stopped

    default virtualbox   Stopped

  2. 删除所有的虚拟机
    $ docker-machine rm dev
    Successfully removed dev

    Removing a machine deletes its VM from VirtualBox and from the~/.docker/machine/machines directory.

  3. Applications文件夹删除Docker Quickstart Terminal and Kitematic程序

  4.  /usr/local/bin文件夹下删除docker, docker-compose, and docker-machine命令

    $ rm /usr/local/bin/docker

  5. 在你的系统上删除~/.docker文件夹


正文到此结束
本文目录