You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
programming-basics-2022/10_docker/lesson02/README.md

3.0 KiB

Lesson 2: Introduction to Docker image builds

  1. Clone this git repository.

  2. Change directory into lesson02:

     $ cd lesson02
    
  3. Why are containers useful? What are their advantages over a traditional server?

    • containers are light
    • containers are portable
    • containers are isolated
    • containers can be run "immutably"
    • containers are built hierarchically
    • developers can create applications without a full server-stack
  4. What are some limitations of containers?

    • interaction is more difficult for multiple containers than for multiple server process (although Kubernetes helps)
    • some overhead so not quite as fast as "bare-metal" processes
    • there are several decades worth of server administration best practices and tools but only a few years for containers
    • not good for large tightly-integrated applications (e.g., Oracle database)
  5. Question: what is the difference between an "image" and a "container"? (See also this Stackoverflow question).

  6. Most Docker images are build on top of existing "base" images. These base containers are usually hosted in Docker Hub. For example, all Debian releases come as Docker images; see https://hub.docker.com/_/debian for a list of the base Debian Docker images.

  7. Let's build a "Hello, world." Docker image. We will build it on a Debian buster base. First, pull the Docker image:

     $ docker pull debian:buster-slim
     # We pull the "slim" image to save disk space
    
  8. Here is an application that echos "Hello, world." and then exits (this file is also in the current directory).

     #!/bin/sh
     echo "Hello, world."
     exit 0
    
  9. Now we create a "Dockerfile" that tells the build process how to create the image. We use debian:buster-slim as the base and "add" the command run.sh. The first argument of ADD is the local copy of the file and the second argument is where we want the file to be in the image.

     # Dockerfile
     FROM debian:buster-slim
     LABEL maintainer="adamhl@stanford.edu"
    
     ADD run.sh /root/run.sh
    
  10. We want to make sure that the script will run, so make it executable.

     # Dockerfile
     FROM debian:buster-slim
     LABEL maintainer="adamhl@stanford.edu"
    
     ADD run.sh /root/run.sh
     RUN chmod a+x /root/run.sh
    
  11. Docker containers must be told which command to run. We do this with the CMD directive

     # Dockerfile
     FROM debian:buster-slim
     LABEL maintainer="adamhl@stanford.edu"
    
     ADD run.sh /root/run.sh
     RUN chmod a+x /root/run.sh
     CMD /root/run.sh
    
  12. We are now ready to build the image:

     $ docker build . -t hello-world
     $ docker images | grep hello-world
    
  13. Question: what is the purpose of . (dot) in the above docker build command?

  14. Note that the tag is latest (the default).

  15. Let's run this image in a container:

     $ docker run --rm hello-world
    
  16. Did you see what you expected?