项目作者: faheem556

项目描述 :
Docker Crash Course Repo
高级语言: C#
项目地址: git://github.com/faheem556/tysons-code-coffee-docker-fundamentals.git


Tysons Code & Coffee - Docker Fundamentals Content

Introduction

Installation

  1. Install docker desktop or engine from the following locations

    • https://www.docker.com/products/docker-desktop or
    • https://docs.docker.com/install/

      Use the following command to verify the Docker engine is running on your machine.

      1. docker version

      Docker version displays information regarding the docker client and docker server. Docker engine listens on REST API endpoints and performs all actions remotely. Results are sent back to the client to display. docker version display version information and verifies that the client and the API server are both working correctly.

      Note: On Windows 10, switch to Linux containers and Share your “C” drive using settings from the Docker tray icon.

Basic Commands

  1. # Download centos image from docker hub. Docker hub is the default container image registry
  2. # 'centos' is the image name '7' is the tag we need
  3. docker pull centos:7
  4. # Check images available on your machine
  5. docker image ls
  6. # Download alpine Linux image.
  7. docker pull alpine
  8. # Check local images and notice docker pulled 'latest' tag for alpine
  9. docker image ls
  10. # Run our first container. Note container runs and exits
  11. docker run hell-world
  12. # Check local images, notice docker CLI automatically pulled the 'hello-world:latest' image for us
  13. docker image ls
  14. # Lets run the alpine container. Notice that the container runs and quits immediately without any display
  15. docker run alpine
  16. # Run alpine container again but attache your terminal to the container
  17. docker run -ti alpine
  18. # NOTE: Run following commands inside the 'alpine' container
  19. # Check IP address. Note it's different from your machine's IP address
  20. ip addr
  21. # Create a file
  22. echo 'Hello container' > test.txt
  23. cat test.txt
  24. # Exit from the container
  25. exit
  26. # Run the alpine container again and note
  27. docker run -ti alpine
  28. # NOTE: Run following commands inside the 'alpine' container
  29. # Note that the file you created previously is gone
  30. cat test.txt
  31. # Exit out of the container
  32. exit
  33. # Create folder on your machine
  34. mkdir share
  35. # Run alpine container again but mount a volume from your host machine using '-v' option
  36. # On Windows you have to enter the absolute path. You may enter current path in Mac or Linux
  37. docker run -ti -v C:\Users\faheem\codes\tysons\share:/share alpine sh
  38. # Inside the container
  39. echo 'Hello container' > /share/test.txt
  40. exit
  41. # Get contents from the current folder and notice that the file is preserved
  42. ls ./share
  43. # Publish ports of the docker container on the host using '-p' option
  44. docker run -d -p 4000:80 nginx
  45. # Either check the response in your browser 'localhost:4000' or do curl
  46. curl http://localhost:4000
  47. # The '-d' switch runs the container in detached mode (background)
  48. # Use the following command to see all running containers
  49. docker container ls
  50. # Stop the running container using the following command
  51. docker stop <copy and paste the container id from the previous command>
  52. # Check non-running containers
  53. docker container ls -a
  54. # Clean all non-running containers and other cruft using the following command
  55. docker system prune
  56. # System prune doesn't delete container images
  57. docker image ls
  58. # Run Nginx container again and do curl
  59. docker run -d -p 4000:80 nginx
  60. curl localhost:4000
  61. # Run following command to get console output from the container
  62. docker container ls
  63. docker logs <nginx container id>
  64. # Stop the Nginx container and clean-up
  65. docker container stop <nginx container id>
  66. docker container rm <nginx container id>

Docker image vendors use tags to release multiple version of the same software. latest is a special tag which always points to the current release. It may not be suitable for production scenarios. For production look for stable, lts, or specific version tags.

Building container images

HTML Website using NGINX

  1. # Clone this repository
  2. git clone https://github.com/memonfaheem/tysons-code-coffee-docker-fundamentals.git
  3. # Checkout ngix folder
  4. cd nginx
  5. ls
  6. # code_coffee.jpeg: Image used on the page
  7. # default.conf: NGINX configuration
  8. # Dockerfile: Default dockerfile used to build docker build
  9. # index.html: Our HTML code we want to deploy
  10. cat Dockerfile
  11. # 'FROM' command specifies our base image. We are going to build on top of the nginx:alpine image
  12. # We are using 'COPY' commands to copy files from our folder context to the image
  13. # Build our docker image
  14. # '-t' specifies the name and tag of the image. 'latest' tag is assumed if none is specified
  15. # '.' at the end is the folder context sent to the docker
  16. docker build -t hello-tysons .
  17. # Check local images, note our image is created successfully
  18. docker image ls
  19. # Let's run our new image. The '--rm' option will remove the container automatically when stopped.
  20. docker run -d --rm -p 4000:80 hello-tysons
  21. # Open http://localhost:4000 in your browser
  22. # Stop the container
  23. docker container ls
  24. docker container stop <container id>
  25. # Open index.html and change 'Hello World!' to 'Hello Tysons Code & Coffee!"
  26. # Rebuild and run the container
  27. docker build -t hello-tysons .
  28. # Run, test and stop the container
  29. docker run -d --rm -p 4000:80 hello-tysons
  30. curl http://localhost:4000
  31. docker container stop (docker container ls -aq)

Angular App

  1. # Change directory to angular-app
  2. cd .\angular-app\
  3. # The application has angular files, Dockerfile, multi-stage.Dockerfile, and nginx.conf
  4. # You would need angular CLI to build the app. Instead, if you want to build inside another docker container, skip these steps and move to the multi-stage build
  5. npm install
  6. ng build -prod
  7. docker built -t angular-app .
  8. docker image ls
  9. # Test the new container
  10. docker run -d --rm -p 3000:80 angular-app
  11. docker container ls
  12. curl localhost:3000
  13. # Stop the angular container
  14. docker container stop $(docker container ls -aq)
  15. # Multi-stage Build Images: Instead of building the application locally and
  16. # then copying the ./dist files to the image. We use another container to build
  17. # the app and copy the files over to the eventual container mid-flight.
  18. docker build -t angular-app2 -f .\multi-stage.Dockerfile .
  19. docker image ls
  20. # Test the new image
  21. docker run -d --rm -p 3000:80 angular-app2
  22. docker container ls
  23. curl http://localhost:3000
  24. # Stop the angular conatiner
  25. docker container stop $(docker conatiner ls -aq)

FunFact (DotNet Core App)

  1. # Build .NET App Container
  2. cd ../funfact
  3. docker build -t funfact .
  4. # Test conatiner
  5. docker run -d --rm -p 5000:5000 funfact
  6. curl http://localhost:5000
  7. curl http://localhost:5000/home/fact
  8. # Stop the container
  9. docker container stop $(docker container ls -aq)