Site icon Learning & Doing

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
docker images
docker run hello-world
docker ps
docker ps -a

Task 2. Build

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
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
docker build -t node-app:0.1 .
docker images

Task 3. Run

docker run -p 4000:80 --name my-app node-app:0.1
curl http://localhost:4000
docker stop my-app && docker rm my-app
docker run -p 4000:80 --name my-app -d node-app:0.1
docker ps
docker logs [container_id]
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');
});
....
docker build -t node-app:0.2 .
docker run -p 8080:80 --name my-app-2 -d node-app:0.2
docker ps
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

Configure authentication

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

Push the container to Artifact Registry

gcloud config list project

docker build -t us-central1-docker.pkg.dev/[project-id]/my-repository/node-app:0.2 .
docker images
docker push us-central1-docker.pkg.dev/[project-id]/my-repository/node-app:0.2

Test the image

docker stop $(docker ps -q)
docker rm $(docker ps -aq)
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
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.

Exit mobile version