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}
ps - Lists running containers, optional -a flag to list all containers.
create - Creates a container from an image.
start - Starts a created container.
run - Creates and starts the container
attach - Attaches the terminal’s standard input and output to a running container, literally connecting you to the container as you would to any virtual machine.
exit - exits and stops the container
build - builds an image
images - lists all images
system - manage Docker
docker system prune -A
docker rmi
docker image rm
# -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
#-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
# --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
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 --version
up - with docker-compose.yml it builds image with Dockerfile and runs it
down
Docker-compose.yml The Compose file is a YAML file defining services, networks and volumes. The default path for a Compose file is ./docker-compose.yml
build Configuration options that are applied at build time. build can be specified either as a string containing a path to the build context or, as an object with the path specified under context
image Specify the image to start the container from. Can either be a repository/tag or a partial image ID.
secrets Grant access to secrets on a per-service basis using the per-service secrets configuration. Two different syntax variants are supported.
restart no is the default restart policy, and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.
environment Add environment variables. You can use either an array or a dictionary. Any boolean values (true, false, yes, no) need to be enclosed in quotes to ensure they are not converted to True or False by the YML parser.
command Override the default command.
links Link to containers in another service. Either specify both the service name and a link alias (SERVICE:ALIAS), or just the service name.
# 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