Docker經常使用命令

環境:VMware+CentOS 7.1(安裝和聯網什麼的就不說啦,這裏設置的IPv4地址爲:192.168.1.50)mysql

1. 安裝和啓動redis

安裝Docker:yum install dockersql

啓動Docker服務:docker

    service docker start(舊式啓動)、chkconfig docker on(舊式開機自啓)shell

    systemctl start docker.service(新式啓動)、systemctl enable docker.service(新式開機自啓)bash

PS:若是出現:Another app is currently holding the yum lock,則使用命令:rm -f /var/run/yum.pidapp

2. Docker鏡像基本命令tcp

檢索鏡像:docker search 鏡像名稱 (如:docker search mysql)spa

鏡像下載:docker pull 鏡像名稱(如:docker pull docker.io/mysql ).net

查看鏡像列表:docker images

刪除鏡像:docker rmi image-id

中止全部的container(見下),纔可以刪除其中的images:docker stop $(docker ps -a -q)

刪除images: docker rmi image id

想要刪除untagged images,也就是那些id爲<None>的image的話能夠用:docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

要刪除所有image的話:docker rmi $(docker images -q)

3. Docker容器基本命令

運行命令:docker run --name 取名 -d image-name(這裏--name後面接的爲運行時容器名稱 -d 表明datached意味着運行完後控制檯不會被阻礙,仍然能夠運行其餘命令)

查看運行中的容器列表:docker ps

查看運行和中止的容器:docker ps -a

啓動容器:docker start container-name/container-id(如:docker start 取名)

中止容器:docker stop container-name/container-id(如:docker stop 取名)

刪除容器:docker rm container-id

刪除全部容器:docker rm $(docker ps -a -q)

啓動容器帶端口映射:

        Docker容器中運行軟件的端口在本地和局域網不可訪問,此處操做是映射端口到當前主機端口上

             Redis:docker run [--name 取名]  -p 6379:6379 取名 -d image-name(如:docker run --name redis2 -p 6379:6379 -d docker.io/redis,此處的--name也能夠去掉,則會默認取名)

            MySQL:啓動MySQL和上面相似,可是要帶上一個-e參數,具體見https://hub.docker.com/r/mysql/mysql-server/(如:docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=密碼 -d docker.io/mysql)

有了如上操做,咱們就能開心的在本地鏈接Docker中的容器了,歐耶歐耶蹦擦擦~

4. 容器日誌:

查看當前容器日誌:docker logs container-name/container-id

5. 登陸容器:

 登陸命令:docker exec -it container-name/container-id bash

 Redis操做以下:

[root@localhost ~]# docker run --name redis -p 6379:6379 -d docker.io/redis
428b031e0cfc252341906964a7103e1b50005a5abc80cc0b68c2635d1e164175
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
428b031e0cfc        docker.io/redis     "docker-entrypoint.sh"   3 seconds ago       Up 1 seconds        0.0.0.0:6379->6379/tcp   redis
[root@localhost ~]# docker exec -it redis bash
root@428b031e0cfc:/data# re 
read             readonly         reboot           redis-check-rdb  redis-server     renice           resizepart
readarray        readprofile      redis-benchmark  redis-cli        remove-shell     reset            return
readlink         realpath         redis-check-aof  redis-sentinel   rename.ul        resize2fs        rev
root@428b031e0cfc:/data# redis-cli -h 192.168.1.50  
Could not connect to Redis at 192.168.1.50:6379: No route to host
Could not connect to Redis at 192.168.1.50:6379: No route to host
not connected> 
not connected> 
root@428b031e0cfc:/data# redis-cli                 
127.0.0.1:6379> set zhu aaa
OK
127.0.0.1:6379> get zhu
"aaa"
127.0.0.1:6379>

由於是本地的Redis,使用redis-cli便可,若是是遠程則:redis-cli -h 主機名 -p 密碼

MySQL操做以下:

[root@localhost ~]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=111111 -d docker.io/mysql
d3fde45b6338954866bb2d6722da947d3475067b665c120b195e344dbadb5460
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d3fde45b6338        docker.io/mysql     "docker-entrypoint.sh"   3 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp   mysql
428b031e0cfc        docker.io/redis     "docker-entrypoint.sh"   5 minutes ago       Up 5 minutes        0.0.0.0:6379->6379/tcp   redis
[root@localhost ~]# docker exec -it mysql bash
root@d3fde45b6338:/# mysql -uroot -p111111
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)

mysql>

純手打,歡迎拍磚~

 轉載請指明出處:http://my.oschina.net/u/1991646/blog/743151

相關文章
相關標籤/搜索