# Docker Lab 2021.03.29 James Hsu https://reurl.cc/V3e535 --- # Docker ---- ### What is Docker? # - Light-weight virtualization - Build once, run anywhere ![](https://miro.medium.com/max/336/0*di43EuQW2WL9gGVb.png) ---- ### Why Docker? - To solve environment conflict - Develop programs in a more agile way ---- ### Basic Docker Concept - Images - Containers - Registry ---- ### Install Docker - [Ubuntu](https://docs.docker.com/engine/install/ubuntu/) - [Windows (need Hyper-V)](https://docs.docker.com/docker-for-windows/install/) - [Mac](https://docs.docker.com/docker-for-mac/install/) - [WorkStation](https://wslab.csie.ntu.edu.tw/docker_tutorial.html) ---- ### Getting Started ```bash $ docker run hello-world ``` You may need `sudo` to run docker. ---- ![](https://i.imgur.com/dhdE1Ul.png) ---- ### Use Docker as alternative VM Not recommended. A good way to start though. ---- ### Use Docker as alternative VM ```bash $ docker pull ubuntu:18.04 $ docker run -it ubuntu:18.04 bash ``` | Short | Full | Description | | -------- | ------------- | ------------------------------------ | | -i | - -interactive | Keep STDIN open even if not attached | | -t | - -tty | Allocate a pseudo-TTY | ---- ### How to Exit if you're in container's shell: ```exit``` and it will kill the container itself. ---- ### How to Exit Or **ctrl+p then ctrl+q**, and this way the container will remain. ---- ### How many containers are running? ```bash= $ docker ps ``` ![](https://i.imgur.com/I4B8t4D.png) ---- ### Some other useful cmds ```bash= $ docker rm -f $container_id $ docker logs $container_id # or $container_name $ docker restart $container_name $ docker run -d --restart=always -p 3306:3306 mysql:8.0 # -d: run as a daemon process $ docker exec -it $container_id bash $ docker image ls $ docker system prune ``` --- # Dockerfile ---- If we want a ubuntu:18.04 image with python3? ```bash $ docker run -it ubuntu:18.04 bash root@ad385d11a855:/$ apt update root@ad385d11a855:/$ apt install -y python3 root@ad385d11a855:/$ python3 ``` ---- If we need to do that a thousand times... ![](https://i.imgur.com/gcB2bfY.png) Wait, where're the images from? ---- ### Build the image yourself ---- Create a file named `Dockerfile` ```dockerfile= FROM ubuntu:18.04 RUN apt update && apt-get install -y python3 ``` ---- ```bash= $ docker build -t ubuntu-with-python3 . $ docker run -it ubuntu-with-python3 bash root@ad385d11a855:/$ python3 ``` ![](https://i.imgur.com/S9pD6CR.png) ---- ```dockerfile= FROM ubuntu:18.04 # Necessary to define source image FROM scratch # An empty image ``` ---- ```dockerfile= RUN apt update RUN python3 setup.py RUN cd /home/ RUN pwd ``` ---- ```dockerfile= ADD src /src/ COPY requirements.txt /tmp/ ``` ---- `COPY` is more transparent than `ADD`. Example: ```dockerfile= ADD rootfs.tar.xz / ``` ---- ```dockerfile= ENV foo=bar ENV PATH=$PATH:/jdk1.8.0_152/bin ``` ---- ```dockerfile= CMD ["python3"] CMD ["bash", "hello.sh"] ``` ---- ```dockerfile= ARG MYSQL_USERNAME EXPOSE 8000 # No effect, just as a document ``` [See More](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) ---- ```bash= $ docker tag ubuntu-with-python3 b07902001/ubuntu-with-python3:1.0 $ docker push b07902001/ubuntu-with-python3:1.0 ``` --- # Lab ---- Create a python3-flask container using Dockerfile. ---- Download the code from [my gist](https://gist.github.com/jameshwc/18011f20b0025dd7923ea26dcc19ad46) ---- You can first run the code in the host (with flask and psutil installed) ![](https://i.imgur.com/RvlGuML.png) ![](https://i.imgur.com/8JneE5i.png) ---- Use whatever base image you like. Ubuntu/CentOS/Python:3.6/... ---- You'll see something like this if you've done it successfully. ![](https://i.imgur.com/jLbKko5.png) ---- Hint 1: The script used to run docker image may be something like this. ![](https://i.imgur.com/VGmyza1.png) ---- Hint 2: Use ```bash= $ docker run -it ubuntu:18.04 bash ``` to debug and find what tools you need. ---- Hint3: The last command in Dockerfile should start with ```CMD```. --- Docker-compose An easy way to start multiple containers simultaneously. ---- Use yaml to config ![](https://i.imgur.com/xK9KHvF.png) ---- [Install](https://docs.docker.com/compose/install/) ---- We use [sameersbn/docker-gitlab](https://github.com/sameersbn/docker-gitlab) to demo. ```bash= $ wget https://raw.githubusercontent.com/sameersbn/docker-gitlab/master/docker-compose.yml $ docker-compose up -d ``` ---- Browse `localhost:10080` and you will see a gitlab website (wait a minute if you see 502) ![](https://i.imgur.com/6JoTAZW.png) ---- Practice: Change the port number `10080` to `80` --- Some Concept to master Docker - one process per container - carefully use cache when building image - use alpine instead of ubuntu as base image - remove useless files and modules
{"metaMigratedAt":"2023-06-15T20:26:45.935Z","metaMigratedFrom":"YAML","title":"Docker Lab","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"9f1940e7-3a0c-47b1-a38c-06003f9cf4d2\",\"add\":5907,\"del\":766}]"}
    863 views