Mac中下载安装docker及使用教程

来源:互联网 发布:上海聚宝网络 编辑:程序博客网 时间:2024/06/05 05:15

1、下载安装

官网下载安装docker community edition版
安装没啥好讲的,下载拖进去安装就行了。
安装成功之后,在终端输入命令docker version可查看安装的版本
执行命令docker run hello-world来验证docker可以正常拉取镜像按照期望的运行
更多docker命令可以自己去查看
test version of docker ,docker-compose,docker-machine use:

docker --version
docker-compose --version
docker-machine --version

2、使用

(1)docker run -d -p 80:80 --name webserver nginx开启一个dockerized web服务,如果没有在本地找到镜像,docker会从docker hub拉取
In a web browser, go to http://localhost/ to bring up the home page.just like:
这里写图片描述
(2)run docker ps to see details on the webserver container

(3)Stop or remove containers and images
webserver will continue to run in the container除非你stop或remove掉它

docker stop webserver to stop the webserver
docker start webserver to start it
一个stop的container不会在docker ps命令之后显示,可以通过docker ps -a查看

docker rm -f webserver To stop and remove the running container,this will移除container,而不是nginx镜像
docker images list local images
不必去删除镜像,要不然用的时候还要再从docker hub去拉,实在不用的docker rmi imageID/imageName来删除镜像,例如:docker rmi nginx

part2:containers

1、Give your environment a quick test run to make sure you’re all set up:

docker run hello-world

2、Define a container with a Dockerfile
创建一个新文件名叫Dockerfile,把下面的代码放进去:

# Use an official Python runtime as a parent imageFROM python:2.7-slim# Set the working directory to /appWORKDIR /app# Copy the current directory contents into the container at /appADD . /app# Install any needed packages specified in requirements.txtRUN pip install -r requirements.txt# Make port 80 available to the world outside this containerEXPOSE 80# Define environment variableENV NAME World# Run app.py when the container launchesCMD ["python", "app.py"]

把下面两个文件requirements.txt和app.py跟Dockerfile放到同一文件夹下(该文件夹要在docker的共享路径中)。
requirements.txt

FlaskRedis

app.py

from flask import Flaskfrom redis import Redis, RedisErrorimport osimport socket# Connect to Redisredis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)app = Flask(__name__)@app.route("/")def hello():    try:        visits = redis.incr("counter")    except RedisError:        visits = "<i>cannot connect to Redis, counter disabled</i>"    html = "<h3>Hello {name}!</h3>" \           "<b>Hostname:</b> {hostname}<br/>" \           "<b>Visits:</b> {visits}"    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)if __name__ == "__main__":    app.run(host='0.0.0.0', port=80)

runpip install -r requirements.txt installs the Flask and Redis libraries for Python
3、Build the app
此时该文件夹下有三个文件,在该文件夹下run the build command to creates a Docker imagedocker build -t friendlyhello .

It’s in your machine’s local Docker image registry:

$ docker imagesREPOSITORY            TAG                 IMAGE IDfriendlyhello         latest              326387cea398

4、Run the app
Run the app, mapping your machine’s port 4000 to the container’s EXPOSEd port 80 using -p:
将container的80端口mapping到本地电脑的4000端口,可通过在本地浏览器输入http://localhost:4000查看container中该程序的执行结果(在这儿这样映射只是为了看差别)。

docker run -p 4000:80 friendlyhello

也可以用curl命令看到同样的内容:

$ curl http://localhost:4000<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

Hit CTRL+C in your terminal to quit.

run the app in the background, in detached mode:

docker run -d -p 4000:80 friendlyhello

通过docker ps查看后台运行的container

$ docker psCONTAINER ID        IMAGE               COMMAND             CREATED1fa4ab2cf395        friendlyhello       "python app.py"     28 seconds ago

然后可以根据id关掉它:

docker stop 1fa4ab2cf395

4、Share your image

A registry is a collection of repositories, and a repository is a collection of images。

Log in with your Docker ID:

docker login

Tag the image:
The notation for associating a local image with a repository on a registry is username/repository:tag. The tag is optional, but recommended, since it is the mechanism that registries use to give Docker images a version.
The syntax of the command is:

docker tag image username/repository:tag

For example:

docker tag friendlyhello username/get-started:part1

Run docker images to see your newly tagged image. (You can also use docker image ls.)

6、Publish the image
Upload your tagged image to the repository:

docker push username/repository:tag

If the image isn’t available locally on the machine, Docker will pull it from the repository.

$ docker run -p 4000:80 john/get-started:part1

Note: If you don’t specify the :tag portion of these commands, the tag of :latest will be assumed, both when you build and when you run images. Docker will use the last version of the image that ran without a tag specified (not necessarily the most recent image).

Part 3: Services

docker run -p 80:80 username/get-started:part1
container的80端口映射到本机的80端口,因此可通过 visit http://localhost:80.访问到

docker run -it ubuntu bash进入一个docker容器
docker ps 列出所有正在运行的容器
docker ps -a 列出所有的容器(包括停止运行的)
docker run -i -t imageName开启镜像,进入容器环境
-d 标识是让 docker 容器在后台运行。
-P 标识通知 Docker 将容器内部使用的网络端口映射到我们使用的主机上
-i
-t

原创粉丝点击