Skip to content

Instantly share code, notes, and snippets.

@safizn
Last active December 19, 2017 12:17
Show Gist options
  • Save safizn/af65e4c2b00b9663fb25cea541cd845c to your computer and use it in GitHub Desktop.
Save safizn/af65e4c2b00b9663fb25cea541cd845c to your computer and use it in GitHub Desktop.
https://github.com/wsargent/docker-cheat-sheet
https://www.docker.com/sites/default/files/Docker_CheatSheet_08.09.2016.pdf
# Install docker from script https://docs.docker.com/v1.11/linux/step_one/
curl -sSL https://get.docker.com/ | sudo bash
# Environment variables for Windows: and maybe injected into env.var
export MSYS_NO_PATHCONV=1
set COMPOSE_CONVERT_WINDOWS_PATHS=1
docker info
docker version
docker ps -a
docker -v
# ip address inside the network bridge docker0
# http://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
# Check ip of host
• sudo ip addr show docker0
# Check ip of container
• ip addr show eth0
• route
# IMAGES
docker images
• --tree
docker history nameofcontainer
docker pull
docker rmi
# CONTAINERS
docker ps -a
docker start X
docker stop X
# One liner to stop / remove all of Docker containers:
• docker stop $(docker ps -a -q)
docker rm
• docker rm $(docker ps -a -q)
• docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)
• -v remove volume
• # delete all running and stopped containers:
• docker ps -aq | xargs docker rm -f
docker build --tag <name> --file <file> <path>
docker run -i -t --rm --name containername nameofimage /bin/sh
docker run -it --name node -v "$(pwd)":/data -w /data -p 80:80 node bash
docker run -it --rm --name nodeapp -v $(pwd):/srv/projects/app/ -w /srv/projects/app/ -p 80:80 nodeapp bash
• -d / -it = for detached and interactive modes.
• -v $(pwd):/usr/share/nginx/html
• -p 8080:80
• --name xcontainer
• --link nameOfContainerOutside:Nameofcontainerinside
• -w “/var/www/” node npm start
#run bash with a compound command:
docker run -it ubuntu /bin/bash -c “apt-get install -y vim && vim”
docker attach nameofcontainer
• docker exec -it <containerIdOrName> /bin/sh
• docker exec -it <container> bash
• use this command to keep container running “tail -F -n0 /etc/hosts”
# Print app output
docker logs <container id>
# DOCKERFILE
docker build -t tagname ./
docker build -t nodeapp --file nodejs.dockerfile ./
• -t=”tagname”
• --file Dockerfile.php-fpm
# Docker check internet connectivity connection inside containers, debug..
docker run microsoft/nanoserver ping google.com
# Keep container running
CMD "sleep 1000000000"
or
CMD "tail -F -n0 /etc/hosts"
or
CMD ["/usr/bin/supervisord", "-n"]
# Docker logs to stdout
docker logs <container_id> --follow
# the commands that are usually unchanged should be on top, in order to create less images for the container.
FROM ubuntu
EXPOSE 80
# command to run
# prefered way to specify PD1 process in container
ENTRYPOINT /usr/sbin/apache2ctl
# passing variables to ENTRY POINT
CMD [“-D”, “FOREGROUND”]
ENV var=value
VOLUMES /data
ENTRYPOINT [“node”, “server.js”]
ENTRYPOINT npm start
WORKDIR /var/www/
RUN apt-get update \
&& apt-get intall -y \
&& npm install \
def
# container healthcheck
HEALTHCHECK --interval=10s wget <API>
# run commands during dockerfile building
COPY entrypoint
RUN chmod +x entrypoint
RUN entrypoint
# PROBLEM: docker-machine in HyperV doesn't mount host filesystem at all. In fact there is no such way as for now. So hyperv is used for local containers which allow volume. But local nodes like docker-machine doesn't.
Install docker for Windows with HyperV enabled - install the components of toolbox seperately without boot2docker
Installation notes - “Docker for Windows” using HyperV without VirtualBox:
• Create new user for Docker to use:
○ Create a new user (Computer Management → System Tools → Local Users and Groups → Users → Create new user), e.g. "DockerHost". Give it a password.
○ Mark "User cannot change password" & "Password never expires" (in properties of that user).
○ Add it to "Administrators" group (also in properties of that user).
○ Share the drive where projects reside with the newly created user. i.e. C:/ (Properties of drive → sharing tab → Advanced sharing → Permissions → add the newly created user for docker → give it full access ). Repeat process for specific path to projects folder (for example of another user C:/Users/OtherUser/projects) and make sure it has permissions for full access. Sometimes this is handy for refreshing sharing and make it work.
○ Shared Drives in docker settings - Add user credentials to Docker for Windows, using the newly created user with its password.
• Create virtualSwitch to allow internet access for VMs (Hyper-V Manager → Virtual switch manager → create external switch → choose internet adapter → docker-machine create -d hyperv --hyperv-virtual-switch "<Primary Virtual Switch>" <VMName>)
• Use GitBash with MSYS_NO_PATHCONV=1 at beging to prevent formating of paths. & use $(PWD) for local containers instead of “./”
• Follow docker docs for more issues related to the installation.
docker run -it --entrypoint=/bin/bash <image>:latest -i
sudo -i
# path to cmd configuration of containers.
vi /var/lib/docker/containers/<containerID>/config.json
# Just stop the whole docker service, override the "Path" / "Args" or "Entrypoint" values in the /var/lib/docker/containers/<CONTAINER_HASH>/config.v2.json, and then start docker service again. And voi la!
# Change "Args"
"Args":["sleep:,"10000"]
# Doesn't appear to work in windows.
# tag image to Docker repository.
docker tag <imageName> myuserindocker/<imageNameInDockerRepository>:latest
docker login
# push tagged image to repository
docker push myuserindocker/<imageNameInRepository>
# remove unused images:
docker rmi -f $(docker images | grep "<none>" | awk "{print \$3}")
docker rmi $(docker ps -qf dangling=true)
# clean all unused:
docker system prune
# remove dangling images
docker image prune
# delete all containers
docker stop $(docker ps -a -q); docker rm -v $(docker ps -a -q)
# delete all containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
# remove all images
docker rmi $(docker images -q)
docker rmi -f $(docker images -q)
# http://stackoverflow.com/questions/32353055/how-to-start-a-stopped-docker-container-with-a-different-command
# commit stopped container
docker commit $STOPPED_CONTAINER user/test_image
# run with different command
docker run -ti --entrypoint=sh user/test_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment