Docker 101 minimalist web dev docker
Write ahead
After we knew some basic ideas and commands of Docker (No? see Docker 101 self-learning tutorial). We can build our own docker images and customized our dev kit.
Today I will show you how to use Dockerfile build your own images, here I build a minimalist web dev image as example:
Let’s code
Dockerfile
Dockerfile is a config doc that descripe how you want to assemble an image. And then use docker build [name-of-image] [path-to-dockerfile] build the image.
Here are the most common commands you can use in your Dockerfile.
All commands are not case-sensitive, but normally we use all UPPERCASE:
- FROM
Using FROM: or FROM @ so you can build your image based on the image you want to set as base image. Since most images build on top of some images, so a FROM must be the first non-comment instruction in the Dockerfile. - MAINTAINER
Using MAINTAINERso you can specify the Author field of the image you generated. - RUN
This is the most useful and common commands in Dockerfile, you can use it run any commands the image you use support, mostly are shell commands: RUNor RUN [“executable”, “param1”, “param2”]. - CMD
The main purpose of a CMD is to provide defaults for an executing container: CMD [“executable”,”param1”,”param2”], CMD [“param1”,”param2”], CMD command param1 param2. One Dockerfile can only have one CMD instruction, if you have multiple, the latest one will be used. - LABEL
Same as MAINTAINER, you can use it add some metadata to an image:LABEL= = = … - EXPOSE
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports.
Normally we use -p while creating a container instead of expose while building the image. - ENV
Treat this as variable delcaration like var in javascript. After you declare a variable, you can use it anytime after by referring the name of the variable: ENV, ENV = … - ADD
If you want to copy files to the image, you should use ADD: ADD… or ADD [“ “,… “ “].
You can also use url as src to download a file from the url, or if you use path , then the path to the src should be relative to the context of build, and if the src is a recognised compression format, it will be unpacked. - COPY
Pretty much the same as ADD, but limited version (can not use url and won’t unpack the compressions). Actually deprecated after 1.1.2. - ENTRYPOINT
An ENTRYPOINT allows you to configure a container that will run as an executable. Like you assign a default command you want it to run after you use this image create any container. ENTRYPOINT [“executable”, “param1”, “param2”] or ENTRYPOINT command param1 param2. And all parameters after docker runwill be added to the ENTRYPOINT. - VOLUME
If you want to mount your local files as a volume to the image, you should use this command. VOLUME [“/data”] - USER
Use to set the user name or UID to use when running the image and also for any RUN / CMD commands. - WORKDIR
The entry point for you image (pwd).
All you want to know about Dockerfile can be found from this link.
Minimalist Web Dev Docker
Now let’s build our own Dockerfile to assemble a minimalist web dev image. First, let’s give a list of requirements we need for this image:
- node@latest
- npm@latest
- zsh ( I love zsh )
- vim / git / curl
- Default settings for zsh and vim: oh-my-zsh, amix/vimrc.
Since we want a minimalist image, we can not use ubuntu or other powerful linux, we should use alpine, the smallest linux I ever seen. And thanks for @mhart, he built a base image for node env based on alpine. So we can just use his shoulder :)
1 | # always use the latest :) |
Now you have a minimalist image to start your web dev which roughly ~100M .
Tips
Since we did not install python, g++ and other tools, so if you want to use node-sass and some packages like it that need to be compiled before using, you may have to add these packages by yourself :)