[基础]Mac OS下使用Docker

背景:Docker引擎能够运行在Linux内核上,但是不能直接运行在Mac OS上。那么如何在Mac OS上使用Docker呢?解决方案时候使用轻量级的VM,在VM上使用Docker。


目的:在docker中运行web程序

步骤:

1、下载Docker for OS X Installer并进行安装;

2、虚拟机的启动与初始化;

boot2docker init
boot2docker start
export DOCKER_HOST=tcp://$(boot2docker ip 2>/dev/null):2375

3、docker version查看Docker的信息;

如果docker version时出现timeout的问题,修改DOCKER_HOST后问题解决。

注意:export DOCKER_HOST=tcp://127.0.0.1:2375

4、使用docker来打印Hello World;

docker search tutorial
docker pull learn/tutorial
docker run learn/tutorial echo "hello world"

learn/tutorial为一个镜像。


5、登录镜像的bash

docker run -i -t learn/tutorial /bin/bash

6、在镜像中安装apache

apt-get update
apt-get install apache2 curl
apachectl start
curl http://localhost

保存镜像:

首先MAC OS中另外打开一个Terminal查看此运行镜像的container id,docker ps查看进项id,如7162451b3fef,

再使用如下命令保存镜像:

docker commit 716 learn/tutorial

7、使得Mac OS能够访问Docker中的WEB服务器。(端口映射)

boot2docker ssh -L 50080:localhost:40080    //登录VM,并将Mac Os端口与VM端口进行映射
docker run -i -t -p 40080:80 learn/tutorial  //登录Docker镜像,将VM端口与Docker镜像中端口映射
apachectl start

此时在Mac OS中使用http://localhost:50080/就可以访问Docker镜像中的WEB服务器了。

The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
the -d flag which tells Docker to run the container in the background. The -P flag is new and tells Docker to map any required network ports inside our container to our host.

参考:http://docs.docker.com/installation/mac/




猜你喜欢

转载自blog.csdn.net/wendll/article/details/38798631