我们可以通过命令行工具去创建和管理容器,事实上首选的创建和定制容器的方式是通过Dockerfile文件。Dockerfile一种能够被Docker程序解释的脚本,由一条条的指令组成,每条指令可以翻译为Linux下面的一条命令。Dockerfile有自己的格式和支持的命令。Docker程序将读取Dockerfile,解决这些命令间的依赖关系,并根据指令一步步地生成定制的image。当我们需要定制自己的需求时,只需在Dockerfile上添加或者修改指令,重新生成image就好
Dockerfile手册
https://docs.docker.com/engine/reference/builder/
https://yeasy.gitbooks.io/docker_practice/content/dockerfile/instructions.html
https://docs.docker.com/engine/getstarted/step_four/
docker build帮助信息
[ohsdba@ovm ~]$ docker help build
Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile Options: --build-arg value Set build-time variables (default []) --cgroup-parent string Optional parent cgroup for the container --cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota -c, --cpu-shares int CPU shares (relative weight) --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) --disable-content-trust Skip image verification (default true) -f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile') --force-rm Always remove intermediate containers --help Print usage --isolation string Container isolation technology --label value Set metadata for an image (default []) -m, --memory string Memory limit --memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap --no-cache Do not use cache when building the image --pull Always attempt to pull a newer version of the image -q, --quiet Suppress the build output and print image ID on success --rm Remove intermediate containers after a successful build (default true) --shm-size string Size of /dev/shm, default value is 64MB -t, --tag value Name and optionally a tag in the 'name:tag' format (default []) --ulimit value Ulimit options (default []) [ohsdba@ovm ~]$
查看image信息
[root@ovm ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql/mysql-server latest 43d4a6c26521 19 hours ago 369.1 MB oraclelinux latest 3d62999fc909 6 days ago 219.5 MB oraclelinux 6 175adfa05e40 10 weeks ago 223.1 MB
创建Dockerfile
[root@ovm ~]# su - ohsdba [ohsdba@ovm ~]$ mkdir docker-web [ohsdba@ovm ~]$ cd docker-web/ [ohsdba@ovm docker-web]$ pwd /home/ohsdba/docker-web [ohsdba@ovm docker-web]$ ls Dockerfile [ohsdba@ovm docker-web]$ cat Dockerfile # Dockerfile for creating a Docker image for OEL6 and httpd and perl FROM oraclelinux:6 MAINTAINER ohsdba <ohsdba@qq.com> RUN yum install -y httpd perl && yum clean all RUN echo "This is for ohsdba testing" > /var/www/html/index.html EXPOSE 80 CMD /usr/sbin/httpd -D FOREGROUND
创建image
[ohsdba@ovm docker-web]$ docker build -t ohsdba/httpd:v2 . Sending build context to Docker daemon 2.048 kB Step 1 : FROM oraclelinux:6 ---> 175adfa05e40 Step 2 : MAINTAINER ohsdba <ohsdba@qq.com> ---> Running in 694a249dc936 ---> 930bd4f22cac Removing intermediate container 694a249dc936 Step 3 : RUN yum install -y httpd perl && yum clean all ---> Running in 589a52e6bc45 Loaded plugins: ovl, security, ulninfo Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.2.15-54.0.1.el6_8 will be installed --> Processing Dependency: httpd-tools = 2.2.15-54.0.1.el6_8 for package: httpd-2.2.15-54.0.1.el6_8.x86_64 --> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-54.0.1.el6_8.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-54.0.1.el6_8.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-54.0.1.el6_8.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-54.0.1.el6_8.x86_64 ---> Package perl.x86_64 4:5.10.1-141.el6_7.1 will be installed --> Processing Dependency: perl-libs = 4:5.10.1-141.el6_7.1 for package: 4:perl-5.10.1-141.el6_7.1.x86_64 --> Processing Dependency: perl-libs for package: 4:perl-5.10.1-141.el6_7.1.x86_64 --> Processing Dependency: perl(Module::Pluggable) for package: 4:perl-5.10.1-141.el6_7.1.x86_64 --> Processing Dependency: perl(version) for package: 4:perl-5.10.1-141.el6_7.1.x86_64 --> Processing Dependency: perl(Pod::Simple) for package: 4:perl-5.10.1-141.el6_7.1.x86_64 --> Processing Dependency: libperl.so()(64bit) for package: 4:perl-5.10.1-141.el6_7.1.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed ---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed ---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed ---> Package httpd-tools.x86_64 0:2.2.15-54.0.1.el6_8 will be installed ---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed ---> Package perl-Module-Pluggable.x86_64 1:3.90-141.el6_7.1 will be installed ---> Package perl-Pod-Simple.x86_64 1:3.13-141.el6_7.1 will be installed --> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.13-141.el6_7.1.x86_64 ---> Package perl-libs.x86_64 4:5.10.1-141.el6_7.1 will be installed ---> Package perl-version.x86_64 3:0.77-141.el6_7.1 will be installed --> Running transaction check ---> Package perl-Pod-Escapes.x86_64 1:1.04-141.el6_7.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: httpd x86_64 2.2.15-54.0.1.el6_8 public_ol6_latest 832 k perl x86_64 4:5.10.1-141.el6_7.1 public_ol6_latest 10 M Installing for dependencies: apr x86_64 1.3.9-5.el6_2 public_ol6_latest 122 k apr-util x86_64 1.3.9-3.el6_0.1 public_ol6_latest 87 k apr-util-ldap x86_64 1.3.9-3.el6_0.1 public_ol6_latest 15 k httpd-tools x86_64 2.2.15-54.0.1.el6_8 public_ol6_latest 78 k mailcap noarch 2.1.31-2.el6 public_ol6_latest 26 k perl-Module-Pluggable x86_64 1:3.90-141.el6_7.1 public_ol6_latest 40 k perl-Pod-Escapes x86_64 1:1.04-141.el6_7.1 public_ol6_latest 32 k perl-Pod-Simple x86_64 1:3.13-141.el6_7.1 public_ol6_latest 212 k perl-libs x86_64 4:5.10.1-141.el6_7.1 public_ol6_latest 578 k perl-version x86_64 3:0.77-141.el6_7.1 public_ol6_latest 51 k Transaction Summary ================================================================================ Install 12 Package(s) Total download size: 12 M Installed size: 39 M Downloading Packages: -------------------------------------------------------------------------------- Total 470 kB/s | 12 MB 00:26 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : apr-1.3.9-5.el6_2.x86_64 1/12 Installing : apr-util-1.3.9-3.el6_0.1.x86_64 2/12 Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 3/12 Installing : httpd-tools-2.2.15-54.0.1.el6_8.x86_64 4/12 Installing : 1:perl-Pod-Escapes-1.04-141.el6_7.1.x86_64 5/12 Installing : 1:perl-Module-Pluggable-3.90-141.el6_7.1.x86_64 6/12 Installing : 3:perl-version-0.77-141.el6_7.1.x86_64 7/12 Installing : 4:perl-libs-5.10.1-141.el6_7.1.x86_64 8/12 Installing : 1:perl-Pod-Simple-3.13-141.el6_7.1.x86_64 9/12 Installing : 4:perl-5.10.1-141.el6_7.1.x86_64 10/12 Installing : mailcap-2.1.31-2.el6.noarch 11/12 Installing : httpd-2.2.15-54.0.1.el6_8.x86_64 12/12 Verifying : 1:perl-Pod-Simple-3.13-141.el6_7.1.x86_64 1/12 Verifying : 1:perl-Pod-Escapes-1.04-141.el6_7.1.x86_64 2/12 Verifying : 4:perl-5.10.1-141.el6_7.1.x86_64 3/12 Verifying : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 4/12 Verifying : 1:perl-Module-Pluggable-3.90-141.el6_7.1.x86_64 5/12 Verifying : apr-1.3.9-5.el6_2.x86_64 6/12 Verifying : httpd-2.2.15-54.0.1.el6_8.x86_64 7/12 Verifying : httpd-tools-2.2.15-54.0.1.el6_8.x86_64 8/12 Verifying : 3:perl-version-0.77-141.el6_7.1.x86_64 9/12 Verifying : mailcap-2.1.31-2.el6.noarch 10/12 Verifying : 4:perl-libs-5.10.1-141.el6_7.1.x86_64 11/12 Verifying : apr-util-1.3.9-3.el6_0.1.x86_64 12/12 Installed: httpd.x86_64 0:2.2.15-54.0.1.el6_8 perl.x86_64 4:5.10.1-141.el6_7.1 Dependency Installed: apr.x86_64 0:1.3.9-5.el6_2 apr-util.x86_64 0:1.3.9-3.el6_0.1 apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 httpd-tools.x86_64 0:2.2.15-54.0.1.el6_8 mailcap.noarch 0:2.1.31-2.el6 perl-Module-Pluggable.x86_64 1:3.90-141.el6_7.1 perl-Pod-Escapes.x86_64 1:1.04-141.el6_7.1 perl-Pod-Simple.x86_64 1:3.13-141.el6_7.1 perl-libs.x86_64 4:5.10.1-141.el6_7.1 perl-version.x86_64 3:0.77-141.el6_7.1 Complete! Loaded plugins: ovl, security, ulninfo Cleaning repos: public_ol6_UEKR4 public_ol6_latest Cleaning up Everything ---> 8fc1496f790d Removing intermediate container 589a52e6bc45 Step 4 : RUN echo "This is for ohsdba testing" > /var/www/html/index.html ---> Running in 0bf5dca5e0ec ---> 220a3aff9b13 Removing intermediate container 0bf5dca5e0ec Step 5 : EXPOSE 80 ---> Running in 176295787a9f ---> 1005d0996dd0 Removing intermediate container 176295787a9f Step 6 : CMD /usr/sbin/httpd -D FOREGROUND ---> Running in e1c9697ffed8 ---> ff2c7c83451e Removing intermediate container e1c9697ffed8 Successfully built ff2c7c83451e [ohsdba@ovm docker-web]$ [ohsdba@ovm docker-web]$ ls -l total 4 -rw-rw-r--. 1 ohsdba ohsdba 284 Oct 5 03:03 Dockerfile
查看image信息
[ohsdba@ovm docker-web]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ohsdba/httpd v2 ff2c7c83451e 17 seconds ago 273.4 MB mysql/mysql-server latest 43d4a6c26521 19 hours ago 369.1 MB oraclelinux latest 3d62999fc909 6 days ago 219.5 MB oraclelinux 6 175adfa05e40 10 weeks ago 223.1 MB [ohsdba@ovm docker-web]$
[ohsdba@ovm ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world c04b14da8d14: Pull complete Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/engine/userguide/ [ohsdba@ovm ~]$
Reference
https://docs.docker.com/engine/reference/builder/
https://yeasy.gitbooks.io/docker_practice/content/dockerfile/instructions.html
https://docs.docker.com/engine/getstarted/step_four/
https://community.oracle.com/docs/DOC-914969