Sitemap
Stan Liu

Stan Liu

Docker

3 min readDec 15, 2017

--

1. install docker

if you’re using mac and you also love to use brew. Install docker like this

brew cask install docker

2. Use docker

Docker is similar in git.

It has repository, e.g. Ubuntu, Ruby, Python and so on. It has tags for versions in every repository. e.g. Ubuntu:16.04 (if you leave it blank, it will install the latest). It has images as we mention asUbuntu:16.04 or Ubuntu:latest.

And the actions is similar in git.

docker pull # pull a image
docker commit # commit changes
docker push # push a image

Don’t use docker commit to edit image, Using Dockerfile

Docker image is base on every sub layers. If you change configurations of image, it builds a new sub layer base on your old layers so it means if you edit your image and commit it. It will keep the old sub layers and create a edited new layer on its. Make the image too fat either unnecessary. Besides you, other teammates couldn’t know what did you edit the configurations of this image. You would forget someday.

3. Dockerfile

Always using Dockerfile to create your image.

# choose a image, 'From' must always in the file and on the top.
FROM ubuntu
# exec a command, (same command should use only one line)
RUN apt-get update -q \
&& apt-get install \
&& nodejs \

Same command should use only one RUN

Because every RUN command builds a sub layer. If you doing like this

RUN apt-get update -q
RUN apt-get install
RUN nodejs
...

it builds 3 or more sub layers on those similar commands. these sub layers make your image fat and builds too many unnecessary sub layers, need more integration time and easy to make some mistakes.

ENV for environment configuration

There are two kinds of format.

  • ENV (Key) (Value)
  • ENV (Key1)=(Value1) (Key2)=(Value2)...

This is official node image dockerfile

ENV NODE_VERSION 7.2.0

RUN curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" \
&& curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
&& gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
&& grep " node-v$NODE_VERSION-linux-x64.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
&& tar -xJf "node-v$NODE_VERSION-linux-x64.tar.xz" -C /usr/local --strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs

You could use env variable $NODE_VERSION within RUN command line

Volume for storage

There are two kinds of format.

  • VOLUME ["path1", "path2"]
  • VOLUME (path)

Database should save in volume. Because volume is the only one could keep datas, edits in container.

VOLUME /data

/data will load when run.

Of course you can override the volume.

$ docker run -d -v mydata:/data xxxx

More about Dockerfile format

4. Docker Compose

Defining and running multi-container Docker applications

Docker-Compose yaml format

Through docker-compose.ymlto create a project contains multiple containers.

  • Service : a application container could include many images
  • project : a full-business module composed by sets of association application containers
version: '3' # docker-compose version, now the latest is '3'
services:
# declare 2 service with name, build, ports
web:
build: . # build the dockerfile in . path
ports:
- 5001:5000 # map local port 5001 to container 5000
redis:
image: "redis:alpine"
  • build
  • command
  • container_name
  • image
  • ports

# (HOST: CONTAINER) or (HOST:CONTAINER:ro)

Get Stan Liu’s stories in your inbox

Join Medium for free to get updates from this writer.

volumes:

  • secrets
  • volumes
# (HOST: CONTAINER) or (HOST:CONTAINER:ro)volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
  • deploy
  • depends_on

will run depends_on first then the service who was depended

  • env_file
env_file: .envenv_file:
- ./common.env
- ./app/web.env
- ./opt/secrets.env

env file should conform env format. using # for comment

# common.env: Set development environment 
PROG_ENV=development
  • environment
  • expose

Only expose port for linked service in container.

nginx:
ports:
- "8080:80"
links:
- web
web:
expose:
- "90"

# nginx could proxy_pass http://web:90

read env varaible

db:
image: "mongo:$(MONGO_VERSION)"

run docker-compose

$ docker-compose up

docker-compose command instructions

Here only list commands useful for me

  • build
docker-compose build [option] [service] ...

build a container like docker build

  • config

to valid compose.yml format is correct, it correct shows configurations otherwise shows error description

  • exec

get into containers so we could exec some command

  • ps

like docker ps to list current containers

  • run

run a command

  • up

activate a container

  • down

stop container which usingup activates and shut down network

--

--