root@ubuntu:~# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos2 latest 61b7efba9133 3 hours ago 192MB nginx2 0.1 7cb5220c81d5 5 hours ago 204MB centos latest a8493f5f50ff 40 hours ago 192MB nginx latest 5766334bdaa0 44 hours ago 183MB hello-world latest 48b5124b2768 2 months ago 1.84kB
# usage: file_env VAR [DEFAULT] # ie: file_env 'XYZ_DB_PASSWORD' 'example' # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of # "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) file_env() { local var="$1" local fileVar="${var}_FILE" local def="${2:-}" if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then echo >&2 "error: both $var and $fileVar are set (but are exclusive)" exit 1 fi local val="$def" if [ "${!var:-}" ]; then val="${!var}" elif [ "${!fileVar:-}" ]; then val="$(< "${!fileVar}")" fi export"$var"="$val" unset"$fileVar" }
if [ "${1:0:1}" = '-' ]; then set -- postgres "$@" fi
# allow the container to be started with `--user` if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then mkdir -p "$PGDATA" chown -R postgres "$PGDATA" chmod 700 "$PGDATA"
# Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user if [ "$POSTGRES_INITDB_XLOGDIR" ]; then mkdir -p "$POSTGRES_INITDB_XLOGDIR" chown -R postgres "$POSTGRES_INITDB_XLOGDIR" chmod 700 "$POSTGRES_INITDB_XLOGDIR" fi
# look specifically for PG_VERSION, as it is expected in the DB dir if [ ! -s "$PGDATA/PG_VERSION" ]; then file_env 'POSTGRES_INITDB_ARGS' if [ "$POSTGRES_INITDB_XLOGDIR" ]; then export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR" fi eval"initdb --username=postgres $POSTGRES_INITDB_ARGS"
# check password first so we can output the warning before postgres # messes it up file_env 'POSTGRES_PASSWORD' if [ "$POSTGRES_PASSWORD" ]; then pass="PASSWORD '$POSTGRES_PASSWORD'" authMethod=md5 else # The - option suppresses leading tabs but *not* spaces. :) cat >&2 <<-'EOWARN' **************************************************** WARNING: No password has been setfor the database. This will allow anyone with access to the Postgres port to access your database. In Docker's default configuration, this is effectively any other container on the same system. Use "-e POSTGRES_PASSWORD=password" to set it in "docker run". **************************************************** EOWARN pass= authMethod=trust fi { echo echo "host all all all $authMethod" } >> "$PGDATA/pg_hba.conf" # internal start of server in order to allow set-up using psql-client # does not listen on external TCP/IP and waits until start finishes PGUSER="${PGUSER:-postgres}" \ pg_ctl -D "$PGDATA" \ -o "-c listen_addresses='localhost'" \ -w start file_env 'POSTGRES_USER' 'postgres' file_env 'POSTGRES_DB' "$POSTGRES_USER" psql=( psql -v ON_ERROR_STOP=1 ) if [ "$POSTGRES_DB" != 'postgres' ]; then "${psql[@]}" --username postgres <<-EOSQL CREATE DATABASE "$POSTGRES_DB" ; EOSQL echo fi if [ "$POSTGRES_USER" = 'postgres' ]; then op='ALTER' else op='CREATE' fi "${psql[@]}" --username postgres <<-EOSQL $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ; EOSQL echo psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" ) echo for f in /docker-entrypoint-initdb.d/*; do case "$f" in *.sh) echo "$0: running $f"; . "$f" ;; *.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;; *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;; *) echo "$0: ignoring $f" ;; esac echo done PGUSER="${PGUSER:-postgres}" \ pg_ctl -D "$PGDATA" -m fast -w stop echo echo 'PostgreSQL init process complete; ready for start up.' echo fi fi exec "$@"
FROM windowsservercore COPY testfile.txt c:\ RUN dir c:\
# 结果 PS C:\John> docker build -t succeeds --no-cache=true . Sending build context to Docker daemon 3.072 kB Step 1 : FROM windowsservercore ---> dbfee88ee9fd Step 2 : COPY testfile.txt c:\ ---> 99ceb62e90df Removing intermediate container 62afbe726221 Step 3 : RUN dir c:\ ---> Running in a5ff53ad6323 Volume in drive C has no label. Volume Serial Number is 1440-27FA
Directory of c:\
03/25/2016 05:28 AM <DIR> inetpub 03/25/2016 04:22 AM <DIR> PerfLogs 04/22/2016 10:59 PM <DIR> Program Files 03/25/2016 04:22 AM <DIR> Program Files (x86) 04/18/2016 09:26 AM 4 testfile.txt 04/22/2016 10:59 PM <DIR> Users 04/22/2016 10:59 PM <DIR> Windows 1 File(s) 4 bytes 6 Dir(s) 21,252,689,920 bytes free ---> 2569aa19abef Removing intermediate container a5ff53ad6323 Successfully built 2569aa19abef PS C:\John>
docker run <image>的命令行参数将附跟在 exec 形式的ENTRYPOINT中的所有元素之后, 并将覆盖使用CMD指定的所有元素. 这允许将参数传递到入口点, 即docker run <image> -d将把-d参数传递给入口点. 您可以使用docker run --entrypoint标志覆盖ENTRYPOINT指令
#!/bin/sh # Note: I've written this using sh so it works in the busybox container too
# USE the trap if you need to also do manual cleanup after the service is stopped, # or need to start multiple services in the one container trap"echo TRAPed signal" HUP INT QUIT TERM
# start service in background here /usr/sbin/apachectl start
echo"[hit enter key to exit] or run 'docker stop <container>'" read
# stop service and clean up here echo"stopping apache" /usr/sbin/apachectl stop
$ docker run -it --rm --name test top Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached CPU: 5% usr 0% sys 0% nic 94% idle 0% io 0% irq 0% sirq Load average: 0.08 0.03 0.05 2/98 6 PID PPID USER STAT VSZ %VSZ %CPU COMMAND 1 0 root R 3164 0% 0% top -b
使用docker stop可以干净的退出:
1 2 3 4 5
$ /usr/bin/time docker stop test test real 0m 0.20s user 0m 0.02s sys 0m 0.04s
如果忘记将exec添加到您的ENTRYPOINT的开头:
1 2 3
FROM ubuntu ENTRYPOINT top -b CMD --ignored-param1
然后,你可以运行它(给它一个名称为下一步):
1 2 3 4 5 6 7
$ docker run -it --name test top --ignored-param2 Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached CPU: 9% usr 2% sys 0% nic 88% idle 0% io 0% irq 0% sirq Load average: 0.01 0.02 0.05 2/101 7 PID PPID USER STAT VSZ %VSZ %CPU COMMAND 1 0 root S 3168 0% 0% /bin/sh -c top -b cmd cmd2 7 1 root R 3164 0% 0% top -b
您可以从top的输出中看到指定的ENTRYPOINT不是PID 1。
如果你运行docker停止测试,容器将不会完全退出, 超时后停止命令将强制发送SIGKILL:
1 2 3 4 5 6 7 8 9 10
$ docker exec -it test ps aux PID USER COMMAND 1 root /bin/sh -c top -b cmd cmd2 7 root top -b 8 root ps aux $ /usr/bin/time docker stop test test real 0m 10.19s user 0m 0.04s sys 0m 0.03s