basic docker commands #class-notes
Tags: beginners, docker, cloudskills, aws
Published: 2022-04-26 15:46:39

#  Container Logic



**Without containerizing:**


![Without containerizing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hp2a5mmkgxfrte554q3a.png)
src:[kablosuzkedi](https://www.youtube.com/watch?v=4XVfmGE1F_w)

**With containerizing:**


![With containerizing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlzanl5iu6eppx1lp2qw.png)
src: [kablosuzkedi](https://www.youtube.com/watch?v=4XVfmGE1F_w)

NOTE: https://hub.docker.com/ -> The holy documentation.

---

**Commands**

`$ docker pull ubuntu |or| docker run ubuntu
$ docker pull mongo
$ docker run redis`
…

- It pulls ubuntu/mongo/redis from dockerhub. docker run controls the local to check if exists, if not it installs it on docker hub.
 
`$ docker images`
shows the installed images  

`$ docker run  -it  ubuntu` 

- opens the ubuntu terminal (-it means interactive terminal)

`$ docker ps
$ docker ps -a`

- this commands shows which container(s) are active right now.
- -a refers to “all” and it shows all containers which were active before (it also shows active ones too). 

`$ docker  run  -it  -- name   my_terminal   ubuntu`  
renames ubuntu to my_terminal (!. double - - before name)

`$ docker run  -it   my_terminal`

- now the terminal can use this name.

`$ docker start  my_terminal
$ docker stop   my_terminal`

- starts and stops terminals in the background.

`$ docker stop e7g`

- Container ID also can use instead of the name, same thing happens (the Container ID can learn by docker ps -a command).

`$ docker rm my_terminal
$ docker rm e7g
$ docker rm  eg7  gh4  77v  22w  fg4  #for multiple containers 
$ docker container rm $(docker container ls -aq) #for all containers`

- removes the specific container.

`$ docker run -d mongo`

- detach mode works in the background.

`$ docker container logs g87dw`

- shows logs.

`$ docker run -it user/lets-meet-app`

- if there is an interactive app wanted to use, -it flag must be written (otherwise it will just print the code). 

`$ docker inspect ubuntu`

- prints its attributes.

---

**Port Mapping**


`$ docker run -p 3000:3000 node #or another custom app, user/my-app
$ docker run -p 3001:3000 node
$ docker run -p 3002:3000 node`

-  Now the app can use on port 3000, 3001, 3002 (host:container).

`#try localhost:3000 , localhost:3001 and localhost:3002 … Same result.` 

---

**Volume Mapping and Connecting Containers**

_= Notes =_
• Containers work as a process (like downloaded Spotify, MS Word…)
• Host containers work stateless… So they don't record or save any data on them. When the container stops all info will be gone forever… To prevent this situation, volumes are used.

`$ docker volume create first-volume`

- creates a volume

`$ docker container run -it -v first-volume:/sample alpine sh`

- the first field is the name of the volume (first-volume). The second field is the path where the file or directory are mounted in the container (/sample)

* A simple exercise for volume mapping
```
docker volume create my-volume
docker container run -it -v my-volume:/sample alpine sh
cd sample
echo "hello from the first container" >> file1.txt
exit
docker container rm <ContainerId>

docker volume inspect my-volume
cd /var/lib/docker/volumes/my-volume/_data
cat file1.txt

or

docker container run -it -v first-volume:/try1 alpine sh
cd try1
cat file1.txt

touch file2.txt
echo "hello from the second container" >> file2.txt
exit

docker container run -it -v first-volume:/try2 ubuntu sh
cd try2
ls
```


`$ docker run     --name   mysql-server   -v  /opt/data:/etc/mysql/conf.d     -p  3306:3306      -e MYSQL_ROOT_PASSWORD=test147  -d mysql`

- -e stands for: env variable, -p: for port, - -name : for rename, -v : volume.
MYSQL_ROOT_PASSWORD is necessary for the password.

`$ docker rmi redis`

- rmi = remove image, removes images.

---

**Networking in Docker**

There are 3 basic network drivers in Docker, but the most common use is the bridge network. 

`$ docker network ls`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7sucev5xatc26gtfr7w.png)

`$ docker run -it -d -- name cont1 alpine ash
$ docker run -it -d -- name cont2 alpine ash`

- run 2 containers with alpine image.

`$ docker network inspect bridge`

- shows the details of the bridge network.


`$ docker inspect cont2 | grep IPAddress `

- getting the ip address of cont2.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01t216n6xidoafamkp17.png)

`$ docker attach cont1`

- attaching containers.

`$ ping -c 5 <cont2 ip address>`

- -c is stand for 5 hops.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2gouhlsc1kunqw6pcj8.png)

! Note
If we try to ping the 2nd container by name, it will give a "bad address" error because default bridge network containers can only communicate with their IP addresses.. 

`$ docker stop cont1 cont2
$ docker rm con1 cont2`

- stops and removes containers.

`$ docker network create --driver bridge mynet `

- creates a network bridge

`$ docker network connect mynet cont3`

- connects a container to my network

---
# Docker Compose


> Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.

> Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose. [Docs](https://docs.docker.com/compose/)

**Example usage**

docker-compose.yml

````
version: "3.9"  # optional since v1.27.0
services:
  web:
    build: .
    ports:
      - "8000:5000"
    volumes:
      - .:/code
      - logvolume01:/var/log
    links:
      - redis
  redis:
    image: redis

volumes:
  logvolume01: {}
```

`$ docker compose up`

- starts and runs the app.

---
**Plus: Dockerfile**

>  A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Dockerfile example
```
FROM python:alpine
COPY . /app
WORKDIR /app 
RUN pip install -r requirements.txt
EXPOSE 80
CMD python ./app.py
```


`$ docker build -f /path/to/a/Dockerfile .`

[Docs](https://docs.docker.com/engine/reference/builder/)


---

Note for AWS VM users:
- Instead of using sudo every command, write these.

`$ sudo usermod -a -G docker ec2-user
$ newgrp docker
`

---

Sources:
- https://docs.docker.com/engine/reference/commandline/rm/
- https://www.youtube.com/watch?v=ZeYIp1PrWXc&t=730s 
- Clarusway pre-classes -> https://clarusway.com/