Important Docker commands
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is. Containerization is increasingly popular because containers are:
- Flexible: Even the most complex applications can be containerized.
- Lightweight: Containers leverage and share the host kernel.
- Interchangeable: You can deploy updates and upgrades on-the-fly.
- Portable: You can build locally, deploy to the cloud, and run anywhere.
- Scalable: You can increase and automatically distribute container replicas.
- Stackable: You can stack services vertically and on-the-fly.
Install
Install Docker
sudo curl -sSL https://get.docker.com/ | sh
#Docker user group setting (If not set, then docker build or docker-compose down commands would meet error like cannot connect to docker daemon)
#Create the docker group
sudo groupadd docker
#Add your user to the docker group
sudo usermod -aG docker $USER
#Logout and login to the server again
#BaseDeviceUUID configured in: /var/lib/docker/devicemapper/metadata/deviceset-metadata
Install Docker-machine
curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine
Install Docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
#Uninstall docker-compose
sudo rm /usr/local/bin/docker-compose
#Start docker-compose app (read the docker-compose.yml in current folder)
docker-compose up
#Start docker-compose app in background mode (read the docker-compose.yml in current folder)
docker-compose up -d
#Remove docker-compose containers
docker-compose rm -v
Install Fig
curl -L https://github.com/docker/fig/releases/download/1.18.0/fig-`uname -s`-`uname -m` > /usr/local/bin/fig; chmod +x /usr/local/bin/fig
sudo pip install -U fig
Install Shipyard
#Master node
curl -sSL https://shipyard-project.com/deploy | PORT=8888 bash -s
#Slave node
curl -sSL https://shipyard-project.com/deploy | ACTION=node DISCOVERY=etcd://http://<master>/:4001 bash -s
Install Rancher
sudo docker run -d --restart=unless-stopped -p 8888:8080 rancher/server
sudo docker run -d --restart=always -p 8888:8080 rancher/server
Docker containers
nsenter
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
#First, figure out the PID of the container you want to enter
PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
#Then enter the container
nsenter --target $PID --mount --uts --ipc --net --pid
VSFTPD
#Run vsftpd ftp container
sudo docker run -d -v /upload:/home/vsftpd \
-p 20:20 -p 21:21 -p 40000-40080:40000-40080 \
-e FTP_USER=user -e FTP_PASS=password \
-e PASV_ADDRESS=120.220.209.94 -e PASV_MIN_PORT=40000 -e PASV_MAX_PORT=40080 \
--name vsftpd --restart=always fauria/vsftpd
#Run vsftpd sftp container(only has write permission in /share folder, and need to login in port 2022)
sudo docker run --name sftp -v /upload/terminal:/home/terminal/share -p 2022:22 -d atmoz/sftp user:password:1001
#Modify the user folder permissions
sudo chmod 777 /upload/terminal/
MySQL
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -d mysql:5.7
MongoDB
#Run mongodb container:
sudo docker run -p 27017:27017 -v /data/db:/data/db -h mongodb --name mongodb -d mongo
#Connect to mongodb
sudo docker run -it --link mongodb:mongodb --rm mongo sh -c 'exec mongo "mongodb:27017/test"'
#Dump db
docker run -i --rm --link reliable_mongo_${RELIABLE_ENV_CONFIG}:mongo -v /tmp/mongodump:/tmp mongo bash -c 'mongodump -v --host $MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT --db '$1' --out=/tmp && chmod 777 /tmp/*'
SpringBoot
sudo docker run -it --link mongodb:mongodb -p 8001:8001 --name springboot-demo test/springboot-demo sh
Nginx
#Run nginx container:
docker container run \
-d \
-p 80:80 \
--name nginx \
nginx
#Run nginx container with folder mapping:
docker container run \
-d \
-p 80:80 \
--name nginx \
--volume /nginx:/usr/share/nginx/html \
nginx
#Run nginx container with folder mapping and conf
docker container run \
--name nginx \
--volume /nginx:/usr/share/nginx/html \
--volume /nginx/conf:/etc/nginx \
-p 80:80 \
-d \
nginx
#nginx.conf default content
server {
listen 80;
sendfile on;
default_type application/octet-stream;
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 256;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 9;
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
RStudio
#Dockerfile
FROM rocker/rstudio
RUN R -e 'install.packages("lubridateā)'
#Run docker container
docker build -t myrstudio .
docker run --name rstudio -d -p 8787:8787 myrstudio
#Navigate the RStudio container
http://localhost:8787
Jenkins
#Run jenkins
sudo docker run --name jenkins -p 8088:8080 -p 50000:50000 -v /var/jenkins_home -d jenkins
#Backup jenkins data
sudo docker cp jenkins:/var/jenkins_home jenkins_home
sudo tar -czf jenkins_home.tar jenkins_home/
#Restore jenkins data
sudo tar -xzf jenkins_home.tar
sudo chmod -R 777 jenkins_home
sudo docker cp jenkins_home jenkins:/var
sudo docker restart jenkins
Wordpress
#Install mysql
sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=431197 -d mysql
#Install wordpress
sudo docker run --name demo_wordpress --link mysql:mysql -p 8080:80 -d wordpress
#Browse wordpress homepage: http://[domain]:8080
Docker commands
Scenarios | Commands |
---|---|
Docker service | 1. sudo service docker start 2. sudo service docker restart 3. sudo systemctl enable docker |
Docker service automatic start | sudo systemctl enable docker |
Login DockerHub | sudo docker login |
Check nginx configuration | /usr/nginx/sbin/nginx -t |
Listen on 2375 for docker rest api | sudo dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock & |
Inspect the container changes | docker diff jenkins |
Commit the docker image from container | docker commit -m "finish the CI configuration for servers in jenkins" jenkins test/demo |
Push the docker image to DockerHub | docker push test/demo |
Basic commands
sudo docker info
sudo docker ps -a
sudo docker run --name deamon -d ubuntu:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"
sudo docker logs -ft deamon
sudo docker logs --tail=100 deamon
sudo docker top deamon
sudo docker exec -d deamon touch /etc/test_file
sudo docker start deamon
sudo docker inspect deamon
sudo docker inspect --format='{{ .NetworkSettings.IPAddress}}' develop
sudo docker rm d7cb3aad80d0
sudo docker build --no-cache -t="hustakin/develop" .
sudo docker run -d -p 8080:80 --name develop hustakin/develop nginx -g "daemon off;"
sudo docker run -d -P --name develop hustakin/develop nginx -g "daemon off;"
sudo docker history hustakin/develop
sudo docker port develop 80
docker kill $(docker ps -a -q)
sudo docker rm $(docker ps -a -q)
sudo docker run -t -i --name develop develop /bin/bash
sudo docker stats deamon
sudo docker system df
sudo docker history jenkins
docker system prune
docker system df -v
docker rmi $(docker images -q)
docker volume rm $(docker volume ls -qf dangling=true)
journalctl -r -u docker (logs of docker engine)
journalctl -u docker.service (logs of docker engine)
systemctl status docker.service(logs of starting docker engine)
journalctl -xe (detailed logs of starting docker engine)