Docker配置

基本命令

命 令 用 途
docker pull 获取image
docker build 创建image
docker images 列出image
docker image rm 删除image
docker run 运行comtainer
docker ps 列出container
docker container stop 停止container
docker tag 设置镜像TAG
docker push 上传镜像到docker仓库

Dockerfile 语法

命 令 用 途
FROM 基于image
RUN 执行命令
ADD 添加文件
COPY 拷贝文件
CMD 执行控制台命令
EXPOSE 暴露端口
WORKDIR 指定路径
MAINTAINER 维护者
ENV 指定环境变量
ENTRYPOINT 容器入口
USER 指定用户
VOLUME mount point

Docker 入门案例

  • 下载依赖镜像:docker pull tomcat
> docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
cc1a78bfd46b: Pull complete
d2c05365ee2a: Pull complete
231cb0e216d3: Pull complete
da23e1e20eae: Pull complete
9d809d99b239: Pull complete
10c61bb6d245: Pull complete
50578eb745b9: Pull complete
85082b9ab294: Pull complete
d34391598837: Pull complete
Digest: sha256:22263c7c58e5397b29d02f88e39e7f2e18943f5bce6682be66f2298afee75769
Status: Downloaded newer image for tomcat:latest
d34391598837: Download complete
  • 查看镜像docker images
> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              61205f6444f9        11 days ago         467MB
  • 启动镜像(生成容器)docker run
> docker run -p 8888:8080 -d tomcat
296ad741e01b5146050b5ef359c61e704e89f035687d1aacd5e051827d3aad5a
  • 查看运行容器 docker ps
> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
296ad741e01b        tomcat              "catalina.sh run"   4 minutes ago       Up 4 minutes        0.0.0.0:8888->8080/tcp   elastic_davinci
  • 进入容器
> docker exec -it 296ad741e01b bash
root@296ad741e01b:/usr/local/tomcat#
root@296ad741e01b:/usr/local/tomcat# pwd
/usr/local/tomcat
root@296ad741e01b:/usr/local/tomcat# exit
exit
  • 停止容器 docker stop
> docker stop 296ad741e01b
296ad741e01b

构建自己的镜像

  • 新建 Dockerfile 文件:

依赖tomcat镜像,拷贝war包到webapps下,查看tomcat镜像webapps路径方式参考 进入容器

FROM tomcat
MAINTAINER xwine
COPY jpress.war /usr/local/tomcat/webapps
  • 构建镜像
> docker build -t jpress:latest .
Sending build context to Docker daemon  20.81MB
Step 1/3 : FROM tomcat
 ---> 61205f6444f9
Step 2/3 : MAINTAINER xwine
 ---> Using cache
 ---> b69cc1bea65f
Step 3/3 : COPY jpress.war /usr/local/tomcat/webapps
 ---> Using cache
 ---> 91b788fb5b7c
Successfully built 91b788fb5b7c
Successfully tagged jpress:latest