Docker Cheat Sheet

Docker Cheat Sheet

Background

Command

  • Know docker container name
    1
    2
      $ docker ps
      # Then you'll find the container name at the end of the result
    
  • Go into container bash shell
    1
    2
    3
    4
    5
      $ docker exec -it <container_name> bash
        
      # 以root的身份進入container
      # 極度建議以此方法進入bash
      $ docker exec -u root -it <container_id> /bin/bash
    
  • Check Log
    1
      $ docker-compose logs
    
  • 啟動所有的 Docker Container 指令如下
    1
      $ docker-compose up -d
    
  • 停止 docker-compose 執行的所有 Container
    1
      $ docker-compose stop
    
  • 刪除 docker-compose 的所有 Container
    1
      $ docker-compose rm
    
  • Leave Container exit or ctrl-D
  • 利用Dockerfile build一個Images並且實際跑起來
    1
      $ docker run -it --rm $(docker build -q .) /bin/sh
    
  • 實作
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $ docker pull httpd    # with the latest version
    $ docker images        # check the current images status
    REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
    httpd        latest    75a48b16cd56   4 days ago   168MB
    $ docker create --name test -p 8080:80 httpd    # create a container with the name test and port number is 80
    70fd43b63fa04c0daebd8128eff7ec58de26cb5c4c7bf63c0cf30fd03d07f1ab
    $ docker start 70fd43b63fa0    # start the container
    70fd43b63fa0
    $ docker ps -a                 # check container status
    CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS          PORTS                  NAMES
    70fd43b63fa0   httpd     "httpd-foreground"       2 minutes ago   Up 27 seconds   0.0.0.0:8080->80/tcp   test
    $ docker exec -it test bash    # get into container with bash shell as terminal
    root@70fd43b63fa0:/usr/local/apache2#
    

Reference

PADNS Midterm Reference

執行步驟

  1. Destroy Droplet
  2. Use the new password to login(be send by Digital Ocean) And then change your password.
  3. Follow the note to install requirement application
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     $ apt-get update
     $ apt-get install ca-certificates curl gnupg lsb-release
     $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
     $ echo \
       "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
     $ apt-get update
     $ apt-get install docker-ce docker-ce-cli containerd.io
     $ docker run hello-world
     $ curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
     $ chmod +x /usr/local/bin/docker-compose
     $ docker-compose --version
    
  4. Deploy docker
    1
     $ docker-compose up -d
    
  5. 外網掛域名
    1
    2
    3
    4
    5
     $ apt install nginx -y
     $ vim /etc/nginx/sites-enabled/website
     $ vim docker-compose.yml
     # add hostname: test.fei.works
     $ service nginx restart
    

    重要: It can not set https