美文网首页
Docker实践

Docker实践

作者: mihope | 来源:发表于2018-12-13 23:30 被阅读3次

Docker实践

命令方式

$ docker search nginx
# 拉取镜像
$ docker pull nginx
# 生成容器,并后台运行
$ docker run -p 8080:80 -d nginx
# 列出正在运行的容器
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
90f388b543ab        nginx               "nginx -g 'daemon of…"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   jolly_raman
# 复制当前目录下的index.html到镜像/usr/share/nginx/html目录下
# 本地主机上访问localhost:8080会看到新的index.html内容
$ docker cp index.html 90f388b543ab://usr/share/nginx/html
# 关闭容器
$ docker stop 90f388b543ab
# 启动容器
$ docker start 90f388b543ab
# 保存修改后的容器为一个新的镜像,名称是nginx-fun-index
$ docker commit -m 'alter index page' 90f388b543ab nginx-fun-index
# 删除container
$ docker rm 90f388b543ab
# 删除image
$ docker rmi nginx

Dockerfile方式

dockerfile-1

$ cd /Users/zhangsheng/docker/d1
$ touch Dockerfile
$ vi Dockerfile
FROM alpine:latest
MAINTAINER zhsheng
CMD echo "hello docker!"
# -t Name and optionally a tag in the 'name:tag' format
$ docker build -t hello_docker:v1 .
$ docker run hello_docker:v1
# 删除也要带上标签名,默认是latest
$ docker rmi hello_docker:v1

dockerfile-2

# 基础镜像
FROM ubuntu
# 作者
MAINTAINER zhsheng
# 执行的命令
RUN apt-get update
RUN apt-get install -y nginx
# 拷贝文件
COPY index.html /var/www/html
# 容器入口
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
# 暴露的端口
EXPOSE 80
$ docker build -t hello-nginx .
$ docker run -d -p 80:80 hello-nginx
$ curl http://localhost

dockerfile-3

创建/Users/zhangsheng/go/workplace/src/docker_demo/main.go

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
    })

    http.ListenAndServe(":8080", nil)
}

创建Dockerfile

FROM golang:alpine

ADD . /go/src/go_demo

RUN go install go_demo

ENTRYPOINT /go/bin/go_demo

EXPOSE 8080

执行命令

$ cd /Users/zhangsheng/go/workplace/src/docker_demo/
$ docker build -t go_demo .
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
go_demo             latest              ba92d5b3bbba        3 seconds ago       317MB
$ docker run --publish 6060:8080 --name go_demo_container --rm go_demo
$ curl http://localhost:6060/hello/go
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b3813fd56db9        go_demo             "/bin/sh -c /go/bin/…"   11 minutes ago      Up 10 minutes       0.0.0.0:6060->8080/tcp   go_demo_container
 $ docker exec -it b3813fd56db9 /bin/sh

docker-compose方式

相关文章

网友评论

      本文标题:Docker实践

      本文链接:https://www.haomeiwen.com/subject/jmdihqtx.html