Skip to content

Commit

Permalink
Update 2024 (#9)
Browse files Browse the repository at this point in the history
* Add compose section

* Add "#include <stdio.h>" to hello.c example (#5)

* Update intro and container sections (#6)

* Fix automation section (#8)

* Add Github action section

* Update automation section

* Add Docker compose section (#7)

* Add Compose content

* Add Compose content

* Add Compose content

* Add compose section

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

* Add Compose content

---------

Co-authored-by: Gioacchino Vino <[email protected]>

---------

Co-authored-by: Lisa <[email protected]>
Co-authored-by: jvino <[email protected]>
Co-authored-by: Gioacchino Vino <[email protected]>
  • Loading branch information
4 people authored Sep 9, 2024
1 parent a9a9403 commit 19ecd5b
Show file tree
Hide file tree
Showing 14 changed files with 1,127 additions and 323 deletions.
710 changes: 710 additions & 0 deletions docs/compose/concepts.md

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions docs/compose/hands-on.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

### Hands-on 1

Write the docker-compose.yml file for the following Docker CLI inserting the `depends_on` condition on db health check

=== "Exercise details"
```bash
docker network create wordpress_net
docker volume create db_data
docker volume create wp_data
docker container run --name db \
--network wordpress_net \
-v db_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=somewordpress \
-e MYSQL_USER=wordpress_user \
-e MYSQL_PASSWORD=wordpress_password \
-e MYSQL_DATABASE=wordpress_db \
--restart always \
--health-cmd="mysqladmin ping --silent" \
--health-interval=10s \
--health-start-period=10s \
--health-timeout=10s \
--health-retries=60 \
--restart always \
-d \
mariadb:10.6.4-focal

docker run --name wp \
--network wordpress_net \
-v wp_data:/var/www/html \
-p 8080:80 \
-e WORDPRESS_DB_HOST=db \
-e WORDPRESS_DB_USER=wordpress_user \
-e WORDPRESS_DB_PASSWORD=wordpress_password \
-e WORDPRESS_DB_NAME=wordpress_database \
-d \
wordpress
```
=== "Solution"
```yaml
services:
db:
image: mariadb:10.6.4-focal
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress_database
- MYSQL_USER=wordpress_user
- MYSQL_PASSWORD=wordpress_password
healthcheck:
test: ["CMD", "mysqladmin", "ping", "--silent"]
interval: 10s
timeout: 10s
retries: 60
start_period: 10s

wordpress:
image: wordpress:latest
volumes:
- wp_data:/var/www/html
ports:
- 8081:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress_user
- WORDPRESS_DB_PASSWORD=wordpress_password
- WORDPRESS_DB_NAME=wordpress_database
depends_on:
db:
condition: service_healthy

volumes:
db_data:
wp_data:
```

### Hands-on 2

Write the docker-compose.yml file that builds the following Dockerfile and uses it

=== "Exercise details"
```
base-image: jupyter/minimal-notebook
python module to install: pandas, numpy
expose port: 8888
command: /opt/conda/bin/python3.11 /opt/conda/bin/jupyter-lab \
--no-browser \
--allow-root \
--NotebookApp.token='' \
--NotebookApp.password=''
```
=== "Solution"
```bash
mkdir exercise2
cd exercise2
cat > requirements.txt << EOF
pandas
numpy
EOF

cat > Dockerfile << EOF
FROM jupyter/minimal-notebook
COPY requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
EOF

cat > compose.yaml << EOF
services:
my_jupyter:
build: .
ports:
- 8888:8888
command: /opt/conda/bin/python3.11 /opt/conda/bin/jupyter-lab --no-browser --allow-root --NotebookApp.token='' --NotebookApp.password=''
EOF

# To execute the application run:
# docker compose up
```
261 changes: 113 additions & 148 deletions docs/container/example.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/container/health_checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ The container is flagged as unhealthy after three failures of the check (you can
-e MYSQL_USER=${MYSQL_USER} \
-e MYSQL_PASSWORD=${MYSQL_PASSWORD} \
-e MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
--health-cmd='mysqladmin -p${MYSQL_ROOT_PASSWORD} ping -h localhost' \
--health-cmd='mariadb-admin -p${MYSQL_ROOT_PASSWORD} ping -h localhost' \
--health-interval=20s \
--health-retries=3 \
mariadb:latest
Expand Down
48 changes: 25 additions & 23 deletions docs/container/manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Let's see what happens:
```bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
445a6a12be2b: Pull complete
Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
31e907dcc94a: Pull complete
Digest: sha256:8a37d68f4f73ebf3d4efafbcf66379bf3728902a8038616808f04e34a9ab63ee
Status: Downloaded newer image for ubuntu:latest
root@2c8a2962ca5c:/#
root@0e539c9ccee4:/#
```

We are inside the docker container!
Expand All @@ -26,32 +26,34 @@ We are inside the docker container!
This is a fully fledged Ubuntu host, and we can do anything we like in it. Let's explore it a bit, starting with asking for its hostname:

```bash
root@2c8a2962ca5c:/# hostname
2c8a2962ca5c
root@0e539c9ccee4:/# hostname
0e539c9ccee4
```

!!! tip
A container's hostname defaults to be the container's ID in Docker. You can override the hostname using `--hostname`.

Let's have a look at the `/etc/hosts` file too.
```bash
root@2c8a2962ca5c:/# cat /etc/hosts
root@0e539c9ccee4:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 2c8a2962ca5c
172.17.0.2 0e539c9ccee4
```
Docker has also added a host entry for our container with its IP address. Let's also check out its networking configuration.

```bash
root@2c8a2962ca5c:/# ip a
root@0e539c9ccee4:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
Expand All @@ -68,10 +70,10 @@ As we can see, we have the `lo` loopback interface and the `eth0@if7` network in
We can also check its running processes:

```bash
root@2c8a2962ca5c:/# ps aux
root@0e539c9ccee4:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4624 3780 pts/0 Ss 07:43 0:00 bash
root 352 0.0 0.0 7060 1568 pts/0 R+ 07:53 0:00 ps aux
root 1 0.0 0.0 4588 3968 pts/0 Ss 11:04 0:00 bash
root 334 0.0 0.1 7888 4224 pts/0 R+ 11:08 0:00 ps aux
```

Note that the process `bash` has PID 1.
Expand All @@ -83,15 +85,15 @@ So the container still exists but it's stopped:
```bash
docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c8a2962ca5c ubuntu "bash" 11 minutes ago Exited (0) 13 seconds ago magical_sanderson
3a3eb75d3da3 hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago sharp_kepler
0e539c9ccee4 ubuntu "bash" 5 minutes ago Exited (0) 15 seconds ago frosty_satoshi
a621c49ed3d6 hello-world "/hello" 15 minutes ago Exited (0) 15 minutes ago bold_galileo
```
### Starting a stopped container
We can start again our stopped container with `docker container start <container-id or container-name>`:

```bash
docker container start 2c8a2962ca5c
2c8a2962ca5c
docker container start 0e539c9ccee4
0e539c9ccee4
```
Our container will restart with the same options we had specified when we launched it with the `docker run` command.

Expand All @@ -108,8 +110,8 @@ This is useful when you want to see what is written in the standard output in re

So running the `attach` command on our Ubuntu container will bring us back to our bash prompt:
```bash
docker container attach 2c8a2962ca5c
root@2c8a2962ca5c:/#
docker attach 0e539c9ccee4
root@0e539c9ccee4:/#
```

You can detach from a container and leave it running using the `CTRL-p CTRL-q` key sequence.
Expand All @@ -124,16 +126,16 @@ The command can be run in background using the option `-d` or interactively usin
Try the following command on your Ubuntu container:

```bash
docker exec -it 2c8a2962ca5c bash
root@2c8a2962ca5c:/#
docker exec -it 0e539c9ccee4 bash
root@0e539c9ccee4:/#
```
Let's look at the processes inside the container:
```bash
root@2c8a2962ca5c:/# ps aux
root@0e539c9ccee4:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4624 3844 pts/0 Ss+ 07:55 0:00 bash
root 9 0.0 0.0 4624 3788 pts/1 Ss 07:59 0:00 bash
root 17 0.0 0.0 7060 1572 pts/1 R+ 07:59 0:00 ps aux
root 1 0.0 0.0 4588 3712 pts/0 Ss+ 13:56 0:00 bash
root 9 0.1 0.0 4588 3968 pts/1 Ss 13:57 0:00 bash
root 17 0.0 0.1 7888 4096 pts/1 R+ 13:57 0:00 ps aux
```
We can see that the `exec` command started a new shell session.

Expand Down
8 changes: 0 additions & 8 deletions docs/image/automation/exercises.md

This file was deleted.

106 changes: 106 additions & 0 deletions docs/image/automation/ghaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
## Create a New Repository on GitHub

1. Go to GitHub and log in to your account.
2. Click the `+` icon in the top right corner and select `New repository`.
3. Fill out the repository name and description, then click `Create repository`.

## Add GitHub as a Remote

Add your newly created GitHub repository as a remote:

```
git remote add github https://github.com/username/repository-name.git
```

## Create a GitHub Personal Access Token:

1. Go to GitHub [Personal Access Tokens](https://github.com/settings/tokens).
2. Click `Generate new token`.
3. Add a note, select the `repo` scope for full control of private repositories, and any other necessary scopes.
4. Click `Generate token` and copy the token. This will be used as your GitHub authentication token.

## Push Your Repository to GitHub

Push your repository’s branches to GitHub:

```
git push github --all
```

## Create a GitHub Action to Build and Push Docker Images

In your GitHub repository, create the necessary directory for GitHub Actions workflows:

```
mkdir -p .github/workflows
```

Create a new file in the `.github/workflows` directory, e.g., `build-and-push.yml`:

```
name: Build and push docker image to Docker Hub
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Docker meta
id: docker_meta
uses: crazy-max/ghaction-docker-meta@v2
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/slat
tags: |
type=sha
type=semver,pattern={{raw}}
type=ref,event=branch
-
name: Build & Push image
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
```

## Add Secrets to Your GitHub Repository:

- Go to your GitHub repository and navigate to `Settings`.
- Select `Secrets and variables``Actions`.
- Click `New repository secret` and add the following secrets:
* `DOCKERHUB_USERNAME`: Your Docker Hub username.
* `DOCKERHUB_PASSWORD`: Your Docker Hub password or access token.

## Commit and Push the Workflow File

Add, commit, and push the workflow file to your GitHub repository:

```
git add .github/workflows/build-and-push.yml
git commit -m "Add GitHub Actions workflow for Docker build and push"
git push
```


Loading

0 comments on commit 19ecd5b

Please sign in to comment.