Lab1
μΈμ
λͺ©ν
Docker Container build, run, debug
Docker Hubλ‘λΆν° Docker images pull νκΈ°
Google Container Registry μ Docker image push νκΈ°
Google Cloud
Cloud Shell μ development tools μ λ‘λλλ κ°μ λ¨Έμ μ΄λ€.

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Auth list νμΈ

docker run hello-world
λ컀 λ°λͺ¬μ hello-world μ΄λ―Έμ§λ₯Ό λ‘컬μμ μ°Ύμ§ λͺ»νλ©΄ Docker Hubμμ μ΄λ―Έμ§λ₯Ό pullνμ¬ μ»¨ν μ΄λλ₯Ό μμ±νκ³ μ€ννλ€.
μ΄λ―Έμ§λ₯Ό ν ν ν λ€μ ν΄λΉ λͺ λ Ήμ΄λ₯Ό μ€ννλ©΄ μ΄μ docker daemonμ local registryμμ μ΄λ―Έμ§λ₯Ό μ°Ύκ³ , κ·Έ μ΄λ―Έμ§λ‘λΆν° 컨ν μ΄λλ₯Ό μ€ννλ€.
Build
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
The initial line specifies the base parent image, which in this case is the official Docker image for node version long term support (lts).
In the second, you set the working (current) directory of the container.
In the third, you add the current directory's contents (indicated by the "." ) into the container.
Then expose the container's port so it can accept connections on that port and finally run the node command to start the application.
node application
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
μ νμΌ μμ± ν imageλ₯Ό buildνλ€.
docker build -t node-app:0.1 .
-t λͺ λ Ήμ΄λ name: tag λ‘ νκ·Έλ₯Ό μ§μ νκΈ° μν λͺ λ Ήμ΄.
λ°λΌμ μ΄λ―Έμ§μ μ΄λ¦μ node-app μ΄κ³ , tagλ 0.1μ΄ λλ€.
νκ·Έλ₯Ό μ§μ νμ§ μμΌλ©΄ latest λ‘ μ μ₯λλ€. μ΄λ κ² λλ©΄ μλ‘μ΄ μ΄λ―Έμ§κ° μκ²Όμ λ ν·κ°λ¦¬κΈ° λλ¬Έμ νκ·Έλ₯Ό λ°λμ μ μ₯νλ κ²μ΄ μ€μνλ€.

μ΄λ―Έμ§ λΉλ ν λ컀 μ΄λ―Έμ§ λͺ©λ‘μ νμΈνλ€.
Run
μμμ λΉλν μ΄λ―Έμ§ κΈ°λ°μ 컨ν μ΄λλ₯Ό μ€ννκΈ° μν΄μλ λ€μ λͺ λ Ήμ΄λ₯Ό μ€ννλ€.
docker run -p 4000:80 --name my-app node-app:0.1
βname μ 컨ν μ΄λ μ΄λ¦μ μ§μ ν μ μλλ‘ νλ λͺ λ Ήμ΄μ΄λ€.
-p λ λμ»€κ° νΈμ€νΈμ ν¬νΈ 4000λ²μ 컨ν μ΄λμ 80λ² ν¬νΈλ‘ λ§€ννλλ‘νλ€.
λ°λΌμ νΈμ€νΈλ http://localhost:4000 μμ μλ²λ₯Ό νμΈν μ μλ€.

λ컀 컨ν μ΄λλ₯Ό λμ΄ ν λ€λ₯Έ ν°λ―Έλμμ μλ λͺ λ Ήμ΄λ₯Ό νμΈν΄λ³Έλ€.
curl <http://localhost:4000>

λ€μκ³Ό κ°μ λͺ λ Ήμ΄λ₯Ό νμΈν μ μλ€.
λ§μ½ 컨ν μ΄λκ° λ°±κ·ΈλΌμ΄λμμ λ°λͺ¬μΌλ‘ λμκ°λμ§ νμΈνκΈ° μν΄μλ -d flagλ₯Ό λΆμΈλ€.
docker run -p 4000:80 --name my-app -d node-app:0.1
docker ps
λ컀 λ΄λ Έλ€κ° backgroundμμ run νλλ‘ νλ λͺ λ Ήμ΄

