Docker
October 28, 2020

Part 2/3 Docker Tutorial for Beginners | Full Course [2020]

Building Images

1. Dockerfile

2. Creating Dockerfile

FROM nginx:latest
ADD . /usr/share/nginx/html

Create currenc directory Dockerfile

docker build --tag website:latest .

3. Building Images

Create currenc directory Dockerfile

docker build --tag website:latest .

Stop this container (remove)

docker rm -f contaner_NAME

Ngnix docker hub:

https://hub.docker.com/_/nginx

Project: Build an Image for an API

1. Nodejs and Express

Open it:

https://expressjs.com/en/starter/installing.html

Install express

npm install --save express

https://expressjs.com/en/starter/hello-world.html

We see: 'Hello world' in Express

Dockerfile for node app

build dockerfile with name repository:

docker build --tag user-service:latest .

Run this docker image

docker run --name user-api -d -p 3000:3000 user-service:latest

.Dockerignore

docker ignore file:

node_modules
Dockerfile
.git
.idea

Run it!

docker build -t user-service:latest .

Catching and layers

Again we create image

docker build -t user-service:latest .

We need to remove old container

docker rm -f user-api

We can improve this docker image

Exapmle caching

D:\other\docker\user-service>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a65a746c0922 d55b7555b4a8 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:3000->3000/tcp user-api
D:\other\docker\user-service>docker rm -f a65a746c0922
a65a746c0922
D:\other\docker\user-service>docker rm -f user-api
Error: No such container: user-api
D:\other\docker\user-service>docker run --name user-api -d -p 3000:3000 user-service:latest
260ac709dcb40777e3bc17d09a3bac4d692bb419dd67df2bc92c5eb2bf271a9b
D:\other\docker\user-service>docker rm -f user-api
user-api
D:\other\docker\user-service>docker build -t user-service:latest .

Reducing Image Size

1. Alpine

docker image ls

https://alpinelinux.org/

It's for simple and small container)

2. Pulling alpine docker images

We create new image for node very short

docker pull node:lts-alpine

Nginx Alphine

docker pull nginx:alpine

PS D:\other\docker> docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
user-service        latest              c0f914ef8443        3 days ago          979MB
<none>              <none>              f3062929d818        3 days ago          979MB
<none>              <none>              cffa1290e568        3 days ago          979MB
<none>              <none>              8766e9621cbf        3 days ago          979MB
<none>              <none>              d55b7555b4a8        3 days ago          979MB
<none>              <none>              ad8370b041d8        3 days ago          945MB
website             latest              f3cd1f99f4b5        3 days ago          140MB
node                lts-alpine          9db54a688554        4 days ago          117MB
nginx               alpine              4efb29ff172a        10 days ago         21.8MB
nginx               latest              f35646e83998        2 weeks ago         133MB

3. Switching to Alpine

Remove image also none version so:

docker image rm IMAGE_ID

Pulling alpine

docker pull node:alpine docker pull nginx:alpine

Building our app

docker build -t user_service:latest . docker build -t website:latest .


The end!