├── 1-building-first-image └── README.md ├── 2-image-dive └── README.md ├── 3-docker-compose ├── README.md ├── docker-compose.yml ├── images │ └── dig-output.png └── src │ └── index.php └── README.md /1-building-first-image/README.md: -------------------------------------------------------------------------------- 1 | # Hands-on #1 2 | 3 | In this first hands-on exercise, we're going to build our first container image, push it to Docker Hub, and then run a container using that image. 4 | 5 | **If you haven't done so yet**, [create a Docker Hub account](https://hub.docker.com/). You will need this to access Play with Docker and sharing images through Docker Hub. 6 | 7 | ## Getting Started 8 | 9 | For the workshop, we will be using Play with Docker, a tool created by several Docker Captains to help you get up and running. Beyond it being a great tool, we don't have to rely on the conference's network speeds, as the containers and images are running remotely. 10 | 11 | 1. Open your web browser to [http://play-with-docker.com](http://play-with-docker.com). 12 | 2. Login using your Docker Hub account. If you don't have an account yet, [you can do so here](https://hub.docker.com). 13 | 14 | 15 | ## Creating and running our first Image 16 | 17 | As we discussed, images are best created and maintained using Dockerfiles. These files are simple text files that provide instructions on how to create a container image. 18 | 19 | 1. In the PWD console, create a folder named `exercise1`. 20 | 2. In the `exercise1` directory, create a file named `index.php`. In that file, we're going to create the simplest PHP application: 21 | 22 | ```php 23 | Connection failed: No such file or directory 44 | 45 | 46 | ## Connecting to the Database 47 | 48 | If you look in the `src/index.php` file, you'll see some connection details at the top of the file. In order for the app to work, we need to get an actual connection to the database. 49 | 50 | When Docker starts a container, it makes itself the DNS resolver for all requests (DNS is used to lookup an IP address for a hostname). While many applications may connect to a database using an IP address, DNS is a _fantastic_ way to do service discovery (finding out where things are). 51 | 52 | 1. Let's get into the PHP container and see if it can find the database. 53 | 54 | ``` 55 | docker exec -ti 3-docker-compose_app_1 bash 56 | 57 | # Now, you have a bash inside the container. Let's install dig 58 | apt update && apt install dnsutils 59 | 60 | dig mysql 61 | ``` 62 | 63 | What you should is an `A` record returned that shows the name `mysql` resolving to an IP address. It may case, it resolved to `172.19.0.2` (see below). 64 | 65 | ![Dig output](./images/dig-output.png) 66 | 67 | 2. Let's verify that that is indeed the IP address for the container. First, we have to drop out of our container and get back on to the host. 68 | 69 | ``` 70 | exit 71 | ``` 72 | 73 | You should now see a prompt simply starting with `$`. 74 | 75 | 3. Let's look at the MySQL container and see what IP address it was given. 76 | 77 | ``` 78 | docker container inspect 3-docker-compose_mysql_1 79 | ``` 80 | 81 | This is pretty overwhelming, but it shows _all_ details for a container. If we look through the JSON, we'll see `NetworkSettings` -> `Networks` -> `3-compose-file` -> `IPAddress` is the same as what we saw in our `dig` command. Cool! 82 | 83 | A simpler way to get the IP address is using "filters" on the result 84 | 85 | ``` 86 | docker container inspect 3-docker-compose_mysql_1 -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 87 | ``` 88 | 89 | 4. Now that we know the hostname for sure, let's update the `index.php` file. 90 | 91 | ``` 92 | $DB_HOST = 'mysql'; 93 | ``` 94 | 95 | 5. Open up the app and you should now see the current time, as reported by the database! 96 | 97 | 98 | ## Wrap-up 99 | 100 | In this hands-on, we updated the compose file to use a custom image, rather than an image directly from Docker Hub. We then looked a little bit into container network and service discovery using DNS. And, finally, we got our app working. 101 | 102 | Hooray! 103 | -------------------------------------------------------------------------------- /3-docker-compose/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.7" 2 | 3 | services: 4 | app: 5 | image: php:7-apache 6 | ports: 7 | - 80:80 8 | volumes: 9 | - ./src:/var/www/html 10 | mysql: 11 | image: mysql:5.7 12 | environment: 13 | MYSQL_ROOT_PASSWORD: secret 14 | MYSQL_DATABASE: workshop 15 | volumes: 16 | - mysql-data:/var/lib/mysql 17 | 18 | volumes: 19 | mysql-data: 20 | -------------------------------------------------------------------------------- /3-docker-compose/images/dig-output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aboullaite/docker101-workshop/91278bcf6e8f8a34155893f5f7e4e2830b2976db/3-docker-compose/images/dig-output.png -------------------------------------------------------------------------------- /3-docker-compose/src/index.php: -------------------------------------------------------------------------------- 1 | connect_error) 10 | die("Connection failed: " . $conn->connect_error); 11 | 12 | $sql = "SELECT LOCALTIMESTAMP() as time FROM DUAL"; 13 | $row = $conn->query($sql)->fetch_assoc(); 14 | echo $row['time']; 15 | $conn->close(); 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker 101 tour 2 | 3 | The materials of Docker Morocco tour. 4 | This work is mainly inspired by [Michael Irwin and Ell Marquez](https://github.com/mikesir87/dceu-2018-workshop) worksop at DockerCon EU 2018! 5 | --------------------------------------------------------------------------------