# Docker
###### tags: `SCh-20.35_23.11.2021_Applications Environment Security` `docker`
[Docker - setup](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
[Docker compose - setup](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04-ru)
## Step 1 — Installing Docker
`sudo apt update`
install a few prerequisite packages which let apt use packages over HTTPS:
`sudo apt install apt-transport-https ca-certificates curl software-properties-common`
Then add the GPG key for the official Docker repository to your system:
`curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -`
Add the Docker repository to APT sources:
`sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"`
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
`apt-cache policy docker-ce`
> Output of apt-cache policy docker-ce
>
> ```
> docker-ce:
> Installed: (none)
> Candidate: 5:19.03.9~3-0~ubuntu-focal
> Version table:
> 5:19.03.9~3-0~ubuntu-focal 500
> 500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
> ```
>
Finally, install Docker:
`sudo apt install docker-ce`
Check that it’s running:
`sudo systemctl status docker`
The output should be similar to the following, showing that the service is active and running:
> Output
> ● docker.service - Docker Application Container Engine
> Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
> Active: active (running) since Tue 2020-05-19 17:00:41 UTC; 17s ago
> TriggeredBy: ● docker.socket
> Docs: https://docs.docker.com
> Main PID: 24321 (dockerd)
> Tasks: 8
> Memory: 46.4M
> CGroup: /system.slice/docker.service
> └─24321 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
## Step 2 — Executing the Docker Command Without Sudo (Optional)
If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
`sudo usermod -aG docker ${USER}`
To apply the new group membership, log out of the server and back in, or type the following:
`su - ${USER}`
You will be prompted to enter your user’s password to continue.
Confirm that your user is now added to the docker group by typing:
`groups`
> Output
> sammy sudo docker
If you need to add a user to the docker group that you’re not logged in as, declare that username explicitly using:
`sudo usermod -aG docker username`
# install docker-compose
`apt install docker-compose`
# Create Docker
## Create Docker image
```
cd /opt/mydockerbuild
nano dockerfile
```
```
# ---
# Kaspersky update utility 4.0
#ubuntu ver
FROM ubuntu:21.10
LABEL maintainer="vaf@itzapad.ru"
# install soft
RUN apt-get update && \
apt-get -yq install cron && apt-get install vim -y && apt-get install wget -y
# setup working dir
WORKDIR /opt/kuu
RUN wget https://products.s.kaspersky-labs.com/special/kasp_updater3.0/4.1.0.474/english-4.1.0.474/3530393035317c44454c7c31/kuu4.1.0.474_x86_64_en.tar.gz && \
tar xvzf kuu4.1.0.474_x86_64_en.tar.gz && rm kuu4.1.0.474_x86_64_en.tar.gz && rm /opt/kuu/updater.ini
COPY updater.ini /opt/kuu/updater.ini
COPY kavupdater.sh /usr/bin/kavupdater.sh
# Giving executable permission to script file.
RUN chmod +x /usr/bin/kavupdater.sh && useradd -ms /bin/bash kladmin && \
chown -R kladmin:kladmin /opt/kuu/ && mkdir /opt/Updates && mkdir /opt/Temp && \
mkdir /opt/logs && chown -R kladmin:kladmin /opt/Updates && \
chown -R kladmin:kladmin /opt/Temp && chown -R kladmin:kladmin /opt/Updates && chown -R kladmin:kladmin /opt/Temp
# setup cron
COPY kavupdater_cron /etc/cron.d/kavupdater_cron
RUN chmod 0644 /etc/cron.d/kavupdater_cron && crontab /etc/cron.d/kavupdater_cron && \
crontab /etc/cron.d/kavupdater_cron && touch /var/log/cron.log
```
## Docker build
```
docker image build -t docker_kaspersky_uu4.0 .
```
```
docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname
```
## Create Repository in Docker Hub
[Create Repository](http://dker.ru/docs/docker-engine/get-started-with-docker/create-a-docker-hub-account-repository/)
## Push image to repository
[Docker Push](https://dker.ru/docs/docker-engine/get-started-with-docker/tag-push-pull-your-image/)
### create version of docker image
```
docker tag 6c2156c2d3a0 vladimirfp/docker_kaspersky_uu4.0:latest
```
```
docker images
```
### login to docker hub
```
docker login
```
### push repository
```
docker push vladimirfp/docker_kaspersky_uu4.0
```
## Remove images
```
docker rmi -f IMAGE_ID
```
## Docker pull
`docker run vladimirfp/docker_kaspersky_uu4.0`
`docker run -v /opt/Updates:/opt/Updates -it vladimirfp/docker_kaspersky_uu4.0 bash`
[Docker cron](https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container)
[Docker mount directory](https://towardsdatascience.com/how-to-mount-a-directory-inside-a-docker-container-4cee379c298b)
[Docker](https://habr.com/ru/company/ruvds/blog/440658/)
# Остановить все Docker контейнеры.
`docker stop $(docker ps -a -q)`
# Удалить все Docker контейнеры
`docker rm $(docker ps -a -q)`
# Удалить все Docker images
`docker rmi $(docker images -q)`