Brief Note of Docker

Installation

On linux, follow the installation guide

On OSX, install boot2docker(deprecated) or Docker Toolbox which is recommended on docker official document.

Hello World

Installation process is smooth, it’s time to test whether docker is successfully installed. We can run a hello-world demo using

docker run ubuntu:14.04 /bin/echo 'Hello world'

If Hello world shows on the screen, it means docker is correctly configured. The main process, docker has done just now, started from pulling the ubuntu:14.04 image from docker hub, which is a image registry hosted by Docker. Then run it in a container with command /bin/echo 'Hellow world'.

Dockerfile

Make your own image starts from writting a dockerfile. An example is as follows:

FROM debian:jessie
RUN echo "deb http://mirrors.163.com/debian jessie main non-free contrib" > /etc/apt/sources.list  && \
        apt-get update && \
        apt-get install -y python2.7 uwsgi python-pip python-dev libmysqlclient-dev uwsgi-plugin-python && \
        pip install web.py redis pytz pymongo MySQL-python PySQLPool python-dateutil&& \
        mkdir /backend/ && \
        mkdir /script/
COPY . /backend/
ENTRYPOINT /usr/bin/uwsgi --ini /backend/uwsgi.ini && tail -f /backend/uwsgi.log
EXPOSE 3015

The new image is based on debian:jessie from docker hub, then executes several commands we defined, such as updating local repository, installing some necessary packages, then creates project directory and copy project files to it. The entrypoint defines the program and argvs when this image runs.

Attention:

  • Image exits after the entrypoint command exits, so the entrypoint should be a foreground program or do an endless command to make the image holds on if the program is designed to be run in the background. In this example, we do a tail -f job in the end.
  • For security reason, docker cann’t copy from parent directory.

Basic Usages

Here lists several commands commonly used.

docker build

docker build -t backend --rm .

docker run

# -p defines the port map between host and docker
docker run -it -p 8888:3015 backend
# run on background
docker run -d -p 8888:3015 backend
docker run -it -p 8888:3015 backend --entrypoint=/bin/bash
# if we want to do `privileged` modify, use --privileged, such as edit net.ipv4.tcp_somaxconn
docker run -it --privileged -p 8888:3015 backend
# mount filesystem (such as: sync timezone of host and docker)
docker run -it -v /etc/localtime:/etc/localtime:ro backend

docker exec

# get a shell when container is running
docker exec -it containerId /bin/bash

docker ps

list running containers

# lists all the containers 
docker ps -a
# remove all stoped containers
docker rm $(docker ps -aq)

docker images

list images on localhost

# remove images
docker rmi imageId...
# batch remove untagged images
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')

docker push

docker push master:31001/user/backend

docker tag

docker tag -f backend master:31001/backend

docker commit

create a new image based on a container

docker commit containerId registry:tag
comments powered by Disqus