컨ν μ΄λ μμ΄λλ₯Ό ν΅ν΄ λ‘κ·Έλ₯Ό νμΈνλ€.
docker build -t node-app:0.2 .
app.js νμΌ λ³κ²½ ν λ€μ μ΄λ―Έμ§λ₯Ό λΉλνκ³ μλ‘μ΄ νκ·Έλ₯Ό λΆμΈλ€.

step2 μμλ μ΄λ―Έ μ‘΄μ¬νλ cache layerλ₯Ό μ¬μ©νλ€.
Step3 λΆν°λ app.jsμμ λ³κ²½μ¬νμ΄ μμκΈ° λλ¬Έμ layerλ€λ μμ λλ€.
docker run -p 8080:80 --name my-app-2 -d node-app:0.2
docker ps
μλ‘μ΄ μ΄λ―Έμ§λ₯Ό μ΄μ©ν 컨ν μ΄λλ₯Ό μ€ννλ€.
μ΄λ―Έ νΈμ€νΈμ 4000λ² ν¬νΈλ₯Ό μ¬μ©μ€μ΄κΈ° λλ¬Έμ λ€λ₯Έ ν¬νΈλ²νΈλ₯Ό μ¬μ©νλ€.

Debug
컨ν μ΄λ μμ΄λλ₯Ό ν΅ν΄ λ컀 λ‘κ·Έλ₯Ό νμΈνλ€.
docker logs -f [container_id]
docker exec -it [container_id] bash
컨ν μ΄λμμμ interactive Bash session μ€ν
-it flagλ pseudo-ttyλ₯Ό ν λΉν¨μΌλ‘μ¨ μ»¨ν μ΄λμ μνΈμμ©ν μ μλλ‘ νλ€.
bashλ WORKDIR μΈ /app μμ μ€νλλ€λ κ²μ κΈ°μ΅νμ. (Dockerfile μμ μμ±ν¨)

docker inspect [container_id]
Publish
μ΄μ μ΄λ―Έμ§λ₯Ό Google Container Registry (gcr)μ νΈμν΄λ³΄μ!
κ·Έ ν 컨ν μ΄λμ μ΄λ―Έμ§λ€μ μμ ν κ²μ΄κ³ , κ·Έ λ€μ ννκ³ μ»¨ν μ΄λλ₯Ό μ€νν κ²μ΄λ€.
gcrμ μ΄λ―Έμ§λ₯Ό νΈμνκΈ° μν΄μλ μ΄λ―Έμ§ μ΄λ¦μ registry nameμΌλ‘ νκ·Έν΄μΌνλ€.
νμμ [hostname]/[project-id]/[image]:[tag] μ΄λ€.
For gcr:
[hostname]= gcr.io
[project-id]= your project's ID
[image]= your image name
[tag]= any string tag of your choice. If unspecified, it defaults to "latest".
project IDλ μλ λͺ λ Ήμ΄λ₯Ό μ€νν΄ νμΈνλ€.
gcloud config list project
node-app:0.2 λ₯Ό νκΉ ν΄λ³΄μ.
docker tag node-app:0.2 gcr.io/[project-id]/node-app:0.2

μ΄μ μ΄ μ΄λ―Έμ§λ₯Ό gcrμ νΈμν΄λ³΄μ.
docker push gcr.io/[project-id]/node-app:0.2
μ΄μ Google Cloudμμ μ΄λ―Έμ§κ° μ νΈμλμλμ§ νμΈνλ€.

μ΄λ―Έμ§ ν
μ€νΈ
λ€μν λ°©λ²μΌλ‘ μλ ν μ μμ§λ§ κ°λ¨ν μ‘΄μ¬νλ κΈ°μ‘΄μ 컨ν μ΄λλ€μ μμ νλ€.
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
node imageλ₯Ό μ§μ°κΈ° μ μ node:lts μ child images λ€λ μμ ν΄μΌνλ€.
λ€μ λͺ λ Ήμ΄λ₯Ό μ¬μ©νλ€.
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 νκ³ μ€νν΄λ³΄μ
docker pull gcr.io/[project-id]/node-app:0.2
docker run -p 4000:80 -d gcr.io/[project-id]/node-app:0.2
curl <http://localhost:4000>
Last updated