Show Menu
Cheatography

dockerexp1 Cheat Sheet (DRAFT) by

experiment with docker

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Start

docker version
get the docker version
docker run hello-­world
check if everething works
docker run -d redis
-d for run in background (detach mode)
docker ps
show all runs. ps stands for ‘process status’
docker ps -a
show all run / paused / stoped
docker stop CONTNAME
stop a running container with his name
docker rm CONTNAME
remove the instance
docker rm $(docker ps -a -q)
remove all listed instances
docker system prune
Erase all /!\ use for big clean
docker kill CONTNAME
stop close nicely with maybe some save, message etc Kill will kill now

DOCKER IMAGE

docker image ls
List all your images

Useful flags

ls
List running
ls --all
List all
ls -aq
List all in quiet mode

REDIS

docker run redis
will start the redis image
docker exec -it 0c04f2­52f8d2 redis-cli
In an other terminal run this command to access the redis instance with redis-cli
exec : execute multi command
-it : intera­ctive mode == -i -t : (-i attach to stdin) (-t is use to format nicely what you see)

USEFUL COMMANDS

docker cp folder­/te­st.txt CONTNA­ME:­/path
CP - Copy file to/from docker container:
docker image inspect IMAGENAME
INSPECT - use to inspect info about an image
docker logs
Access to logs useful when build, run fail

DELETE IMAGES & CONTAINERS

docker rmi <yo­ur-­ima­ge-­id>
delete docker image (no container linked)
docker container rm <co­nta­ine­r_i­d_1>
Delete container
docker image prune -a
delete all images
docker container prune
delete all stopped containers

Working with volume

VOLUME ["ap­p/t­emp­fil­e"]
Add anonymous volume for temp files in Dockerfile
docker volume ls
List all volumes available
docker run -p 3000:3000 --rm --name test-app -v tosave­:ap­p/s­aveme imageN­ame­:info
Create a named volume to get data persistant
docker volume rm VOL_NAME
To delete a specific volume
docker volume prune
To clear al volumes

ENV & ARG

ENV name value
is use on docker file
--env PORT=3000 or -e PORT=3000
is use on docker run (overwrite docker­file)
--env-file .env
ondocker run use a file to define env variable
ARG DEFAUL­T_P­ORT=80
is use on dockerfile for the image build
ENV PORT $DEFAU­LT_PORT
ex of use ARG in dockerfile
docker build -app:1 --buil­d-arg DEFAUL­T_P­ORT­=3000
Change ARG in dockerfile on build with --buil­d-arg
 

TIPS 1 - CREATE START LOGS

docker run = docker create + docker start
docker create hello-­world
return a container id
docker start -a 456789­123dj..
-a mean get output to my terminal
docker logs 456789­123dj..
print the logs when not use the -a
456789­123dj.. is the container id

TIPS 2 - EXEC -it SH

docker exec -it 0c04f2­52f8d2 sh
open a shell in a running container
cmd + d or type exit
exit the shell
docker run -it busybox sh
attach stdin stdout & run shell

DOCKERFILE

Create a Dockerfile
FROM WORKDIR COPY RUN EXPOSE CMD
docker build .
run this command to build your image
=> Succes­sfully built b3cc42­a9ef20
Return you the image id
docker build -t myredis .
Add a tag to the image
mcz/re­dis­:latest
tag conven­tion: docker­id/­pro­jec­tna­me:­version
FROM node:a­lpine
take the node base image
COPY ./ ./
copy all file inside our project
WORKDIR /user/app
Set a default safe directory to pull files
RUN npm install
node classic action
ENV PORT 3000
Use env variable in your docker file EXPOSE $PORT`
CMD ["np­m", "­sta­rt"]
Script start in packag­e.json
docker build -t john/n­ode­server .
Build image with a tag name
docker run -p 8080:8080 john/n­ode­server
Set port mapping to enable incoming traffic
docker ps
list running container with id
docker exec -it c66323­38e690 sh
Get a shell inside your running container
docker build -f Docker­fil­e.dev
To run a dev version called Docker­fil­e.dev

DOCKER COMPOSE

docker­-co­mpose
List all cmd available
docker­-co­mpose up
like docker run
docker­-co­mpose up --build
build image + run
docker­-co­mpose up -d
With -d run in background
docker­-co­mpose down
Stop the containers
restart: on-failure
LIST OF RESART CMD
"­no"
never restart
always
restart anyway
on-failure
restart on proces­s.e­xit(34) value > 0
unless­-stoped
always unless the dev stop
docker­-co­mpose ps
Only works in the current directory

Termin­ology

SIGTERM
terminate signal
SIGKILL
Kill signal