# Docker

# General Commands

## Containers

### List running containers

```bash
docker ps
```

### List all containers

```bash
docker ps -a
```

### Run a container from an image

```bash
docker run [OPTIONS] imagename[:tag]
```

Options:

- --rm (remove after close)
- -p 8000:8000 (bind docker port 8000 to computer port 8000)
- -d (run in background)
- --env-file .env (adds env file to container run)

### Restart container

```bash
docker start container-name
```

### Stop a container

```bash
docker stop container-name
```

### Remove a container

```bash
docker rm myubuntu
```

## Images

### List images

```bash
docker images
```

### Remove image

```bash
docker rmi IMAGE_ID
```

### Build image

```bash
sudo docker build -t image_name .
```

# Building a first setup

### Bulding a container

```bash
sudo docker build -t CONTAINER_NAME .
```

`-t` is the container "tag" name  
`.` assumes the "Dockerfile" is in the same directory as the PWD

### Running and opening a container

```bash
docker run -it --rm CONTAINER_NAME bash
```

`-it` interactive terminal like bash  
`--rm` removes the container after exiting