Server/ETC

Docker Basics Summary

Agrafenaaa 2022. 9. 14. 10:16

DefinitionA tool for running apps in an IOSLATED ENVIRONMENT.

<Terms>

  • Container: an abstract app layer where package codes and dependencies are together. != virtual machine

* Multiple containers are able to run on the same machine and share the OS kernel.

** Structure: Infrastructure < Host Operating System < Docker < App a,b,c,d,e, ... .

  • Docker Image: a template for creating an environment/ snapshot/ it contains all for running app. 

 

<Advantages>

  1. Less running time
  2. Less resources
  3. Less memory
  4. NOT NEED full operating system
  5. Easy to test and deploy

 

<Customized Format>

docker ps --format="ID\t{{.ID}}\nNAME\t{{.Names}}\nIMAGE\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
## show in the customized format
docker ps --format=$FORMAT

 

<Pull Image>

*refer to official images on the website "https://hub.docker.com/".

Example:

docker pull nginx

 

## -d : detached mode
docker run -d nginx:latest

## stop
docker stop id || name

## start
docker start id || name

 

 

<Expose Port>

## -p : port
## single
docker run -d -p 8080:80 nginx:latest
## multiple
docker run -d -p 3000:80 -p 8080:80 nginx:latest

 

<Container Management>

## help
docker ps --help
## -a : show all containers
docker ps -a
## rm: remove a container
docker remove id || name
## remove all containers
docker rm $(docker ps -aq)
## rename
docker run --name xxx -d -p ~~

 

<Volumes>

-> Data sharing between host and container or containers.

Example)

## :ro => read only
docker run --name website -v $(pwd):/usr/share/nginx/html:ro -d -p 8080:80 nginx
## go to the running container
docker exec -it "container-name" bash
## show contents
ls -al

Sharing between containers

## in the website directory
docker run --name website-copy --volumes-from website -d -p 8081:80 nginx

 

<Dockerfile>

: a text document containing all the commands that a user could call on the command line to assemble an image.

1. Add an dockerfile in the root directory.

2. Build an image. 

docker build . --tag website:latest

successful output

 

🧐Typo#1: "docker build" requires exactly 1 argument.

=> Don't forget to put a DOT == "." after "build" and space. ex) docker build . --tag ~~

 

<Set up express.js && node.js for test>

1. Commnd the lines below to install express.js and create a index.js file with the test data. (+ do NOT forget to pull image of node)

## in the server-side project directory
npm init
npm install --save express

2. Run the server.

node index.js

3. Create a Dockerfile in the server-side project.

FROM node:latest
WORKDIR /app
ADD . .
RUN npm install
CMD node index.js

4. Build. 

<dockerignore>

docker build -t user-service-api:latest .

 

<Caching>

The more dependencies, the slower docker builds an image. Add the lines below to reduce uneffectiveness. 

before caching

after caching (data has been changed in index.js)

 

<Alpine - size reducer>

: a Linux distribution build around musl libc and BusyBox.

- pull node/nginx alpine images. (refer to the tag from docker hub)

docker pull node:alpine
docker pull nginx:alpine

- switch to alpine tags and rebuild them.

docker build -t user-service-api:latest .
docker build -t website:latest .

Each image size decreased drastically. (136 -> 23.5, 982 -> 206)

 

<Image Versioning && Tagging>

- Rename

## tag imagename:"old name" imagename:"new name"
docker tag code-website:latest code-website:1

 

<Regsitries>

: Scalable server-side app for storage and distribution of docker images.

Types: Public / Private

Most used hubs:  docker hub, quay.io, amazon ECR, etc.

- Dockerhub repository

1. Create a repository at Dockerhub

2. Rename tags of the images to be pushed.

3. Sign in

4. Push

 

<Inspection>

docker inspect "id || name"

 

<Logs>

## -f : follow
docker logs "id || name"

 

<Exec>

docker exec -it "id" /bin/sh

 

 

 

 

 

 

 

 

Ref: https://www.youtube.com/watch?v=p28piYY_wv8