docker_practice/appendix/best_practices.md

345 lines
16 KiB
Go
Raw Normal View History

# Dockerfile
2019-01-06 09:40:31 +08:00
Docker [Best practices for writing Dockerfiles](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) 的理解与翻译。
##
###
`Dockerfile`
### 使 `.dockerignore`
使 `Dockerfile` `Dockerfile` `.dockerignore` `.dockerignore` Git `.gitignore`
### 使
`Docker 17.05` 使 [](../image/multistage-builds.md)
###
###
web web
使 [Docker ](../network/linking.md)
###
`Dockerfile`
###
便 `PRs` `\`
`buildpack-deps`
```docker
RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion
```
###
Docker `Dockerfile` Docker 使使 `docker build` 使 `--no-cache=true`
使
* `FROM` 使
* `Dockerfile`
* `ADD` `COPY` 访
* `ADD` `COPY` `RUN apt-get -y update` Docker
`Dockerfile` 使
## Dockerfile
`Dockerfile`
### FROM
2017-12-31 14:20:04 +08:00
使使 [Alpine](https://hub.docker.com/_/alpine/) 镜像,因为它被严格控制并保持最小尺寸(目前小于 5 MB但它仍然是一个完整的发行版。
### LABEL
`LABEL` `#`
>使使
```docker
# Set one or more individual labels
LABEL com.example.version="0.0.1-beta"
LABEL vendor="ACME Incorporated"
LABEL com.example.release-date="2015-02-12"
LABEL com.example.version.is-production=""
```
`LABEL`
```docker
# Set multiple labels at once, using line-continuation characters to break long lines
LABEL vendor=ACME\ Incorporated \
com.example.is-beta= \
com.example.is-production="" \
com.example.version="0.0.1-beta" \
com.example.release-date="2015-02-12"
```
2019-01-06 09:40:31 +08:00
[Understanding object labels](https://docs.docker.com/config/labels-custom-metadata/)。关于查询标签信息,参考 [Managing labels on objects](https://docs.docker.com/config/labels-custom-metadata/)。
### RUN
`Dockerfile` `RUN` `\`
#### apt-get
`RUN` `apt-get` `RUN apt-get`
使 `RUN apt-get upgrade` `dist-upgrade` `foo`使 `apt-get install -y foo` `foo`
`RUN apt-get update` `apt-get install` `RUN`
```docker
RUN apt-get update && apt-get install -y \
package-bar \
package-baz \
package-foo
```
`apt-get update` `RUN` `apt-get install` `Dockerfile`
```docker
2018-12-19 17:24:52 +08:00
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install -y curl
```
Docker `apt-get install`
```docker
2018-12-19 17:24:52 +08:00
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install -y curl nginx
```
Docker `RUN apt-get update` `apt-get update` 使 `apt-get update` `apt-get install` `curl` `nginx`
使 `RUN apt-get update && apt-get install -y` Dockerfiles `cache busting` `cache-busting`
```docker
RUN apt-get update && apt-get install -y \
package-bar \
package-baz \
package-foo=1.3.*
```
使
`RUN` `apt-get`
```docker
RUN apt-get update && apt-get install -y \
aufs-tools \
automake \
build-essential \
curl \
dpkg-sig \
libcap-dev \
libsqlite3-dev \
mercurial \
reprepro \
ruby1.9.1 \
ruby1.9.1-dev \
s3cmd=1.1.* \
&& rm -rf /var/lib/apt/lists/*
```
2023-01-07 23:45:41 +08:00
`s3cmd` `1.1.*`使 `apt-get update`
2023-01-07 23:45:41 +08:00
apt `var/lib/apt/lists` `RUN` `apt-get update` `apt-get install`
> Debian Ubuntu apt-get clean apt-get clean
### CMD
`CMD` `CMD` `CMD ["executable", "param1", "param2"...]` 使( `Apache`) `CMD ["apache2", "-DFOREGROUND"]` 使
`CMD` `shell` (bash, Python, perl ) `CMD ["perl", "-de0"]` `CMD ["PHP", "-a"]`使 `docker run -it python` `shell` `CMD` `CMD ["param", "param"]` `ENTRYPOINT` 使使 `ENTRYPOINT`
### EXPOSE
`EXPOSE` 使 `Apache` web 使 `EXPOSE 80` `MongoDB` 使 `EXPOSE 27017`
访 `docker run` 使
### ENV
便使 `ENV` `PATH` 使 `ENV PATH /usr/local/nginx/bin:$PATH` `CMD ["nginx"]`
`ENV` Postgres `PGDATA`
`ENV`
```docker
ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
```
`ENV`
### ADD COPY
`ADD` `COPY` 使 `COPY` `ADD` `COPY` `ADD` tar URL `ADD` tar `ADD rootfs.tar.xz`
`Dockerfile` 使 `COPY` `COPY`
```docker
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY . /tmp/
```
`COPY . /tmp/` `RUN` `.`
使 `ADD` URL 使 `curl` `wget`
```docker
ADD http://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all
```
使
```docker
RUN mkdir -p /usr/src/things \
&& curl -SL http://example.com/big.tar.xz \
| tar -xJC /usr/src/things \
&& make -C /usr/src/things all
```
使
`ADD` 使 `COPY`
### ENTRYPOINT
`ENTRYPOINT` `CMD`
`s3cmd`:
```docker
ENTRYPOINT ["s3cmd"]
CMD ["--help"]
```
```bash
$ docker run s3cmd
```
```bash
$ docker run s3cmd ls s3://mybucket
```
`ENTRYPOINT` 使使
`Postgres` 使 `ENTRYPOINT`
```bash
#!/bin/bash
set -e
if [ "$1" = 'postgres' ]; then
chown -R postgres "$PGDATA"
if [ -z "$(ls -A "$PGDATA")" ]; then
gosu postgres initdb
fi
exec gosu postgres "$@"
fi
exec "$@"
```
>使 Bash exec PID 1 Unix
`ENTRYPOINT`
```docker
COPY ./docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
```
`Postgres`
`Postgres`
```bash
$ docker run postgres
```
`Postgres`
```bash
$ docker run postgres postgres --help
```
`Bash`
```bash
$ docker run --rm -it postgres bash
```
### VOLUME
`VOLUME` 使 `VOLUME`
### USER
使 `USER` root `Dockerfile` 使 `RUN groupadd -r postgres && useradd -r -g postgres postgres`
2022-01-04 10:41:51 +08:00
> UID/GID UID/GID UID/GID UID/GID
使 `sudo` TTY `sudo` root root 使 [gosu](https://github.com/tianon/gosu)。
使 `USER`
### WORKDIR
`WORKDIR` 使使 `WORKDIR` `RUN cd ... && do-something`
2019-01-06 09:40:31 +08:00
##
2019-01-06 09:40:31 +08:00
Dockerfile https://github.com/docker-library/docs