docker安装nginx
pull拉去Nginx镜像下来,并且启动
docker run -d -p 80:80 --restart=always nginx:latest
# -d 后台启动 -p 宿主机的80端口映射容器的80端口
# --restart=always 伴随docker重启自动重启该容器
进入容器
docker ps # 获取运行容器信息
docker exec -it 容器名|id /bin/bash
文件拷贝 首先我们需要拿到容器id的全称:
root@gj-master:/etc/docker# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e531c6f483b redis:4.0 "docker-entrypoint.s…" 23 hours ago Up 10 minutes 0.0.0.0:6379->6379/tcp myredis
cc9666a8d587 nginx "nginx -g 'daemon of…" 3 weeks ago Up 10 minutes 0.0.0.0:8000->80/tcp mynginx
root@gj-master:/etc/docker# docker inspect -f '{{.Id}}' cc9666a8d587
cc9666a8d587e0036bccb3c8451f06af30ed955207f939b56cd2f44a2bf542d1
root@gj-master:/etc/docker#
将文件拷贝到docker容器中:
docker cp /opt/html/user_login.html cc9666a8d587e0036bccb3c8451f06af30ed955207f939b56cd2f44a2bf542d1:/root/user_login.html
进入容器查看
root@gj-master:/etc/docker# docker exec -it mynginx /bin/bash
root@cc9666a8d587:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@cc9666a8d587:/# cd /root/
root@cc9666a8d587:~# ls
user_login.html
root@cc9666a8d587:~#
docker里面安装redis
pulll拉取镜像
docker pull redis:4.0
启动redis镜像
root@gj-master:/etc/docker# docker run -p 6379:6379 \
-v $PWD/data:/data \
-v $PWD/redis.conf:/etc/redis/redis.conf \
--privileged=true \
--name myredis \
-d redis:4.0 redis-server /etc/redis/redis.conf \
--appendonly yes
# -p 6379:6379: 将容器的6379端口映射到主机的6379端口
# -v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data
# redis-server —appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
备注,“redis-server /etc/redis/redis.conf”可能不成功,因为配置文件中指定了各种文件路径,而容器中可能不存在路径,可以先启动容器,再进入容器中重启Redis:
# 配置文件/etc/redis/redis.conf设置密码
# use a very strong password otherwise it will be very easy to break.
requirepass 123456
重启Redis
root@6e531c6f483b:/#docker exec -it redis /bin/bash
root@6e531c6f483b:/# redis-server /etc/redis/redis.conf
root@VM-0-4-ubuntu:/etc/docker/redis# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:d4ff:fed5:1017 prefixlen 64 scopeid 0x20<link>
ether 02:42:d4:d5:10:17 txqueuelen 0 (Ethernet)
RX packets 223035 bytes 14681300 (14.6 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 240303 bytes 123073991 (123.0 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.21.0.4 netmask 255.255.240.0 broadcast 172.21.15.255
inet6 fe80::5054:ff:fe28:ee1f prefixlen 64 scopeid 0x20<link>
ether 52:54:00:28:ee:1f txqueuelen 1000 (Ethernet)
RX packets 79189019 bytes 8710277698 (8.7 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 80967042 bytes 12372145190 (12.3 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 7623492 bytes 880953027 (880.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7623492 bytes 880953027 (880.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker安装MySQL
拉取镜像
docker pull mysql:latest
运行容器
# MYSQL_ROOT_PASSWORD=123456 mysql数据库开启安全认证,密码123456,用户名root
$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
参数说明:
- -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过宿主机ip:3306访问到MySQL的服务。
- MYSQL_ROOT_PASSWORD=123456:设置MySQL服务root用户的密码。
更多配置
# 把主机的/conf/mysql文件夹挂载到 mysqldocker容器的/etc/mysql/文件夹里面
# 改mysql的配置文件就只需要把mysql配置文件放在自定义的文件夹下(/etc/mysql/)
docker run --name mysql-test -v /etc/mysql:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
# 指定mysql的一些配置参数
docker run --name mysql-test -e MYSQL_ROOT_PASSWORD=123456 -d mysql \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
# 设置mysql server的编码
docker run -p 3307:3306 --name mysq-test \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
登录mysql服务器
root@gj-master:/etc/mysql/mysql.conf.d# mysql -uroot -h0.0.0.0 -P3307 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
# 一定带上ip和port