Practical exercises and tasks related to Docker performed during CSCE certification course.
Task
Test Docker installation by running the "hello-world" container.
$ docker run hello-worldDisplay all running containers.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c38cde9e19f7 hello-world "/hello" 51 seconds ago Exited (0) 50 seconds ago elated_yalowDisplay all existing images.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 1b44b5a3e06a 6 weeks ago 10.1kBClean up
docker stop
docker rm c38cde9e19f7Task
Dockerize a simple Node.js web application (available in examples/simple) and run it in a docker container.
Solution
Build image and run container.
$ cd examples/simple
$ docker build -t node-app .
# Verify image creation
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
node-app latest 353cea72f89c 11 seconds ago 142MBRun Container with the created image.
$ docker run -p 3000:3000 node-app
Server running on http://localhost:3000Verify the app.
$ curl http://localhost:3000
Hello World!Clean up
# Get container ID
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d522594cf85 node-app "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3000->3000/tcp, [::]:3000->3000/tcp upbeat_bhabha
$ docker stop 0d522594cf85
$ docker rm 0d522594cf85
$ docker rmi node-appTask
Run a multi-container application with a web server (Nginx) and a MariaDB database using Docker Compose. Manfiest file is available in examples/multi-container directory.
Solution
Deploy the application.
$ cd examples/multi-container
$ docker compose up -d
[+] Running 4/4
✔ Network docker-compose-demo_default Created 0.0s
✔ Volume "docker-compose-demo_db_data" Created 0.0s
✔ Container docker-compose-demo-db-1 Started 0.4s
✔ Container docker-compose-demo-web-1 StartedCheck services.
$ docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
docker-compose-demo-db-1 mariadb:latest "docker-entrypoint.s…" db 48 seconds ago Up 47 seconds 3306/tcp
docker-compose-demo-web-1 nginx:latest "/docker-entrypoint.…" web 48 seconds ago Up 47 seconds 0.0.0.0:80->80/tcp, [::]:80->80/tcpView container logs.
$ docker compose logs
# Output omittedTest the DB.
$ docker exec -it $(docker compose ps -q db) mariadb -uuser -ppassword mydb
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 12.0.2-MariaDB-ubu2404 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [mydb]> exit
ByeTest web application.
$ curl localhost:80
Hello World!Clean up
$ docker compose down -v
[+] Running 4/4
✔ Container docker-compose-demo-web-1 Removed 0.3s
✔ Container docker-compose-demo-db-1 Removed 0.5s
✔ Network docker-compose-demo_default Removed 0.3s
✔ Volume docker-compose-demo_db_data Removed 0.0s