Docker

Docker - How to use it, what’s all that (best link)

Install docker - ALREADY DONE, READ SOMEWHERE

Adding user to docker group

sudo usermod -aG docker ${USER}
su - ${USER}

Keywords

Basic commands

docker system prune -A 
docker rmi
docker image rm

Real commands

How to start

# -it for integrated terminal
docker create -it ubuntu:16.04 bash     
docker ps -a
#where 7643dba89904 is ID from before
docker start 7643dba89904       
#lists only started containers
Docker ps                   
#attaching to the container
docker attach 7643dba89904
#exiting container and will be stopped
exit
docker rm 7643dba89904      

How to volumes

#-v map directory (volume) $(pwd) on directory /var/www
docker create -it -v $(pwd):/var/www ubuntu:latest bash
#-d tells to run it detached
docker run -it -v $(pwd):/var/www -d ubuntu:16.04 bash
# Remove all unused local volumes
volume prune

How to ports

# --name my_container_name sets the name which can be later used
# -p 8080:80 sets my containers port 80 to be forwarded on my port 8080
docker run --name webserver -v $(pwd):/usr/share/nginx/html -d -p 8080:80 nginx

Dockerfile (How to images)

# Dockerfile
FROM nginx:alpine
VOLUME /usr/share/nginx/html
EXPOSE 80
#-t specifies the tag for the image
docker build . -t webserver:v1
docker run -v $(pwd):/usr/share/nginx/html -d -p 8080:80 webserver:v1
# Remove unused images
Image prune

Docker-compose

docker-compose --version
# docker-compose.yml
version: '2'
services:
 webserver:
   build: .
   ports:
    - "8080:80"
   volumes:
    - .:/usr/share/nginx/html
docker-compose up (-d)
# -f for specifying other file than docker-compose.yml
docker-compose -f demo.detailed.yml up
docker-compose -f demo.detailed.yml down