reffer to:
Install Docker and run hello-world - Docker
Centos 7: Get Docker for CentOS - Docker

non-root user: Post-installation steps for Linux - Docker

$ curl -fsSL | sh
$ sudo usermod -aG docker user
$ sudo service docker restart

logout once and login again not to need to use sudo

$ curl -fsSL | sudo apt-key add -

$ docker -v

Docker version 1.11.0, build 4dc5990

check docker
$ docker run hello-world

go into inside on bash
$ docker run -it ubuntu bash


show downloaded docker images
$ docker images

run ubuntu container and run a command.
if ubuntu isn’t downloaded, it starts downloading it.
docker container is booted once and stops.
$ docker run –name sample ubuntu cat /etc/lsb-release

show all containers (includes stopped one)
$ docker ps -a

show container details
$ docker inspect (id)

search docker images in dockerhub $ docker search nginx

remove stopped container
$ docker rm (id)

delete all docker containers
$ docker rm $(docker ps -aq)

remove an docker image
$ docker rmi (image)

delete all docker images
$ docker rmi $(docker images -q)

stop docker
$ docker stop (id)

start a stopped container
$ docker start (id)

run one command in a working container
$ docker exec (id) md5sum /etc/nginx/nginx.conf

implement bash
$ docker exec -i -t (id) /bin/bash

show docker logs
-t show time
$ docker logs -t (id)

show image detail $ docker inspect (image)

attache deamon container
$ docker attach (id)

show docker status
$ docker stats (id) (id) ..

show container process
$ docker top (container id)

show port transffer of container
$ docker port (container id)

change container name
$ docker rename (old) (new)

copy file between host and docker container
$ docker cp file (container id):file
$ docker cp (container id):file file

show differences between image and container
$ docker diff (container id)

show docker information
$ docker info

create images from container
$ docker commit (container id) (image name:tag name)

export container
export and import is pair
this tar file includes directory hierarchy
$ docker export container > (file.tar)

import image from exported container tar file
$ docker import (file) (image name:tag name)

save docker image to tar
save and load is pair
save as layer
$ docker save -o (file) (image name)

load saved docker image file
$ docker load -i (file)

verify network
inspect command can show the network of the container
$ docker network ls

let the container join the specified network
$ docker run -d –net=my-bridge-network –name db training/postgres

change the network
$ docker network connect my-bridge-network web

show container volumes
$ docker volume ls

show container dangling volumes
this is remained when to delete containers with volume container
$ docker volume ls -qf dangling=true

clean up dangling volumes
$ docker volume rm $(docker volume ls -qf dangling=true)

back up and restore data
$ docker run –rm –volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
$ docker run –rm –volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c “cd /dbdata && tar xvf /backup/backup.tar –strip 1”

docker run parameters

-d -> deamon
-P -> connect port between the host and the container
–name -> container name
-e -> set env
-v -> bind directory
$ docker run -d -P –name=server -e myenv=foo -v ~/:/usr/share/local/nginx/html nginx

bind ports
-p 60000-60030:60000-60030 -p 8080:80

–link (id):(alias)
you can set alias by yourself
$ docker run –link (id):(alias) ubuntu

$ docker run –link server:ngx ubuntu env | grep -i _port


–rm automatically remove the container if it exists
$ docker run –rm –link server:ngx ubuntu cat /etc/hosts

prepare Dockerfile and run

$ vim Dockerfile

FROM centos:7
MAINTAINER sample taro <[email protected]>
RUN yum -y install epel-release
RUN yum -y install httpd
EXPOSE 80 443
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]

$ docker build -t (image name:tag name) .


this is used to create docker image

shell type
implement on /bin/sh
RUN echo こんにちは

exec type
this doesn’t use /bin/sh
RUN [“echo, “こんにちは”]

exec type
RUN [“/bin/bash”, “-c”, “echo ‘こんにちは’”]

implement command
this can be overwritted docker run parameters

start deamon using shell type
CMD /usr/sbin/httpd -D FOREGROUND

start deamon using exec type
CMD [“/usr/sbin/httpd”,“-D”,“FOREGROUND”]

implement command
this can’t be overwritted docker run parameters
e.g. $ docker run -d -p 81:80 sample /usr/sbin/nginx -g “daemon off;”

start deamon using ENTRYPOINT shell type

start deamon using ENTRYPOINT exec type
ENTRYPOINT [“/usr/sbin/httpd”,“-D”,“FOREGROUND”]

place web files when building
this create new images
ONBUILD website.tar /var/www/html/

set environment parameter
ENV myName “taro”

set working directory
equals to cd sample
WORKDIR sample

assign implement user
USER sample

set label
SET samplelabel

expose port

add file or directory
this can add tar file, then automatically untar
ADD index.html /var/www/html/
ADD sample.tar /var/www/html/
ADD /var/www/html/

copy file or directory
COPY index.html /var/www/html/

docker-build sends all files in same directory,
so prepare .dockerignore file

mount volume
MOUNT /var/log/httpd

RUN locale-gen ja_JP.UTF-8

prepare private repository and push

use registory image
tag 2.0 is created by go-lang
docker pull registory:2.0

run repository
if this container is stopped, registered image is gone
this needs to mount or something
docker run -d -p 5000:5000 registry:2.0

name ruled image name
docker tag (local image) localhost:5000/(image name)

push image
docker push localhost:5000/(image name)

pull image
docker pull localhost:5000/(image name)

push to hub.docker

create account on
$ docker tag docker-whale shojik/docker-whale:latest
$ docker login –username=shojik –[email protected]

save token to ~/.docker/config.json

$ docker push shojik/docker-whale

implement composer install

use composer - Docker Hub

docker pull composer
move the directory include composer.json
$ docker run –rm -v $(pwd):/app composer install

backup mysql data container

$ docker run –rm –volumes-from dataonly -v $(pwd):/backup dataonly tar cvf /backup/backup.tar /var/lib/mysql

$ docker run –rm –volumes-from dataonly -v $(pwd):/backup dataonly sh -c “cd /var/lib/ && tar -xvf /backup/backup.tar”

security check

see docker/docker-bench-security: The Docker Bench for Security is a script that checks for dozens of common best-practices around deploying Docker containers in production.


$ docker run -it --net host --pid host --cap-add audit_control \
    -v /var/lib:/var/lib \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v /usr/lib/systemd:/usr/lib/systemd \
    -v /etc:/etc --label docker_bench_security \


show application logs to use docker logs

ln -sf /dev/stdout /var/log/nginx/access.log
ln -sf /dev/stderr /var/log/nginx/error.log