Docker基础操作

安装curl时报错

curl : Depends: libcurl3-gnutls (= 7.47.0-1ubuntu2.12) but 7.58.0-2ubuntu3.6 is to be installed

sudo apt-get purge  libcurl3-gnutls
sudo apt-get install curl

安装docker

curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh

安装完成后有个提示:

If you would like to use Docker as a non-root user, you should now consider
    adding your user to the "docker" group with something like:

    sudo usermod -aG docker liukang
   Remember that you will have to log out and back in for this to take effect!  

当要以非root用户可以直接运行docker时,需要执行 sudo usermod -aG docker liukang 命令,然后重新登陆,否则会报错

liukang@ubuntu:~/docker$ sudo service docker start

查看docker版本,验证是否安装成功

liukang@ubuntu:~/docker$ docker version
Client: Docker Engine - Community
 Version:           19.03.3
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        a872fc2
 Built:             Tue Oct  8 00:59:54 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.3
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       a872fc2
  Built:            Tue Oct  8 00:58:28 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

docker info

常用操作命令

docker run hello-world
docker run ubuntu:15.10 /bin/echo "hello world"
运行交互式的容器

docker run -i -t ubuntu:15.10 /bin/bash
root@dc0050c79503:/# cat /proc/version

可以通过运行exit命令或者使用CTRL+D来退出容器。

启动容器(后台模式)
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
docker ps
docker logs 2b1b7a428627
docker stop 2b1b7a428627

docker images  来列出本地主机上的镜像。
docker search httpd  搜索 httpd 来寻找适合我们的镜像。
docker pull httpd

docker nginx

docker run --name liukang-nginx -p 8081:80 -d nginx
docker ps -a
docker stop liukang-nginx
docker restart liukang-nginx
docker exec -it liukang_nginx /bin/bash

docker cp liukang-nginx:/etc/nginx/conf.d/default.conf ~/

docker cp ~/default.conf liukang-nginx:/etc/nginx/conf.d/default.conf

删除所有容器
docker rm $(docker ps -a -q)
删除所有镜像
docker rmi $(docker images -q)

docker run -d -p 8081:80 --name liukang-nginx -v /home/liukang/workspace/ReactCar/build:/usr/share/nginx/html nginx

docker 更新镜像

docker run -i -t --name nginx-bash nginx /bin/bash
#apt-get update 非必须

另一个终端去拷贝部署文件和配置到容器中

docker cp ~/default.conf nginx-bash:/etc/nginx/conf.d/

docker commit -m="has update" -a="liukang" 924eadfb1864 liukang/nginx

docker images 查看所有镜像中是否有刚才新加的
docker run -i -t liukang/nginx /bin/bash 可到系统内部去检查

docker镜像导出,tar镜像包可以拷贝到其他机器docker环境中加载
docker save liukang/nginx -o liukangNginx.tar

scp -P 23 liukangNginx.tar [email protected]:/home/tu

docker镜像加载
docker load -i liukangNginx.tar
sudo docker run -d -p 2334:80 --name react-nginx -v /home/tu/nginx/www:/usr/share/nginx/html nginx

docker镜像制件

Dockerfile

FROM    nginx

COPY default.conf /etc/nginx/conf.d/
ADD build/ /usr/share/nginx/html/
liukang@ubuntu:~/docker/makedocker$ ls
build  default.conf  Dockerfile
liukang@ubuntu:~/docker/makedocker$ docker build -t liukang/reactnginx .
docker save liukang/reactnginx -o liukangReactNginx.tar

docker mysql

docker pull mysql:latest
docker run -di --name liukang-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
docker exec -it liukang-mysql /bin/bash
# mysql -u root -p
mysql> status;
修改root 可以通过任何客户端连接
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

docker redis

docker pull redis:3.2
docker run -p 6379:6379 -d --name liukang-redis redis:3.2 redis-server --appendonly yes

你可能感兴趣的:(工具)