Docker file details

Source: Internet
Author: User
Tags file url

------------------------------------------------------------- Dockerfile ------------------------------------------------------

Dockerfile is an image representation. You can use Dockerfile to describe how to build an image and automatically build a container.
The format of all Dockerfile commands is:
INSTRUCTION arguments
Although the commands are case-insensitive, we recommend that you use uppercase letters.
FROM Command

FROM <image>
Or
FROM <image >:< tag>
This basic image is used for subsequent commands. Therefore, it should be the first command of Dockerfile.
For example:
FROM ubuntu
If no tag is specified, the default tag Is latest. If no tag is specified, an error is returned.
RUN command

The RUN command will execute any command in the image specified by FROM above, and then submit (commit) results. The submitted image will be used later.
Two formats:
RUN <command> (the command is run in a shell-'/bin/sh-c ')
Or:
RUN ["executable", "param1", "param2"...] (exec form)
The RUN command is equivalent:
Docker run image command
Docker commit container_id
Note

Use # As comment
For example:
# Memcached
#
# VERSION 1.0

# Use the ubuntu base image provided by dotCloud
FROM ubuntu

# Make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe">/etc/apt/sources. list
RUN apt-get update

# Install memcached
RUN apt-get install-y memcached
MAINTAINER command

MAINTAINER <name>
The MAINTAINER command is used to specify the name and contact information of the MAINTAINER.
For example:
MAINTAINER Guillaume J. Charmes, [email protected]
ENTRYPOINT command

There are two syntax formats: one is the above (shell mode ):
ENTRYPOINT cmd param1 param2...
The second is the exec format:
ENTRYPOINT ["cmd", "param1", "param2"...]
For example:
ENTRYPOINT ["echo", "Whale you be my container"]
ENTRYPOINT command settings run at container startup
[Email protected]: ~ # Cat Dockerfile
FROM ubuntu
ENTRYPOINT echo "Welcome! "

[Email protected]: ~ # Docker run 62fda5e1_d5
Welcome!
USER command

For example, you can use the above ENTRYPOINT to specify a memcached running User:
ENTRYPOINT ["memcached", "-u", "daemon"]
The better way is:
ENTRYPOINT ["memcached"]
USER daemon
EXPOSE command

The EXPOSE command can be used to set a port to be exposed in a running image.
EXPOSE <port> [<port>...]
For example, memcached uses port 11211 to expose the port so that the port can be seen outside the container and communicate with it.
EXPOSE 11211
A complete example:
# Memcached
#
# VERSION 2.2

# Use the ubuntu base image provided by dotCloud
FROM ubuntu

MAINTAINER Victor Coisne [email protected]

# Make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe">/etc/apt/sources. list
RUN apt-get update

# Install memcached
RUN apt-get install-y memcached

# Launch memcached when launching the container
ENTRYPOINT ["memcached"]

# Run memcached as the daemon user
USER daemon

# Expose memcached port
EXPOSE 11211
The above is an official example. We recommend that you replace it with 163 or sohu source in China. Otherwise, it will be too slow.
[Email protected]: ~ # Docker build-t tankywoo-<dck [0, 38/480]
Uploading context 2.56 kB
Uploading context
Step 0: FROM ubuntu
---> 99ec81b80c55
Step 1: MAINTAINER Victor Coisne [email protected]
---> Using cache
---> 2b58110877f6
Step 2: RUN echo "deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse">/etc/apt/sources. list
---> Running in f55a4a8bb069
---> D48c6a965398
Step 3: RUN apt-get update
---> Running in da091a1dd6e7
Ign http://mirrors.163.com precise InRelease
Get: 1 http://mirrors.163.com precise Release. gpg [198 B]

....

Processing triggers for libc-bin (2.19-0ubuntu6 )...
Processing triggers for ureadahead (0.100.0-16 )...
---> 2886671b5b86
Step 5: ENTRYPOINT ["memcached"]
---> Running in e8aeeab92cb6
---> 7148293a4053
Step 6: USER daemon
---> Running in 288766b19606
---> 235e7f630ffa
Step 7: EXPOSE 11211
---> Running in c6f881b9d51f
---> 666c5d65f396
Successfully built 666c5d65f396
Removing intermediate container f55a4a8bb069
Removing intermediate container da091a1dd6e7
Removing intermediate container f23631d3d45a
Removing intermediate container e8aeeab92cb6
Removing intermediate container 288766b19606
Removing intermediate container c6f881b9d51f
ENV command

Used to set environment variables
ENV <key> <value>
After the settings, subsequent RUN commands can be used
Use the image generated by the dockerfile to create a container. You can view the environment variable through docker inspect:
[Email protected]: ~ # Docker inspect 49bfc7a9817f
...
"Env ":[
"Name = tanky ",
"HOME = /",
"PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
...
Name = tanky is set.
You can also set or modify the environment variables during docker run:
Docker run-I-t -- env name = "tanky" ubuntu: newtest/bin/bash
ADD command

Copy the file from src to the dest path of the container:
ADD <src> <dest>
<Src> is the relative path of the constructed source directory. It can be a file or directory path, or a remote file url.
<Dest> is the absolute path in the INER.
VOLUME command

VOLUME ["<mountpoint>"]
For example:
VOLUME ["/data"]
Create a mount point to share a directory
For details, refer to Docker 4-summary
WORKDIR command

WORKDIR/path/to/workdir
Configure the RUN, CMD, and ENTRYPOINT commands to set the current working path
You can set it multiple times. If it is a relative path, it is relative to the previous WORKDIR command.
For example:
WORKDIR/a WORKDIR B WORKDIR c RUN pwd
In fact, pwd is executed under/a/B/c.
CMD command

There are three formats:
CMD ["executable", "param1", "param2"] (like an exec, preferred form)
CMD ["param1", "param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (as a shell)
One Dockerfile can have only one CMD. If there are multiple, only the last one takes effect.
The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT as well.
TODO hasn't figured out its role


This article from "Life is an attitude" blog, please be sure to keep this source http://sangh.blog.51cto.com/6892345/1573967

Docker file details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.