Group Name: Group 8.
Section: 01.
Team Mates:
- Ahmed Ashraf Bin Anuar and 2210177
- Nurul Adlina Binti Roslan and 2219814
- Nurain Awatif Binti Ismail and 2214552
- Nuralya Medina Binti Mohammad Nizam and 2211788
- You are allowed to have 3 group members. Exception is allowed IFF (if and only if) you are allowed to have 4 group members if you are a multinational or a multigender group.
- When you complete the project, make sure to submit the repository link of your cloned project. Make sure all the files are as what you aspect in your repository.
- Answer all questions in the README.md, in your own repository. Either use the online VSCode, terminal or github to edit. Answers are expected where you see Fill answer here.
- Learn how to use markdown. https://www.w3schools.io/file/markdown-introduction/
-
First thing you need in doing this project is to have a github account. Make sure to sign up at https://www.github.com
-
The second thing you need is to fork the OS project repository in your own github account.
- Go to https://github.com/joeynor/OSProject and click fork to copy the project into your own repository
- Make sure that the new fork is now in your own repository
Questions:
- What is the link of the fork OSProject in your repository. (1 mark)
- How many files and folders are in this repository. (1 mark)
- 2 files,images file and README.md
- The next thing that we will be doing is exploring codespaces. First of all, read about codespaces https://docs.github.com/en/codespaces/overview#what-is-a-codespace
- Then go to the link https://github.com/codespaces and we shall start a new codespace.
- Click on New codespace.
- Choose your own OSProject repository to start your codespace.
- Once you have created you codespace, you will see the following. You might already be familiar with this, since it will look similar to VSCode.
- You will see the README file file. One is a preview of how it looks like on the web, and the other is the editing view in markdown language.
- Edit the README file. Make sure you have your group details correct, ie, group name, section and team members along with their matric IDs.
- Once you have finish editing, click File->Save or ctrl-s to save it.
- After saving, you will notice an M or U next to your file. You will need to commit any changes, whenever you make changes so that it is uploaded to the github repository.
- Click on the source control, hint: its on the left side panel, and it will list down the files that have been modified or updated. Click on commit. It will then ask you "Would you like to stage all your changes and commit them directly?" Just say yes, and a new tab will appear. Type a message to log what you have done, and click on the check mark.
- After that, sync the changes to the main repository.
- Make sure to commit and sync your files to the main repository, or else, your work will be lost since it is not saved into the main repository when you submit your project.
Questions:
- What is default OS used to run the virtual environment for codespaces. (1 mark)
- ideal space orbit
- What are the two options of ram, disk and vcpu configuration you can have in running codespaces . (1 mark)
- i.2-core with 8GB RAM, and 32GB storage.
- ii.4-core with 16GB RAM, and 32GB storage..
- Why must we commit and sync our current work on source control? (1 mark)
- Committing our work is to publish our work to the team while syncing is to ensure we get the latest commit by the team..
- Look at the TERMINAL tab. Explore and run commands according to the questions below.
- You can include your answers as images, or cut and paste the output here. If you are cutting and pasting your answers, wrap your answers in the codeblock clause in markdown. For example, if i run the command whoami the the output would look like the one below.
@joeynor ➜ /workspaces/OSProject (main) $ whoami
codespaceQuestions:
Look at the TERMINAL tab. Run the following commands and provide the output here.
- Run the command pwd . (1 mark)
/workspaces/OSProject- Run the command cat /etc/passwd . (1 mark)
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
systemd-timesync:x:101:101:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin
systemd-network:x:102:103:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:103:104:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
messagebus:x:104:105::/nonexistent:/usr/sbin/nologin
codespace:x:1000:1000::/home/codespace:/bin/bash
sshd:x:105:65534::/run/sshd:/usr/sbin/nologin- Run the command df . (1 mark)
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 32847680 10380372 20773208 34% /
tmpfs 65536 0 65536 0% /dev
shm 65536 0 65536 0% /dev/shm
/dev/root 30298176 24263272 6018520 81% /vscode
/dev/sda1 46127956 92 43752288 1% /tmp
/dev/loop3 32847680 10380372 20773208 34% /workspaces
4. Run the command **du** . ***(1 mark)***
4 ./.git/lfs/tmp
8 ./.git/lfs
8 ./.git/objects/6c
712 ./.git/objects/12
12 ./.git/objects/6a
8 ./.git/objects/f6
300 ./.git/objects/d1
116 ./.git/objects/03
200 ./.git/objects/34
4 ./.git/objects/pack
408 ./.git/objects/3a
112 ./.git/objects/5c
8 ./.git/objects/c0
4 ./.git/objects/info
1896 ./.git/objects
8 ./.git/refs/heads
4 ./.git/refs/tags
16 ./.git/refs
8 ./.git/logs/refs/heads
12 ./.git/logs/refs
20 ./.git/logs
68 ./.git/hooks
4 ./.git/branches
8 ./.git/info
2044 ./.git
1972 ./images
4040 .- Run the command ls . (1 mark)
README.md images- Run the command ls -asl . (1 mark)
total 36
4 drwxrwxrwx+ 4 codespace root 4096 Jun 26 16:03 .
4 drwxr-xrwx+ 5 codespace root 4096 Jun 26 16:04 ..
4 drwxrwxrwx+ 9 codespace root 4096 Jun 26 16:04 .git
20 -rw-rw-rw- 1 codespace root 19859 Jun 26 17:49 README.md
4 drwxrwxrwx+ 2 codespace root 4096 Jun 26 16:03 images- Run the command free -h . (1 mark)
total used free shared buff/cache available
Mem: 7.7Gi 1.3Gi 280Mi 63Mi 6.2Gi 6.1Gi
Swap: 0B 0B 0B- Run the command cat /proc/cpuinfo . (1 mark)
processor : 0
vendor_id : AuthenticAMD
cpu family : 25
model : 1
model name : AMD EPYC 7763 64-Core Processor
stepping : 1
microcode : 0xffffffff
cpu MHz : 2852.905
cache size : 512 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl tsc_reliable nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext invpcid_single vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves clzero xsaveerptr rdpru arat npt nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload umip vaes vpclmulqdq rdpid fsrm
bugs : sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass srso
bogomips : 4890.85
TLB size : 2560 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : AuthenticAMD
cpu family : 25
model : 1
model name : AMD EPYC 7763 64-Core Processor
stepping : 1
microcode : 0xffffffff
cpu MHz : 2833.829
cache size : 512 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl tsc_reliable nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext invpcid_single vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves clzero xsaveerptr rdpru arat npt nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload umip vaes vpclmulqdq rdpid fsrm
bugs : sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass srso
bogomips : 4890.85
TLB size : 2560 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:- Run the command top and type q to quit. (1 mark)
processor : 1
vendor_id : AuthenticAMD
cpu family : 25
model : 1
model name : AMD EPYC 7763 64-Core Processor
stepping : 1
microcode : 0xffffffff
cpu MHz : 2833.829
cache size : 512 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl tsc_reliable nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext invpcid_single vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves clzero xsaveerptr rdpru arat npt nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload umip vaes vpclmulqdq rdpid fsrm
bugs : sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass srso
top - 17:53:59 up 12 min, 0 users, load average: 0.36, 0.56, 0.48
Tasks: 17 total, 1 running, 16 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.4 us, 3.7 sy, 0.0 ni, 92.6 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7929.6 total, 272.3 free, 1338.5 used, 6318.8 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 6212.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
581 codespa+ 20 0 21.5g 330256 49920 S 1.3 4.1 0:19.98 node
560 codespa+ 20 0 1332444 106588 45184 S 0.3 1.3 0:04.14 node - Run the command *uname -a. *(1 mark)
Linux codespaces-3221e8 6.5.0-1021-azure #22~22.04.1-Ubuntu SMP Tue Apr 30 16:08:18 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux- What is the available free memory in the system. (1 mark)
- 262.3 MB
- What is the available disk space mounted on /workspace. (1 mark)
- 6202.6 mb
- Name the version and hardware architecture of the linux Virtual environment. (1 mark)
- Ubuntu SMP 22.04.1.
- What is the difference between ls vs *ls -asl. *(1 mark)
- ls: show directories/folders in the project.
- ls -asl: show access for owners, private, and public to the directories.
- What is the TLB size of the Virtual CPU. (1 mark)
- 2560 4K pages
- What is the CPU speed of the Virtual CPU. (1 mark)
- 2833.829 MHz
- What is the top running process that consumes the most CPU cycles. (1 mark)
- 581 codespa+ 20 0 21.5g 334960 49920 S 2.0 4.1 0:20.16 node
- At the terminal, run a linux instance. By typing the following command.
docker pull debian
docker run --detach -it debian
- This will run the debian container. To check if the debian container is running, type
@joeynor ➜ /workspaces/OSProject (main) $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f65be1987f84 debian "bash" 4 minutes ago Up 4 minutes romantic_jackson- Keep note of the name used by your container, this is usually given random names unless you specify your own name. Now run a bash command on the container. Make sure you use the name of your container instead of the one shown here.
docker exec -i -t romantic_jackson /bin/bash- Create a file on the container. First you must make sure you are in the bash command prompt of the container. The container is new, and does not have any software other than the debian OS. To create a new file, you will need an editor installed. In the bash shell of the container, run the package manager apt-get to install nano text editor.
root@f65be1987f84:~# apt-get update
root@f65be1987f84:~# apt-get install nano
root@f65be1987f84:~# cd /root
root@f65be1987f84:~# nano helloworld.txt-
Edit your helloworld.txt, create your messsage and save by typing ctrl-X. Once saved, explore using the container to see where the file is located. Then exit the shell, by typing exit.
-
Stop the container and run docker ps -a, and restart the container again. Is your file in the container still available?
@joeynor ➜ /workspaces/OSProject (main) $ docker stop romantic_jackson
@joeynor ➜ /workspaces/OSProject (main) $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f65be1987f84 debian "bash" 19 minutes ago Exited (137) 18 seconds ago romantic_jackson
@joeynor ➜ /workspaces/OSProject (main) $ docker restart romantic_jackson- Stop the container and delete the container. What happened to your helloworld.txt?
@joeynor ➜ /workspaces/OSProject (main) $ docker stop romantic_jackson
@joeynor ➜ /workspaces/OSProject (main) $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f65be1987f84 debian "bash" 19 minutes ago Exited (137) 18 seconds ago romantic_jackson
@joeynor ➜ /workspaces/OSProject (main) $ docker rm romantic_jacksonQuestions:
- Are files in the container persistent. Why not?. (1 mark)
- Not persistent. The files lost when the container is removed or stopped.
- Can we run two, or three instances of debian linux? . (1 mark)
- Yes
- In the previous experiment, you might have notice that containers are not persistent. To make storage persistent, you will need to mount them. At the terminal, create a new directory called myroot, and run a instance of debian linux and mount myroot to the container. Find out the exact path of my root, and mount it as the root folder in the debian container.
- Create a file in /root on the container, the files should also appear in myroot of your host VM.
@joeynor ➜ /workspaces/OSProject (main) $ mkdir myroot
@joeynor ➜ /workspaces/OSProject (main) $ cd myroot/
@joeynor ➜ /workspaces/OSProject/myroot (main) $ pwd
/workspaces/OSProject/myroot
@joeynor ➜ /workspaces/OSProject/myroot (main) $ docker run --detach -it -v /workspaces/OSProject/myroot:/root debianQuestions:
- Check the permission of the files created in myroot, what user and group is the files created in docker container on the host virtual machine? . (2 mark) total 4
- -rw-rw-rw- 1 root root 8 Jun 26 18:40 helloworld.txt
- Can you change the permission of the files to user codespace. You will need this to be able to commit and get points for this question. (2 mark)
//use sudo and chown
sudo chown -R codespace:codespace myroot
*** Yes you can change ***
- Create a directory called webpage in your host machine
- Inside the directory, create a page index.html, with any content you would like
- Then, run the apache webserver and mount the webpage directory to it. Hint:
## the -p 8080:80 flag points the host port 8080 to the container port 80
docker run --detach -v /workspaces/OSProject/webpage:/usr/local/apache2/htdocs/ -p 8080:80 httpd- If it works, codespace will trigger a port assignment and provide a URL for you to access your webpage like the one below.
-
You can also see the Port in the PORTS tab, next to the terminal tab.
-
You can then access your website by adding an index.html towards the end of your url link, like the one below.
Questions:
- What is the permission of folder /usr/local/apache/htdocs and what user and group owns the folder? . (2 mark)
Permissions: drwxrwxrwx+
d: This indicates it's a directory.
rwxrwxrwx: This indicates read, write, and execute permissions for the owner, group, and others.
+: This indicates that there are additional access control list (ACL) entries.
Owner: 1000
Group: 1000- What port is the apache web server running. (1 mark)
- Port '80'.
- What port is open for http protocol on the host machine? (1 mark)
- Port '8080'.
- In docker, you can create your own private networks where you can run multiple services, in this part, we will create two networks, one called bluenet and the other is rednet
- Run the docker create network to create you networks like the ones below
## STEP 1:
## Create Networks ##
docker network create bluenet
docker network create rednet`
## STEP 2: (automatically running)
## Create (1) Container in background called "c1" running busybox image ##
docker run -itd --net bluenet --name c1 busybox sh
docker run -itd --net rednet --name c2 busybox shQuestions:
- Describe what is busybox and what is command switch --name is for? . (2 mark)
- BusyBox is a software suite that provides several Unix utilities in a single executable file. It is often used in embedded systems and Docker containers because it offers a lightweight set of tools that are efficient and functional. --name switch: The --name switch in Docker is used to assign a name to a container. This makes it easier to reference the container by name rather than by its container ID, simplifying management and identification of the container
- Explore the network using the command
docker network ls, show the output of your terminal. (1 mark)
NETWORK ID NAME DRIVER SCOPE
7c9a2668b993 bluenet bridge local
afe6dade3149 bridge bridge local
adaf77f09c85 host host local
acd86aee4060 none null local
413579fdbb0f rednet bridge local- Using
docker inspect c1anddocker inspect c2inscpect the two network. What is the gateway of bluenet and rednet.? (1 mark)
- bluenet : 172.18.0.1
- rednet : 172.19.0.1
- What is the network address for the running container c1 and c2? (1 mark)
- IP address c1: 172.18.0.2
- IP address c2: 172.19.0.2
- Using the command
docker exec c1 ping c2, which basically tries to do a ping from container c1 to c2. Are you able to ping? Show your output . (1 mark)
ping: bad address 'c2'No , Since c1 and c2 are on different networks (bluenet and rednet respectively), they cannot communicate directly with each other. Therefore, the ping command fails with a "bad address" error.
- Let's try this again by creating a network to bridge the two containers in the two subnetworks
docker network create bridgenet
docker network connect bridgenet c1
docker network connect bridgenet c2
docker exec c1 ping c2
Questions:
- Are you able to ping? Show your output . (1 mark)
PING c2 (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.130 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.082 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.105 ms
64 bytes from 172.20.0.3: seq=3 ttl=64 time=0.071 ms
64 bytes from 172.20.0.3: seq=4 ttl=64 time=0.096 ms
64 bytes from 172.20.0.3: seq=5 ttl=64 time=0.079 ms
64 bytes from 172.20.0.3: seq=6 ttl=64 time=0.076 ms
64 bytes from 172.20.0.3: seq=7 ttl=64 time=0.069 ms
64 bytes from 172.20.0.3: seq=8 ttl=64 time=0.081 ms
64 bytes from 172.20.0.3: seq=9 ttl=64 time=0.068 ms
64 bytes from 172.20.0.3: seq=10 ttl=64 time=0.064 ms
64 bytes from 172.20.0.3: seq=11 ttl=64 time=0.073 ms
64 bytes from 172.20.0.3: seq=12 ttl=64 time=0.078 ms
64 bytes from 172.20.0.3: seq=13 ttl=64 time=0.081 ms
64 bytes from 172.20.0.3: seq=14 ttl=64 time=0.106 ms
64 bytes from 172.20.0.3: seq=15 ttl=64 time=0.079 ms
64 bytes from 172.20.0.3: seq=16 ttl=64 time=0.103 ms
64 bytes from 172.20.0.3: seq=17 ttl=64 time=0.068 ms
64 bytes from 172.20.0.3: seq=18 ttl=64 time=0.078 ms
64 bytes from 172.20.0.3: seq=19 ttl=64 time=0.081 ms
64 bytes from 172.20.0.3: seq=20 ttl=64 time=0.077 ms
64 bytes from 172.20.0.3: seq=21 ttl=64 time=0.084 ms
64 bytes from 172.20.0.3: seq=22 ttl=64 time=0.115 ms
64 bytes from 172.20.0.3: seq=23 ttl=64 time=0.080 ms
64 bytes from 172.20.0.3: seq=24 ttl=64 time=0.090 ms
64 bytes from 172.20.0.3: seq=25 ttl=64 time=0.084 ms
64 bytes from 172.20.0.3: seq=26 ttl=64 time=0.074 ms
64 bytes from 172.20.0.3: seq=27 ttl=64 time=0.068 ms
64 bytes from 172.20.0.3: seq=28 ttl=64 time=0.081 ms
64 bytes from 172.20.0.3: seq=29 ttl=64 time=0.076 ms
64 bytes from 172.20.0.3: seq=30 ttl=64 time=0.080 ms
64 bytes from 172.20.0.3: seq=31 ttl=64 time=0.069 ms
64 bytes from 172.20.0.3: seq=32 ttl=64 time=0.113 ms
64 bytes from 172.20.0.3: seq=33 ttl=64 time=0.071 ms
64 bytes from 172.20.0.3: seq=34 ttl=64 time=0.084 ms
64 bytes from 172.20.0.3: seq=35 ttl=64 time=0.068 ms
64 bytes from 172.20.0.3: seq=36 ttl=64 time=0.068 ms
64 bytes from 172.20.0.3: seq=37 ttl=64 time=0.086 ms
64 bytes from 172.20.0.3: seq=38 ttl=64 time=0.073 ms- What is different from the previous ping in the section above? (1 mark)
- I am able to ping c2 from c1 successfully. Previous Ping: The previous ping attempt failed with the error ping: bad address 'c2' because c1 and c2 were on separate, isolated networks (bluenet and rednet) and couldn't communicate with each other directly.
Current Ping: After connecting both containers to the bridgenet network, they are now part of a common network, allowing them to communicate with each other. As a result, the ping from c1 to c2 is successful.
This guide will help you set up a simple Node.js website that retrieves a random row from a MySQL database. Both the MySQL server and the Node.js server will run in separate Docker containers on two separate networks. Your job is to make it work by making the two containers in two separate network bridged together.
Create a Docker network to for the two containers. For mysql, call it mysqlnet for nodejs call it nodejsnet .
Run a MySQL container on the created network.
docker run --name mysql-container --network mysqlnet -e MYSQL_ROOT_PASSWORD=rootpassword -e MYSQL_DATABASE=mydatabase -e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypassword -d mysql:latest-
Create a directory for your Node.js application and initialize it.
mkdir nodejs-app cd nodejs-app npm init -y npm install express mysql -
Create a file named
index.jswith the following content:const express = require('express'); const mysql = require('mysql'); const app = express(); const port = 3000; // Create a MySQL connection const connection = mysql.createConnection({ host: 'mysql-container', user: 'myuser', password: 'mypassword', database: 'mydatabase' }); // Connect to MySQL connection.connect((err) => { if (err) { console.error('Error connecting to MySQL:', err); return; } console.log('Connected to MySQL'); }); // Define a route to get a random row app.get('/random', (req, res) => { const query = 'SELECT * FROM mytable ORDER BY RAND() LIMIT 1'; connection.query(query, (err, results) => { if (err) { console.error('Error executing query:', err); res.status(500).send('Server Error'); return; } res.json(results[0]); }); }); // Start the server app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });
-
Create a Dockerfile for the Node.js application:
# Use the official Node.js image FROM node:14 # Create and change to the app directory WORKDIR /usr/src/app # Copy application dependency manifests to the container image COPY package*.json ./ # Install production dependencies RUN npm install # Copy local code to the container image COPY . . # Run the web service on container startup CMD [ "node", "index.js" ]
-
Build the Docker image for the Node.js application.
docker build -t nodejs-app . -
Run the Node.js container on the same network as the MySQL container.
docker run --name nodejs-container --network nodejsnet -p 3000:3000 -d nodejs-app
You can now test the setup by accessing the Node.js application in your browser or using a tool like curl:
curl http://localhost:3000/randomMake sure you have created the mytable table and populated it with some data in your MySQL database for the above steps to work correctly.
You can use the following SQL commands to create and populate the table (run these commands in the MySQL container):
CREATE TABLE mytable (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
value VARCHAR(255) NOT NULL
);
INSERT INTO mytable (name, value) VALUES ('example1', 'value1'), ('example2', 'value2'), ('example3', 'value3');You have now set up a Node.js application in a Docker container on nodejsnet netowrk and a MySQL database in another Docker container on mysqlnet network. Now bridge the two network together.
Questions:
- What is the output of step 5 above, explain the error? (1 mark)
- Cannot GET/
- This error occurs because the Node.js application cannot connect to the MySQL database container, as they are on separate Docker networks (mysqlnet and nodejsnet) and have not been bridged.
- Show the instruction needed to make this work. (1 mark)
docker network create bridgenet
docker network connect bridgenet mysql-container
docker network connect bridgenet nodejs-container- Make sure to commit all changes on your source control, and make sure your source control is sync to the repository.
- Check your repository link, to see if all the files and answers are included in the repository.
- Submit through italeem, by providing the link to your repository.
- Due by AS STATED IN ITALEEM SYSTEM





