Docker Tutorial

author: Frank

Getting Started

$
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine;
                  
sudo yum install -y yum-utils;

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo;
    
sudo yum install docker-ce docker-ce-cli containerd.io;

Take out sudo

$
sudo groupadd docker;
sudo usermod -aG docker $USER;
sudo restart

Launch Docker Deamon

$
sudo systemctl enable --now docker.service;

lesson 1

$
docker run hello-world;
docker ps -a;
docker stop {CID};
docker rm {CID};

docker –help docker info docker logs {CID} docker images

lesson 2

exec

$
docker run --rm -p 80:80 httpd
docker run --rm -d --name httpd01 -p 80:80 httpd
docker exec -it httpd01 bash
echo '<html><body><h1>Docker works!</h1></body></html>' > /usr/local/apache2/htdocs/index.html
docker stop httpd01
docker rm httpd01

http://127.0.0.1/

-p Publish 在port of OS對應到port of container self

lesson 3

volume

$
mkdir html;
echo '<html><head><title>GG</title></head><body><h1>Docker is work.</h1></body></html>' > ./html/index.html;
docker run --rm --name httpd01 -p 80:80 -v "$PWD"/html:/usr/local/apache2/htdocs/ httpd

lesson 4

volume

$
docker volume create --name workspace;
docker run --rm -d --name httpd01 -p 80:80 -v workspace:/usr/local/apache2/htdocs/ httpd
docker run --rm -it --name developer -v workspace:/root ubuntu bash

docker stop httpd01

lesson 5

network

$
docker network create net-mysql

docker run --rm -d --name simple-mysql -e MYSQL_ROOT_PASSWORD=root --network net-mysql mysql:5.7.25

docker run --rm -d --name simple-phpmyadmin -e PMA_HOST=simple-mysql -p 80:80 --network net-mysql phpmyadmin/phpmyadmin:latest

docker ps -a;

docker stop simple-mysql simple-phpmyadmin;
docker network rm net-mysql

lesson 6

net

// none
docker run --rm -it --name test --net=none busybox
// brige
docker run --rm -it --name test --net=bridge busybox
// host
docker run --rm -it --name test --net=host busybox

//container tty1
docker run --rm -it --name test busybox

docker exec -it test
->#ifconfig


//container tty2
docker run --rm -it --name test01 --net=container:test busybox

docker exec -it test01
->#ifconfig

lesson 7

exec & attach

docker run -dit --name gg busybox

docker exec -it gg sh
->ps aux
->exit

docker attach gg
->ps aux
->exit

docker start gg
docker attach gg

exit & “Ctrl p + Ctrl q”

lesson 8

Dockerfile (simple)

mkdir myproject && cd myproject
echo "hello" > hello
echo -e "FROM busybox\nCOPY /hello /\nRUN cat /hello" > Dockerfile
docker build -t helloapp:v1 .

Thank you for listening.