---
title: Docker Introduction
tags: Docker
---

:::info
* The core concepts are ==image== and ==container==
* We can **pull**/**push** images from Docker hub
* We can store the image into a tar file with **save** command, and load the image by **load** command
* We can run the container by **run** command, and save the container setting by **commit** command
* We can write down script in Dockerfile, and run Dockerfile to build the image by **build** command
:::
## You don't have to install docker
* [Click here to enjoy docker online](https://labs.play-with-docker.com/ "Play with docker")
* You can play around here for 4 hours, and renew again after that

## Pull
* 1. Let's pull an image called **nginx** from Docker Hub
* ```docker pull nginx```
* or ```docker pull nginx:latest```
If you don't specify the version in the end, it will pull the latest version by default

## Run
* Before run the image, we might wanna check all the images we have, we can list our image by running
* ```docker images```

* Run the image **[more info](https://docs.docker.com/engine/reference/run/)**
* ```docker run -d -p 80:80 nginx```
* **-d** means execute in the background ==Detached mode: Run container in the background, print new container id==
* **-p 80:80** set external and internal port, first one is external, second one is internal
* Then the image **name** you wanna run
* After successfully run the image, it will show the container ID, and the port number with link above

* Click the link, you will see the page

### Modify a file inside a container
* ```docker exec -it b3 bash```
* b3 is the first 2 letters of container's ID
* Hit enter, then you are in!

* Now we gonna modify **index.html**
* ```cd /usr/share/nginx/html```

* We can browse **index.html** by running ```cat index.html```
* Let's modify it, ```echo hello world > index.html```
* We can click on the link to see if it has changed

* We can now type ```exit``` to quit the editor
### Stop running a container
* ```docker ps``` to see all the on running containers
* ```docker ps -aq``` will list **all** the containers' ID
* ```docker stop 26``` to stop the conaitner whose ID starts with 26

### Remove a container
* ```docker container rm [container ID]```, You can type the first 2 letters just like examples above
### Remove an image
* ```docker images```, list all images
* ```docker rmi [image's name]```

## Commit
* We can copy container's content's and build an image
* ```docker commit [container's ID] [name the image we gonna build]```

* Run the image we have just built and see if it's the same
* ```docker run -d -p 8080:80 test```
## Build
### 1. Create a Dockerfile document
* ```vim Dockerfile```
* press **i** switch to --INSERT-- mode
```javascript=1
FROM test //which image we gonna use
// add all stuff under current src ./
// to this src /usr/share/nginx/html
ADD ./ /usr/share/nginx/html/
```
* press **ESC** to quit insert mode, type **:wq** to save and quit
* ==:w== save ==:q== quit
### 2. Create an index.html file
* ```vim index.html```
* press **i** switch to --INSERT-- mode

```javascript=1
// Type whatever you like
Welcome to my page!!!
```
* press **ESC** to quit insert mode, type **:wq** to save and quit
### 3. Run build
* ```docker build -t [name the image] .```

## Save
* List all the images ```docker images```
* ```docker save [image's name] > [name].tar```

## Load
* We remove some images before using **load** command, to make sure we really **load** the image from ==.tar== file

* ```docker load < [tar file name].tar```

## Cheat sheet for removing
* List all containers (only IDs)
```docker ps -aq```
* Stop all running containers
```docker stop $(docker ps -aq)```
* Remove all containers
```docker rm $(docker ps -aq)```
* Remove all images
```docker rmi $(docker images -q)```
* Remove all untagged images
```docker rmi $(docker images | grep "^<none>" | awk "{print $3}")```
## Get into the container (in Windows)
``` winpty docker exec -it [container's id, the first 4 lettes are enoughcd ] bash```
## Check container's ip
```docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container's id or name]```
## Logs
* List logs of a container
`docker logs [container id]`
| Name, shorthand| Default | Description|
| -------- | -------- | -------- |
| --details| | Show extra details provided to logs |
| --follow , -f | | Follow log output |
| --since | | Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) |
| --tail | all | Number of lines to show from the end of the logs |
| --timestamps , -t| | Show timestamps |
|--until| | Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) |
## Got permission denied while trying to connect to the Docker daemon socket => can't run docker commands without sudo
* ```sudo chmod 666 /var/run/docker.sock```
---
## Install PostgreSQL
`docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=11111111 postgres`
---
## Source
<iframe width="560" height="315" src="https://www.youtube.com/embed/AASsRoUfisk" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>