Docker Cheatsheet for Web Devs
Tags: docker, webdev, codenewbie, devops
Published: 2021-07-01 16:06:43

As a web-dev in the microservices era you will encounter Docker sooner than later. It's always good to have the essential commands at arm's reach. Following is a list of Essential Docker commands that have helped me on a daily basis, hope this helps yo too. Happy Hacking!

## Docker Commands

### Docker run command

`docker run <image-name>`

example
`docker run hello-world`

### To add a override command
`docker run <image-name> <default command override>`

example

`docker run busybox ls`

### List all the running containers

Docker ps command specifically shows running containers

`docker ps`

Show all container ever run on machine

`docker ps --all`

**Docker ps is also used to get the ID of coantainer**

_docker run = docker create + docker start_

- when we create container we just prep it with say file sysytem etc
- When we run the container then we provide the default command that runs the program

`docker create hello-world`

now we use the id to start

`docker start -a 3c8a5bb0fe7153955d1261054e5b4c99f757920f7760fa7c07803cdbee4b36c1`

`-a` argument watches for any output and prints it on terminal

### To re-run an exitied container

first we get the conatiner of ID with ps command

`docker ps --all`

Then use the id with start command

`docker start -a 1285b5630516`

> When we re-run / restart a container we cannot replace the default command, it automatically takes the default command on re-run with commond it was created with.

### Removing stopped containers

`docker system prune`

### Docker logs command

Docker logs command shows all the output of con tainer when it had been running

`docker logs 73e564097bd5e6689c2bc5687c038dca99ec77d8716f7ff9eeab9434d422bb8d`

### Stopping a running container

`docker stop <container-id>`

This sends a SIGTERM signal allowing container to shut down in some time usually 10s, after that it issues Docker kill anyway 

`docker kill <container-id>`

This command issues a SIGKILL that immediately stops the container

**Docker stop is better way to stop***

### Executing commands in running containers

`docker exec -it <container-id> <command>`

- exec command allows us to type extra commands
- it flag helps us to enter text and send it to container

**The `it` flag**
- its a -i and -t 
- i attaches our terminal to STDIN
- t flag makes sure that it shows the output formatted on terminal, like auto complete you see with redis-cli

### How to get shell access in docker container

sh is command processor like bash, zsh etc

`docker exec -it <container-id> sh`

_if ctrl + c doesnt help you exit use ctrl+d or just type exit_

### starting container with a shell

`docker run -it <container-name> sh`

## Creating a Docker image 
### Flow to Creating a DockerFile

1. specify base image
2. Run some commands to install additional programs
3. specify command to run on container startup

### Sample docker file to create redis image

Create a file named `Dockerfile`  no extension just Dockerfile

```shell
# Use an existing docker image as base 
FROM alpine


# Download and install a dependency
RUN apk add --update redis

# Tell the image what to do when it starts as a container
CMD ["redis-server"]
```
use the docker build command to build image

`docker build .`

Run the image as usual

**Writing a docker file == say, given a computer with no OS and being asked to install chrome**

### Tagging an image

`docker build -t <docker-hub -id>/<project>:<version> .` Dont forget the dot at the end.

to run the image

`docker run <docker-hub -id>/<project>`

this automatically takes the latest version.

_Technically, the version number at the end is the actual tag here_

### Manual image generation with docker commit

`docker commit -c 'CMD ["<command>"]' <container id>`

## Copy build files

`COPY <relative local file system> <relative dest>`

## Port forwarding
`docker run -p <portno> : <container-port-no> <image-id>`

## Specifying a working directory

`WORKDIR /usr/app`

Any following commands will be executed relative to working dir