Summaries/Overig/Docker/commands.md

90 lines
2.4 KiB
Markdown
Raw Normal View History

2022-08-09 21:04:44 +02:00
# Docker
2024-02-21 22:09:32 +01:00
This container runs in background
2024-02-19 22:01:37 +01:00
```bash
2024-02-21 22:09:32 +01:00
docker run -d -it --gpus all -name pytorch-container pytorch/pytorch:latest
docker run -d -it --rm -v $(pwd):/src --gpus all -name pytorch-container pytorch/pytorch:latest
2024-02-19 22:01:37 +01:00
```
2024-02-21 22:09:32 +01:00
The latter removes the docker instance when stopped and alse has a volume
## Connect to running container
2022-08-13 20:29:49 +02:00
2024-02-19 22:01:37 +01:00
```bash
2024-02-21 22:09:32 +01:00
docker exec -it <container-name> bash
docker exec -it <container-name> bash -c "cat aap"
2024-02-19 22:01:37 +01:00
```
2022-08-09 21:04:44 +02:00
2024-02-21 22:09:32 +01:00
## Stop and start an execistion container
2022-08-09 21:04:44 +02:00
```bash
2024-02-21 22:09:32 +01:00
docker stop <container-name>
docker start <container-name>
2022-08-09 21:04:44 +02:00
```
2024-02-19 22:01:37 +01:00
### docker ipaddress or running container
2022-08-09 21:04:44 +02:00
2024-02-21 22:09:32 +01:00
```bash
docker inspect <container-name or id> | grep '"IPAddress"' | head -n 1
```
2022-08-09 21:04:44 +02:00
2024-02-21 22:09:32 +01:00
### Run Postgresql daemon and psql attach
2022-08-09 21:04:44 +02:00
```bash
2024-02-21 22:09:32 +01:00
docker run --name my-postgres -e POSTGRES_PASSWORD=qw12aap -d postgres:latest
docker exec -it my-postgres psql -h localhost -U postgres -d postgres
2022-08-09 21:04:44 +02:00
```
2024-02-21 22:09:32 +01:00
inside: psql -U postgres -h my-postgres
2022-08-09 21:04:44 +02:00
### docker files
```dockerfile
FROM python:latest
RUN pip3 install numpy
CMD python3 /src/hello-world.py
```
### docker networks
```bash
docker network create net_1
2024-02-21 22:09:32 +01:00
2022-08-09 21:04:44 +02:00
docker run --rm -d --net net_1 --name my_py -v $(pwd):/src python:latest python3 /src/run.py
2024-02-21 22:09:32 +01:00
2022-08-09 21:04:44 +02:00
docker run --rm -it --net net_1 alpine:latest /bin/bash
docker network create net_2
docker run --rm --name my-postgres --network net_2 -e POSTGRES_PASSWORD=qw12aap -d postgres:latest
docker run -it --rm --name my_postgre2 --network net_2 postgres:latest /bin/bash
```
2024-02-21 22:09:32 +01:00
### Docker Compose; Container Network is created automatically
2022-08-09 21:04:44 +02:00
```docker-compose
version: '3'
services:
python:
image: python:latest
container_name: my_py
volumes:
- .:/src
command: python3 /src/run.py
restart: always
postgres:
image: postgres:latest
container_name: my_post
environment:
- e POSTGRES_PASSWORD=qw12aap
restart: always
alpine:
image: alpine:latest
command: echo "hello from alpine"
restart: always
```
2022-08-13 20:29:49 +02:00
[How To Remove Docker Images, Containers, and Volumes](https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes)
[How to Troubleshoot “Cannot Connect to the Docker Daemon” Errors](https://www.howtogeek.com/devops/how-to-troubleshoot-cannot-connect-to-the-docker-daemon-errors/)