Skip to content

Instantly share code, notes, and snippets.

@thibaut-d
Last active August 7, 2022 21:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thibaut-d/c391d7625931d6233184421a359ce5ff to your computer and use it in GitHub Desktop.
Save thibaut-d/c391d7625931d6233184421a359ce5ff to your computer and use it in GitHub Desktop.
Cheatsheet for Docker
https://docs.docker.com/compose/reference/overview/
docker-compose build # build containers (usefull if changed)
docker-compose up # builds, (re)creates, starts, and attaches to containers for a service.
docker-compose up -d # same in detached mode
docker-compose up --force-recreate # stop all containers and recreate
docker-compose up --build # rebuild custom images
docker-compose stop # stop but dont remove
docker-compose start # restart
docker-compose down # stop & remove containers
docker-compose ps # list containers
docker-compose pull # download images
docker-compose push # push locally build images to remote
docker-compose restart # restarts all stopped and running services (use build if you changed docker-compose.yml)
docker-compose rm # removes stopped service containers
docker-compose exec web sh # run a bash command in web service
docker container ls
docker exec -it 3e2fdcbac6f7 /bin/bash
# Informations
docker --version # Print what version of Docker is installed
docker info # More informations, including number of running containers
docker image ls # List images
docker container ls # List running containers
docker container ls --all # Also show exited containers
docker container ls -q # Show running containers
docker container ls -aq # Also show containers in quiet mode
docker service ls # List running services (deployed with docker-compose.yml)
docker service ps myapp_myservice # List containers (aka tasks) used by the app
docker stack ps myapp # Show all tasks of a stack
#Build
docker build --tag=mycontainer . # Build a container in ./ using ./Dockerfile and name it mycontainer
# Run
docker run mycontainer # Run a container
docker run -p 4000:80 mycontainer # Run mycontainer while mapping its 80 port to the machine 4000 port
docker run -d mycontainer # Run in background detached mode
#Stop
docker container stop 1fa4ab2cf395 # Stop a container using its ID
#Use Docker Hub
docker login # Login from your machine in command line
docker tag imagename username/repository:tag # Give a name to the image, tag being a version number
docker push username/repository:tag # Make the image publicly available on Docker Hub
docker run -p 4000:80 username/repository:tag # Run the image pulling from the repository
# Deploy with docker-compose.yml
docker swarm init # For swarm management
docker stack deploy -c docker-compose.yml myapp # Deploy the app based on docker-compose.yml
docker stack rm myapp # Take down the app
docker swarm leave --force # Take down the swarm
# Run a command in a container
docker exec -i -t container_name /bin/bash # run a bash
docker exec -ti my_container sh -c "echo a && echo b" # run a command
# Same using composer
docker-compose exec container_name sh # run a bash
docker-compose exec <container name> <command> # run a command
docker-compose exec <name in yml> sh -c '<command 1> && <command 2> && <command 3>' # run several commands
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Set proxy server if needed, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port
# Run app.py when the container launches
CMD ["python", "app.py"]
version: "3"
services:
redis:
image: redis:alpine
ports:
- "6379"
networks:
- frontend
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- "5000:80"
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- "5001:80"
networks:
- backend
depends_on:
- db
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
placement:
constraints: [node.role == manager]
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
frontend:
backend:
volumes:
db-data:
@thibaut-d
Copy link
Author

thibaut-d commented Mar 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment