提交 a485b66a 编写于 作者: J Jeff Fox

Image Building Best Practices

上级 689964a0
node_modules
\ No newline at end of file
FROM node:12-alpine
RUN apk add --no-cache python g++ make
WORKDIR /docker-app
COPY . .
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . .
CMD ["node", "src/index.js"]
\ No newline at end of file
......@@ -366,3 +366,62 @@ Now that we have our docker-compose.yml file, we can start it up!
When you're ready to tear it all down, simply run ```docker-compose down``` or hit the trash can on the Docker Dashboard for the entire app. The containers will stop and the network will be removed.
Once torn down, you can switch to another project, run ```docker-compose up``` and be ready to contribute to that project! It really doesn't get much simpler than that!
## Image Building Best Practices
### Image Layering
Did you know that you can look at what makes up an image? Using the docker image history command, you can see the command that was used to create each layer within an image.
1. Use the docker image history command to see the layers in the getting-started image you created earlier in the tutorial.
```sh
docker image history getting-started
```
2. You'll notice that several of the lines are truncated. If you add the --no-trunc flag, you'll get the full output.
```sh
docker image history --no-trunc getting-started
```
### Layer Caching
Let's look at the Dockerfile we were using one more time...
```docker
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
```
Going back to the image history output, we see that each command in the Dockerfile becomes a new layer in the image. You might remember that when we made a change to the image, the yarn dependencies had to be reinstalled. Is there a way to fix this? It doesn't make much sense to ship around the same dependencies every time we build, right?
To fix this, we need to restructure our Dockerfile to help support the caching of the dependencies. For Node-based applications, those dependencies are defined in the package.json file. So, what if we copied only that file in first, install the dependencies, and then copy in everything else? Then, we only recreate the yarn dependencies if there was a change to the package.json. Make sense?
1. Update the Dockerfile to copy in the package.json first, install dependencies, and then copy everything else in.
```docker
FROM node:12-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . .
CMD ["node", "src/index.js"]
```
2. Create a file named .dockerignore in the same folder as the Dockerfile with the following contents.
```ignore
node_modules
```
.dockerignore files are an easy way to selectively copy only image relevant files. You can read more about this here. In this case, the node_modules folder should be omitted in the second COPY step because otherwise, it would possibly overwrite files which were created by the command in the RUN step. For further details on why this is recommended for Node.js applications and other best practices, have a look at their guide on Dockerizing a Node.js web app.
3. Build a new image using docker build.
```bash
docker build -t getting-started .
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册