新建一个路径,存放源代码,Dockerfile文件。
$ mkdir /home/joe/Templates/Image
新建一个python的hello.py文件。
$ cd /home/joe/Templates/Image
$ touch hello.py
代码写入hello.py文件。
print ("Hello, world")
运行hello.py文件。
$ python hello.py
Hello, world
新建一个Docker的"Dockerfile"文件——注意,没有后缀名,全名是"Dockerfile"。
"Dockerfile"包含三行代码:第一行代码FROM python:2.7.12表示运行commands所需的环境,通常该环境在官方Docker Hub中有相应的镜像文件。比如,这里的运行print (“Hello, world”)指令所需的环境就是python编译器,其中我们制定编译器版本为2.7.12。
第二行代码COPY . /Image告诉Docker编译器源文件"hello.py"和"Dockerfile"文件的所在路径。
第三行代码CMD [“python”, “/Image/hello.py”]表示要执行的执行。
FROM python:2.7.12
COPY . /Image
CMD ["python", "/Image/hello.py"]
进入源代码"hello.py"和"Dockerfile"文件的根路径。
$ cd /home/joe/Templates/Image
通过docker build .命令生成Docker镜像。注意:命令是"docker build .",不是"docker build"——末尾记得加上英文句号。-t 后面的参数表示要生成的镜像名。
$ docker build -t first-image .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM python:2.7.12
---> 0900c636e10d
Step 2/3 : COPY . /Image
---> 6aaa78c0aca7
Step 3/3 : CMD ["python", "/Image/hello.py"]
---> Running in e4a7058173c8
Removing intermediate container e4a7058173c8
---> 00662f8af726
Successfully built 00662f8af726
Successfully tagged first-image:latest
查看本地生成的镜像first-image。
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
first-image latest 00662f8af726 5 minutes ago 676MB
执行刚刚的first-image镜像文件。
实例化一个container容器。屏幕输出的“Hello, world”就是该实例化container容器显示的。更多关于交互式/非交互式的Docker镜像执行方式查看Docker镜像交互式/非交互式执行方式。
$ docker run -it first-image
Hello, world
Docker的镜像创建,执行完毕。
新建一个路径,存放源代码,Dockerfile文件。
$ mkdir /home/joe/Templates/Image
新建一个Docker的"Dockerfile"文件——注意,没有后缀名,全名是"Dockerfile"。其中,系统密码是 “root”。
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y openssh-server
RUN apt-get install -y openmpi-bin
RUN mkdir /var/run/sshd
RUN echo 'root:root' |chpasswd
RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN mkdir /root/.ssh
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
进入"Dockerfile"文件的所在路径。
$ cd /home/joe/Templates/Image
通过docker build .命令生成Docker镜像。注意:命令是"docker build .",不是"docker build"——末尾记得加上英文句号。-t 后面的参数表示要生成的镜像名。
$ docker build -t ubuntu16.04-sshd .
成功生成一个名为 “ubuntu16.04-sshd” 的镜像。
登录Docker Hub,输入用户名和密码,登录成功。
$ vi Dockerfile
joe@ubuntu00:~/tmp/Image$ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/joe/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
查看本地最新生成的镜像ID “f987582accc8”。
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu16.04-sshd latest f987582accc8 10 minutes ago 198MB
给镜像添加版本信息,Docker Hub要求tag。
docker tag f987582accc8 canhui/ubuntu16.04-sshd:latest
上传到docker hub。
$ docker push canhui/ubuntu16.04-sshd
最后,拉取该镜像到本地。
$ docker pull canhui/ubuntu16.04-sshd:latest
[1. Dockerfile之COPY] https://medium.freecodecamp.org/dockerfile-copy-vs-add-key-differences-and-best-practices-9570c4592e9e
[备注一] 本文感谢 Aleksandar Diklic 和他精彩的作品 Ubuntu sshd。(Thanks Aleksandar Diklic and credits to you.)
[版本一] 初稿,2019年05月16号。
[版本二] 完美,2019年05月31号。