Thursday, April 4, 2024
Docker Dockerfile GCP Juara GCP

Introduction to Docker

introduction

“Introduction to Docker”

Daftar Isi

Pengantar

Docker adalah platform terbuka untuk mengembangkan, mengirimkan, dan menjalankan aplikasi. Dengan Docker, Anda dapat memisahkan aplikasi dari infrastruktur Anda dan memperlakukan infrastruktur Anda seperti aplikasi yang dikelola. Docker membantu Anda mengirimkan kode lebih cepat, menguji lebih cepat, menerapkan lebih cepat, dan mempersingkat siklus antara menulis kode dan menjalankan kode.

Praktikum

Task 1. Hello world

docker run hello-world
  • Run Image
docker images
docker run hello-world
docker ps
docker ps -a

Task 2. Build

  • Buat Folder baru
mkdir test && cd test
cat > Dockerfile <<EOF
# Use an official Node runtime as the parent image
FROM node:lts
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Make the container's port 80 available to the outside world
EXPOSE 80
# Run app.js using node when the container launches
CMD ["node", "app.js"]
EOF
  • Buat node app
cat > app.js <<EOF
const http = require('http');
const hostname = '0.0.0.0';
const port = 80;
const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
});
server.listen(port, hostname, () => {
    console.log('Server running at http://%s:%s/', hostname, port);
});
process.on('SIGINT', function() {
    console.log('Caught interrupt signal and will exit');
    process.exit();
});
EOF
  • Build app
docker build -t node-app:0.1 .
  • Cek list image
docker images

Task 3. Run

  • Run image
docker run -p 4000:80 --name my-app node-app:0.1
  • test app
curl http://localhost:4000
docker stop my-app && docker rm my-app
  • Run Container in the background
docker run -p 4000:80 --name my-app -d node-app:0.1
docker ps
docker logs [container_id]
  • Modify app
cd test

-- edit app.js

....
const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Welcome to Cloud\n');
});
....
  • build app
docker build -t node-app:0.2 .
  • run container
docker run -p 8080:80 --name my-app-2 -d node-app:0.2
docker ps
  • test container
curl http://localhost:8080
curl http://localhost:4000

Task 4. Debug

docker logs -f [container_id]

docker exec -it [container_id] bash

docker inspect [container_id]

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_id]

Task 5. Publish

Create the target Docker repository

  • Klik Navigation Menu, under CI/CD navigate to Artifact Registry > Repositories
  • Click Create Repository.
  • Specify my-repository as the repository name
  • Choose Docker as the format
  • Under Location Type, select Region and then choose the location us-central1 (Iowa)
  • Click Create

Configure authentication

gcloud auth configure-docker us-central1-docker.pkg.dev

Push the container to Artifact Registry

  • cek project
gcloud config list project

docker build -t us-central1-docker.pkg.dev/[project-id]/my-repository/node-app:0.2 .
  • Cek list image
docker images
  • push to repo
docker push us-central1-docker.pkg.dev/[project-id]/my-repository/node-app:0.2
  • Klik Navigation Menu, under CI/CD navigate to Artifact Registry > Repositories.
  • Klik my-repository

Test the image

  • stop and remove all container
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
  • remove all docker image
docker rmi node-app:0.2 gcr.io/[project-id]/node-app node-app:0.1
docker rmi node:lts
docker rmi $(docker images -aq) # remove remaining images
docker images
  • Pull image and run it
docker pull us-central1-docker.pkg.dev/[project-id]/my-repository/node-app:0.2
docker run -p 4000:80 -d us-central1-docker.pkg.dev/[project-id]/my-repository/node-app:0.2
curl http://localhost:4000

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Introduction to Docker. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

(Visited 107 times, 1 visits today)

Similar Posts