Tao He

Tao He

  • Docs
  • API
  • Help
  • Blog

›Recent Posts

Recent Posts

  • Microsoft PowerPoint
  • Chrome
  • Alfred
  • Docusaurus
  • Git Commands

Docker

April 26, 2014

{% include JB/setup %}

Installation

Ubuntu

Get Docker CE for Ubuntu

sudo apt-get remove docker docker-engine docker.io
sudo apt-get update
sudo apt-get install \
    linux-image-extra-$(uname -r) \
    linux-image-extra-virtual
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install docker-ce
sudo docker run hello-world

Manage Docker as a non-root user

sudo groupadd docker
sudo usermod -aG docker $USER

Start Docker

docker version
docker images
docker ps -a

macOS

Start Docker

export DOCKER_HOST=tcp://localhost:4243
docker
docker version
docker images
docker ps -a

Work

docker login
docker search centos
docker search ubuntu
docker pull centos
docker pull ubuntu

AWS

sudo yum install docker
sudo service docker restart

Build Image

vim Dockerfile
docker build --rm -t app_server .
docker images

Run Container from Image

CentOS

docker run -it --name=centos_1 centos bash  # run and exec shell
docker exec -it centos_1 bash  # exec another shell

Ubuntu

docker run -it --name=ubuntu_1 ubuntu bash  # run and exec shell
docker exec -it ubuntu_1 bash  # exec another shell

Run as daemon

docker run -itd --name=ubuntu_2 ubuntu bash
docker attach ubuntu_2
# press up arrow to show shell
# after operation, press Ctrl+P Ctrl+Q to detach

Remove Containers and Images

docker will NOT remove dead containers. Remove them manually is needed.

Stop container

docker stop CONTAINER_NAME_OR_ID

Remove container

docker rm CONTAINER_NAME_OR_ID

Remove all stopped containers

docker rm $(docker ps -a -q)

Remove image

docker rmi IMAGE_NAME_OR_ID

Remove all untagged images

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

Remove Untagged Images From Docker

SSH

In host

vim Dockerfile
docker build --rm -t eg_sshd .
docker run -P --name=test_sshd eg_sshd

In container

/usr/sbin/sshd
adduser taohe

In host

docker port test_sshd 22
# mark down the port of localhost
ssh taohe@localhost -p 49154

In container

apt-get install netcat
kill 13  # kill sshd to release port 22
nc -p 22 -l -vv  # for linux (netcat-traditional)
nc 22 -l -v  # for macos (netcat-openbsd)
netstat -ntlp | grep nc

In host

nc localhost 49154 -vv  # for linux (netcat-traditional). no `-p`!
nc localhost 49154 -v  # for macos (netcat-openbsd)
curl localhost:49154

Dockerize an SSH service

macOS上的Docker Container与主机通讯

  1. 确保Dockerfile里面暴露了端口,如EXPOSE 1883
  2. docker build --rm -t test_docker .
  3. docker run -P -it --name=test_docker_1 test_docker bash
  4. docker network inspect bridge确认包含test_docker_1,记录IP地址
  5. docker port test_docker_1 1883或者docker ps -a记录端口
  6. 本地用地址localhost和上面记录的端口连接

Comparing --expose/EXPOSE and -p

Network

docker network ls
docker network inspect bridge

Shell Access

docker exec -it some-mysql bash

Commit

Commit container changes to image

docker commit CONTAINER_NAME_OR_ID IMAGE_NAME

File

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Docker Hub

Firstly, create a repository on Docker Hub.

In order to push a repository to the Docker Hub, you need to name your local image using your Docker Hub username and the repository name.

docker tag phx:latest phxtest/phxtest:latest
docker push phxtest/phxtest
docker pull phxtest/phxtest  # test

Clean useless data

Large data remain in /var/lib/docker/aufs/ and /var/lib/docker/volumes/.

docker volume ls -qf dangling=true | xargs -r docker volume rm

https://lebkowski.name/docker-volumes/

MySQL

Start server

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=000000 -d mysql:5.7

Shell access

docker exec -it some-mysql bash
mysql -h localhost -P 3306 -u root -p

If failed, view logs

docker logs some-mysql

Start client

# $MYSQL_PORT_3306_TCP_ADDR is the MySQL server IP that get from
# `docker network inspect bridge` or
# `docker inspect some-mysql | grep IP`
docker run -it --rm mysql:5.7 sh -c 'exec mysql -h "$MYSQL_PORT_3306_TCP_ADDR" -P 3306 --protocol=tcp -u root -p'

MySQL

sameersbn (deprecated)

https://github.com/sameersbn/docker-mysql

docker pull sameersbn/mysql
docker stop mysql && docker rm mysql
docker run --name mysql -d -p 3306:3306 -v ~/mnt:/mnt -v ~/etc/mysql:/etc/mysql -v ~/var/lib/mysql:/var/lib/mysql sameersbn/mysql
docker ps -a
docker run -it --rm --volumes-from=mysql sameersbn/mysql mysql -uroot
docker inspect mysql | grep IP

Java

https://github.com/dockerfile/java

docker pull dockerfile/java
docker nohup run -it --rm -w /workspace/bin -v ~/workspace/bin:/workspace/bin dockerfile/java:oracle-java8 java -jar /workspace/bin/weibominer.jar uid 1000000000L 4 &

dive

dive

A tool for exploring each layer in a docker image

brew tap wagoodman/dive
brew install dive
Recent Posts
  • Installation
    • Ubuntu
    • macOS
    • AWS
  • Build Image
  • Run Container from Image
  • Remove Containers and Images
  • SSH
  • macOS上的Docker Container与主机通讯
  • Network
  • Shell Access
  • Commit
  • File
  • Docker Hub
  • Clean useless data
  • MySQL
  • sameersbn (deprecated)
  • Java
  • dive
Tao He
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Facebook Open Source
Copyright © 2019 Your Name or Your Company Name