Docker

docker, linuxkit, virtualbox, instance, volume, snapshots, EC2, tty

Naranjito 2021. 2. 9. 17:37
  • docker -v

Version of docker.

$ docker -v
Docker version 19.03.12, build 48a66213fe

 

  • linuxkit

A toolkit for building custom minimal, immutable Linux distribution. It used for creating virtual machine on mac. It loads minimum linux kernel on it therefore mac kernel follows linuxkit kernel.

 

  • virtualbox

It is a gerneral purpose virtualization tool that allows users and administrators to easily run multiple guest operating system on a single host.

 

  • instance

It is a virtual server instance in a cloud computing environments. It is build, hosted and delivered using a cloud computing platform, and can be accessed remotely. A cloud computiing platform is a shared pool of compute resources and services.

 

  • volume

Data storage which can be connected with instance permanently. It is easy to flexible tackle when traffic increase rather than using normal storage.

 

  • snapshots

It can be restored in the event of something inacting production dataset. The copy can then be made available for recovery, but also to other systems for testing and development.

 

  • EC2

Amazon Elastic Compute Cloud, allows users to rent virtual computers on which to run their own computer applications.

 

  • docker run -i -t  ContainerName:ContainerVersion

Generate, execute the container and come inside the container.(pull+create+start+attach)

-i -t : For interactive processes(like a shell), you must use -i -t together in order to allocate a tty for the container process. -i -t is often written -it

$ docker run -i -t ubuntu:14.04 //ubuntu is name of image, 14.04 is version of image
Unable to find image 'ubuntu:14.04' locally //therefore, it download from docker hurb
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pull complete 
95201152d9ff: Pull complete 
5f63a3b65493: Pull complete 
Digest: sha256:63fce984528cec8714c365919882f8fb64c8a3edf23fdfa0b218a2756125456f
Status: Downloaded newer image for ubuntu:14.04
root@de8df71fc2d9:/# 

I am in the container now by change the user and host name. Inside the container, basic user is root, host name is random 16bit hash value.

 

tty : Teletype which connects a user's terminal with the stdin and stdout stream, allowing you to type text and send it away, commonly through a shell such as bash. In the case of docker, you'll often use -t and -i together when you run processes in interactive mode, such as when starting a bash shell. it commands of terminal basically prints the file name of the terminal connected to standard input. 

 

- turn back to host

1. exit : terminate bash shell, it stops the container

2. ctrl+d : exit from container and stop the container simultaneously

3. ctrl+p,q : exit only from container shell, it doesnt stop the container

[root@8c67b8e30b2d /]# read escape sequence
$ 

 

  • docker pull centos:7

Pull an image or a repository, give me centos7.

$ docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7

 

  • docker images

List images.

$ docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
kafka-docker_kafka       latest              287b0946b02c        7 weeks ago         435MB
centos                   7                   8652b9f0cb4c        2 months ago        204MB
ubuntu                   14.04               df043b4f0cf1        4 months ago        197MB
openjdk                  8u212-jre-alpine    f7a292bbb70c        21 months ago       84.9MB
wurstmeister/zookeeper   latest              3f43f72cb283        2 years ago         510MB

 

  • docker create -i -t --name ImageNameWhatIWant RepositoryImagaName:ImageVersion

Create the docker image

 

The random 16bit hash is unique container ID. Basically using the head of 12 digits.

It only creates container but doesnt go inside the container.

$ docker create -i -t --name mycentos centos:7
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
8c67b8e30b2d1b6c621e12f26fed9b613b105c28d1f38f29d4770657e3b2ef73

 

  • docker start mycentos

Execute the container named mycentos.

 

  • docker attach mycentos

Go inside the container.

$ docker start mycentos
mycentos
$ docker attach mycentos
[root@8c67b8e30b2d /]# 

 

  • docker ps

ps : Process Status, it is used to describe running container only. It doesnt describe any exit container, but ctrl +p,q.

 

  • docker ps -a

It shows all the container regardless exited or running.

docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
8c67b8e30b2d   centos:7   "/bin/bash"   21 minutes ago   Up 19 minutes             mycentos

-a : All

bin : Binary, It is just a directory where a user of an operating system can expect to find applications.

bash : Bourne Again Shell, it is a command language interpreter

 

  • docker rename oldname newname
docker rename my_container joohyun_container

 

  • docker ps --format "table {{.ID}}\t{{.Status}}\t{{.Image}}"

It returns neatly.

docker ps --format "table {{.ID}}\t{{.Status}}\t{{.Image}}"
CONTAINER ID   STATUS          IMAGE
8c67b8e30b2d   Up 36 minutes   centos:7

 

  • docker rm mycentos

Cannot remove a running container so in order to remove, stop the container before attempting removal or force remove.

docker rm mycentos
Error response from daemon: You cannot remove a running container 8c67b8e30b2d1b6c621e12f26fed9b613b105c28d1f38f29d4770657e3b2ef73. Stop the container before attempting removal or force remove

