Skip to content

Latest commit

 

History

History
246 lines (174 loc) · 4.33 KB

File metadata and controls

246 lines (174 loc) · 4.33 KB

GSP055 - Introduction to Docker

Task 1. Hello world

  1. cari image hello-world di local, jika tidak ada, docker akan mencari di docker hub

    docker run hello-world
  2. command untuk melihat image yang ada di local

    docker images
  3. coba jalankan image yang sudah ada di local

    docker run hello-world
  4. Lihat container yang sedang berjalan

    docker ps
  5. Lihat semua container yang pernah berjalan

    docker ps -a

Task 2. Build

  1. Buat directory dan masuk ke directory tersebut

    mkdir test && cd test
  2. Buat DOCKERFILE

     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
  3. Buat aplikasi nodejs

    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
  4. Build image

    docker build -t node-app:0.1 .
  5. Lihat image yang sudah dibuat

    docker images

Task 3. Run

  1. Jalankan image yang sudah dibuat

    docker run -p 4000:80 --name my-app node-app:0.1
  2. Lihat aplikasi yang sudah berjalan di terminal lain

    curl http://localhost:4000
  3. Tutup terminal yang menjalankan aplikasi

  4. Stop n remove container

    docker stop my-app && docker rm my-app
  5. Jalankan lagi

    docker run -p 4000:80 --name my-app -d node-app:0.1
    docker ps
  6. Lihat log aplikasi

    docker logs [container_id]
  7. ganti file app.js ("ganti respond nya aja")

  8. Build image baru

    docker build -t node-app:0.2 .
  9. Jalankan image baru

    docker run -p 8080:80 --name my-app-2 -d node-app:0.2
    docker ps
  10. Lihat aplikasi terbaru

    curl http://localhost:8080

Task 4. Debug

  1. Lihat log

    docker logs -f [container_id]
  2. bash ke container

    docker exec -it [container_id] bash
  3. Lihat direktori

    ls
  4. Exit

    exit
  5. Lihat metadata container

    docker inspect [container_id]
  6. Lihat metadata image dengan format

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

Task 5. Publish

Create the target Docker repository

  1. Navigation Menu > CI/CD > Artifact Registry > Repositories
  2. Create Repository

Configure authentication

  1. Auth

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

Push the container to Artifact Registry

  1. Set project id environment variable

    export PROJECT_ID=$(gcloud config get-value project)
    cd ~/test
  2. Tag image

    docker tag node-app:0.2 us-central1-docker.pkg.dev/${PROJECT_ID}/node-app:0.2
  3. Push image

    docker push us-central1-docker.pkg.dev/$PROJECT_ID/my-repository/node-app:0.2

Test the image

  1. Stop container

    docker stop $(docker ps -q)
    docker rm $(docker ps -aq)
  2. Remove image

    docker rmi us-central1-docker.pkg.dev/$PROJECT_ID/my-repository/node-app:0.2
    docker rmi node:lts
    docker rmi -f $(docker images -aq) # remove remaining images
    docker images
  3. Pull and Run images from Artifact Registry

    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