開發中常常須要安裝一些經常使用的服務軟件,如Nginx、MySql、Redis等,若是按照普通的安裝方法,通常都相對比較繁瑣 —— 要通過下載軟件或源碼包,編譯安裝,配置,啓動等步驟,使用 Docker 來安裝這些服務軟件能極大地簡化安裝過程,且速度也很快。html
本文如下操做假定你已經裝好了docker,並作好了鏡像配置。若是沒有,請參考 Docker筆記(三):Docker安裝與配置mysql
~$ docker pull mysql:5.7
~$ mkdir -p apps/mysql/conf apps/mysql/data apps/mysql/logs
如上分別建立了配置文件目錄,數據存放目錄,以及日誌文件目錄linux
~$ docker run -d -p 3306:3306 --name mysql -v /home/devuser/apps/mysql/conf/my.cnf:/etc/mysql/conf.d/my.cnf \
-v /home/devuser/apps/mysql/logs:/var/log/mysql -v /home/devuser/apps/mysql/data:/var/lib/mysql \
--restart=always -e MYSQL_ROOT_PASSWORD=Passw0rd mysql:5.7
其中nginx
-d:表示在後臺運行
-p:宿主機端口與容器端口映射
–name:容器名稱
-v:宿主機目錄與容器目錄映射
–restart=always:除非被docker stop
命令明確中止,不然一直嘗試重啓處於中止態的容器;若是Docker重啓,也會自動啓動容器
-e:設置環境變量,這裏設置了mysql root用戶的密碼爲Passw0rdredis
如此,MySql服務就跑起來了,很快很簡單有木有。sql
~$ docker pull redis:5.0.5
~$ docker run -d --name redis -p 6379:6379 -v /home/devuser/apps/redis/data:/data \
--restart=always redis:5.0.5 redis-server --appendonly yes --requirepass "Passw1rd"
-p, -v 與上同,不贅述
redis-server –appendonly yes : 在容器啓動時執行redis-server命令,並打開redis持久化配置
–requirepass:設置密碼docker
~$ docker exec -it redis redis-cli -h 172.17.0.4 -p 6379 -a Passw1rd Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.17.0.4:6379> keys * (empty list or set) 172.17.0.4:6379>
這種方式把密碼暴露了,其它登陸用戶經過history便可看到密碼,不是太安全。可改用以下方式,安全
~$ docker exec -it redis redis-cli 127.0.0.1:6379> 127.0.0.1:6379> keys * (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 'Passw1rd' OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379>
~$ docker pull nginx
不加標籤默認會拉取最新的(latest)鏡像微信
~$ mkdir -p apps/nginx/html apps/nginx/logs apps/nginx/conf
~$ docker run -d -p 80:80 --name nginx nginx 1fdcd13457a6eaacb511878e10d84ffbe48fe63fd1fb3705f58b2d4195b151d8
這裏若是直接指定映射路徑運行會報錯,app
~$ docker run -d -p 80:80 --name nginx -v ~/apps/nginx/html:/usr/share/nginx/html -v ~/apps/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/apps/nginx/logs:/var/log/nginx nginx dab56c13f9e76aad37fcf73411c78d495a6466f1c0d214949650dbae44adddf4 docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:424: container init caused \"rootfs_linux.go:58: mounting \\\"/home/devuser/apps/nginx/conf/nginx.conf\\\" to rootfs \\\"/home/docker_image/overlay2/e40ccaf4d845a9af92487b47cbc4d505c5c776800ef8887c5b43833b10661427/merged\\\" at \\\"/home/docker_image/overlay2/e40ccaf4d845a9af92487b47cbc4d505c5c776800ef8887c5b43833b10661427/merged/etc/nginx/nginx.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
~$ docker cp 1fdcd13457a6:/etc/nginx/nginx.conf ~/apps/nginx/conf/
~$ docker stop 1fdcd1345 ~$ docker rm 1fdcd1345 ~$ docker run -d -p 80:80 --name nginx -v ~/apps/nginx/html:/usr/share/nginx/html -v ~/apps/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/apps/nginx/logs:/var/log/nginx nginx
~$ docker kill -s HUP nginx
相似於 nginx -s reload
本文沒有總結。
個人微信公衆號:jboost-ksxy (一個不僅有技術乾貨的公衆號,歡迎關注)
——————————————————————————————————————
原文出處:https://www.cnblogs.com/spec-dog/p/11320513.html