$ docker ps -a
CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS                     PORTS     NAMES
0339283abc84   ubuntu:14.04   "echo hello world!"   4 minutes ago    Exited (0) 4 minutes ago             magical_villani
8c67b8e30b2d   centos:7       "/bin/bash"           41 minutes ago   Up 38 minutes                        mycentos

$ docker stop mycentos
mycentos

$ docker rm -f mycentos
mycentos

 

  • docker stop $(docker ps -a -q)
  • docker rm $(docker ps -a -q)

Stop all the containers and remove them.

$ docker stop $(docker ps -a -q)
6d1127c3dd4f
d7d99ad8731e
096fc78b5fc5
5c23a92beb15
9de047d370ce
96e35048ff3b
37a131da55bd

docker rm $(docker ps -a -q)
6d1127c3dd4f
d7d99ad8731e
096fc78b5fc5
5c23a92beb15
9de047d370ce
96e35048ff3b
37a131da55bd

docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

  • docker container prune
  • docker volume prune

Remove all stopped containers.

Remove all not used volumes.

docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
0339283abc8442f675f8b83b99e36181535dc82208a359aa0fb86f5e44aa0a8f

Total reclaimed space: 0B

 

  • docker image prune -f

Remove all <none> images.

 

  • docker ps -a -q
docker ps -a -q
37a131da55bd

-a : All

-q : Quiet, only display container IDs

 

  • ifconfig

Interface configuration, , it is used to view and change the configuration of the network interfaces on your system.

 

eth : Ethernet, Docker interface, it can be connected external, it is a way of connecting computers and other network devices in a physical space. This is often referred to as a local area network or LAN.

 

inet addr : NAT IP, basically docker allocates 172.17.0.x to container in order, it can be changeable whenever restart the container. It is inner IP, needs to be connect to external. It is done by creating veth interface. 

 

lo : Local Host

root@37a131da55bd:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:836 (836.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

 

 

eth0 container interface is connected to veth host interface.

 

veth : Virtual Ethernet, a local Ethernet tunnel. Devices are created in pairs. It is binding with docker0(bridge) in order to communicate with external. Packets transmitted on one device in the pair are immediately received on the other device. When either device is down, the link state of the pair is down.

NAT : Network Address Translation, is a method of remapping an IP address space into another by modifying network address informaton in the IP header of packets while they are in transit across a traffic routing device. (jwprogramming.tistory.com/30) The reason why using NAT is normally in order to access to various private networks through one public ip.

 

  • docker run -i -t --name mywebserver -p 80:80 ubuntu:14.04

-p : Port

Host port:Container port : Let host port to connects to container port.

Host IP:Host port:Container port : Let specific host ip to connects to container port.

In case -p, meaning Container port

docker run -i -t --name mywebserver -p 80:80 ubuntu:14.04
root@096fc78b5fc5:/# 

docker run -i -t -p 3306:3306 -p 192.168.0.100:7777:80 ubuntu:14.04

 

  • apt-get update
root@096fc78b5fc5:/# apt-get update
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]    

...

root@096fc78b5fc5:/# apt-get install apache2 -y
Reading package lists... Done

...

root@096fc78b5fc5:/# service apache2 start
 * Starting web server apache2  

apt-get : Advanced Packaging Tool, which helps in handling packages in Linux. Its main task is to retrieve the information and packages from the authenticated sources for installation, upgrade and removal of packages along with thier dependencies.

 

  • docker run -d --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress mysql:5.7

Create database container using with mysql image.

 

-d : Detached, opposite to -i -t, in other words, it estabilished running the application on the foreground, unlike interactive mode -i -t. It executes the container without input and output. It doesnt receive user's input

 

  • docker run -d -e WORDPRESS_DB_PASSWORD=password --name wordpress --link wordpressdb:mysql -p 80 wordpress

Create web server container using with prepared wordpress image. One of host port connects to container port 80.

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                   NAMES
4086b363b290   wordpress      "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:55000->80/tcp   wordpress

mysql account for one terminal, wordpress also account for one terminal, therefore it executes -d

 

-e : Environment, establish inner environment

 

--link : Container A can be connected to Container B

 

password : establish password

 

tcp : Transimission Control Protocol, the protocol using with IP in order to send the data as message on the Internet. Basically tcp uses with ip. For example, ip handles deliver the data, tcp manages and traces the packet.

 

0.0.0.0 : Binding to all the applicable network interface.

 

  • docker exec -i -t wordpressdb /bin/bash

exec : It is available using inner shell when the container created -d. It returns result inside the container after executing -i -t.

docker exec -i -t wordpressdb /bin/bash
root@c7b7da831d2c:/# echo $MYSQL_ROOT_PASSWORD
password

 

  • docker exec wordpressdb ls

exec : If it used without any option(such as -i -t), it returns the result

docker exec wordpressdb ls
bin
boot
dev
docker-entrypoint-initdb.d

...

 

  • -v HostDirectory:ContainerDirectory
  • -v VolumeName:ContainerDirectory

Volume sharing

docker run -d --name wordpressdb_hostvolume -v /home/wordpress_db:/var/lib/mysql

or 

docker run -i -t --name myvolume_1 -v myvolume:/root/ubuntu:14.04 //myvolume is mounting over the root(container)