|
| 1 | +# Hello, world! |
| 2 | +As a reminder, when we ran the `hello-world` image, we saw the following output: |
| 3 | +```bash |
| 4 | +$ docker run hello-world |
| 5 | +``` |
| 6 | +```bash |
| 7 | +Hello from Docker! |
| 8 | +This message shows that your installation appears to be working correctly. |
| 9 | + |
| 10 | +To generate this message, Docker took the following steps: |
| 11 | + 1. The Docker client contacted the Docker daemon. |
| 12 | + 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. |
| 13 | + (amd64) |
| 14 | + 3. The Docker daemon created a new container from that image which runs the |
| 15 | + executable that produces the output you are currently reading. |
| 16 | + 4. The Docker daemon streamed that output to the Docker client, which sent it |
| 17 | + to your terminal. |
| 18 | + |
| 19 | +To try something more ambitious, you can run an Ubuntu container with: |
| 20 | + $ docker run -it ubuntu bash |
| 21 | + |
| 22 | +Share images, automate workflows, and more with a free Docker ID: |
| 23 | + https://hub.docker.com/ |
| 24 | + |
| 25 | +For more examples and ideas, visit: |
| 26 | + https://docs.docker.com/get-started/ |
| 27 | +``` |
| 28 | + |
| 29 | +As a simple first step, let's try and recreate this image from scratch. We will create a new directory called `hello-world` and create a new file called `Dockerfile` inside it. The contents of the `Dockerfile` will be as follows: |
| 30 | +```Dockerfile |
| 31 | +# Use a minimal base image |
| 32 | +FROM alpine:latest |
| 33 | + |
| 34 | +# Create a simple script that will be our executable |
| 35 | +COPY hello.sh / |
| 36 | +RUN chmod +x /hello.sh |
| 37 | + |
| 38 | +# Set the script as our container's entry point |
| 39 | +ENTRYPOINT ["/hello.sh"] |
| 40 | +``` |
| 41 | + |
| 42 | +and also create a new file called `hello.sh` with the following contents: |
| 43 | +```bash |
| 44 | +#!/bin/sh |
| 45 | + |
| 46 | +echo "Hello from your custom Docker container!" |
| 47 | +echo "This message demonstrates that you've successfully:" |
| 48 | +echo "" |
| 49 | +echo " 1. Built a custom Docker image from a Dockerfile" |
| 50 | +echo " 2. Created a container from that image" |
| 51 | +echo " 3. Executed a script inside the container" |
| 52 | +echo " 4. Had the output streamed back to your terminal" |
| 53 | +echo "" |
| 54 | +echo "Things this example demonstrates:" |
| 55 | +echo " - Using a base image (alpine:latest)" |
| 56 | +echo " - Copying files into a container" |
| 57 | +echo " - Setting file permissions" |
| 58 | +echo " - Using ENTRYPOINT to define container behavior" |
| 59 | +echo "" |
| 60 | +echo "Try these next steps:" |
| 61 | +echo " 1. Look at the Dockerfile to see how this works" |
| 62 | +echo " 2. Modify the script to print different messages" |
| 63 | +echo " 3. Rebuild the image to see your changes" |
| 64 | +echo "" |
| 65 | +echo "Happy Dockerizing! 🐳" |
| 66 | +``` |
| 67 | + |
| 68 | +We can now run |
| 69 | +```bash |
| 70 | +docker build -t my-hello-world . |
| 71 | +``` |
| 72 | + |
| 73 | +and then |
| 74 | +```bash |
| 75 | +docker run my-hello-world |
| 76 | +``` |
| 77 | + |
| 78 | +and we should see the following output: |
| 79 | +```text |
| 80 | +Hello from your custom Docker container! |
| 81 | +This message demonstrates that you've successfully: |
| 82 | +
|
| 83 | + 1. Built a custom Docker image from a Dockerfile |
| 84 | + 2. Created a container from that image |
| 85 | + 3. Executed a script inside the container |
| 86 | + 4. Had the output streamed back to your terminal |
| 87 | +
|
| 88 | +Things this example demonstrates: |
| 89 | + - Using a base image (alpine:latest) |
| 90 | + - Copying files into a container |
| 91 | + - Setting file permissions |
| 92 | + - Using ENTRYPOINT to define container behavior |
| 93 | +
|
| 94 | +Try these next steps: |
| 95 | + 1. Look at the Dockerfile to see how this works |
| 96 | + 2. Modify the script to print different messages |
| 97 | + 3. Rebuild the image to see your changes |
| 98 | +
|
| 99 | +Happy Dockerizing! 🐳 |
| 100 | +``` |
| 101 | + |
| 102 | +Let's try something harder... |
0 commit comments