Skip to content

Instantly share code, notes, and snippets.

@Ivanitch
Forked from yesnik/docker-compose.md
Created February 19, 2022 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ivanitch/f14561f1e430605a36d2291564e27fbe to your computer and use it in GitHub Desktop.
Save Ivanitch/f14561f1e430605a36d2291564e27fbe to your computer and use it in GitHub Desktop.
Docker Guides

Docker Compose

Docker compose is a tool that is used for defining and running multi-container Docker apps in an easy way. It provides docker-compose.yml configuration file that can be used to bring up an app and the suite of services it depends on with just one command.

  • docker-compose up - start all services from docker-compose.yml
  • docker-compose up db - start only service db
  • docker-compose up --build - rebuild all images and run containers
  • docker-compose -f docker-compose.prod.yml up - start all services from docker-compose.prod.yml
  • docker-compose build db - rebuild db service
  • docker-compose down
  • docker-compose down --remove-orphans - remove containers for services not defined in the Compose file.
  • docker-compose down -v - shutdown containers, remove volumes. Docker stores volumes at /var/lib/docker/volumes
  • docker-compose logs db | less - show logs of db container
  • docker-compose ps - show running app's containers
  • docker-compose exec fpm bash - connect to running fpm container and start bash console in it
  • docker-compose exec db mysql -u root -ppassword - connect to running db container and start mysql console
  • docker-compose run --rm php-cli sh - run php-cli container, start sh console. --rm - Docker also removes the anonymous volumes associated with the container when the container is removed.
  • docker-compose rm db - remove db container
  • docker-compose rm -fv - remove all containers with volumes
  • docker-compose config - show docker-compose.yml content after the substitution step has been performed

Proxy settings for Docker

If you see an error:

docker-compose up
Pulling db (mysql:)...
ERROR: Get https://registry-1.docker.io/v2/: Proxy Authentication Required

you should configure your Docker's proxy settings (see stackoverflow).

  1. Create file /etc/systemd/system/docker.service.d/http-proxy.conf:
[Service]
Environment="HTTP_PROXY=http://kenny:123@277.27.16.233:3118"
Environment="HTTPS_PROXY=http://kenny:123@277.27.16.233:3118"
Environment="NO_PROXY=localhost,127.0.0.1,localaddress,.localdomain.com"
  1. Reload docker:
sudo service docker restart
  1. Check if it works:
docker pull busybox

Compose CLI environment variables

We can create .env file and define there env variables.

  • COMPOSE_PROJECT_NAME=myapp - Sets the project name. This value is prepended along with the service name to the container on start up. For example, if your project name is myapp and it includes two services db and web, then Compose starts containers named myapp_db_1 and myapp_web_1 respectively.

Docker Commands

Installation

We can install Docker using official bash script.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

System

  • docker system prune - clean up any resources - images, containers, volumes, and networks — that are dangling (not associated with a container)
  • docker system prune -a - remove ALL stopped containers, unused networks, abandoned images, build cache

Containers

They are created from Docker Images and run the actual app.

  • docker run busybox echo 123 - find image, load up the container, run a provided command
  • docker run --rm busybox - flag --rm: remove container when it exits
  • docker run -it busybox sh - flag --it: attach us to an interactive tty in the container
  • docker run --name hello -it busybox sh - param --name hello sets name hello for created container
  • docker run -p 8000:80 nginx - flag -p: publish a container's port to the host
  • docker run --net foo-network foo-container - flag --net: set network for container
  • docker run --rm -it -v $(pwd):/app php:8 bash - create volume for current dir
  • docker ps - list running containers
  • docker ps -a - list all containers
  • docker volume ls - list all volumes
  • docker start foo-container - start container
  • docker stop foo-container - stop container
  • docker rm foo-container - remove container
  • docker kill foo-container - kill container
  • docker container ls - list contaners
  • docker container ls -a - list all containers
  • docker container prune - remove all stopped containers
  • docker port foo-container - show exposed ports
  • docker container logs foo-container - see logs of the container
  • docker exec -it foo-container sh - run console into container
  • docker logs -f foo-container - show logs of container. Docker's log location: /var/lib/docker/containers/
  • docker stats - show statistics for running containers
  • docker stats foo-container - show CPU, Memory Usage, Net, Block I/O of container

Images

They are blueprints of our app which form the basis of containers.

  • docker build -t kenny/nginx:2.0 . - build an image with repo name kenny/nginx and tag 2.0
  • docker image ls - list images
  • docker image prune - remove all dangling images (not used by containers)
  • docker rmi -f foo-image-name - remove image, -f - forced mode
  • docker save nginx > nginx.tar - save image to .tar archive. Size: 121Mb
  • docker save nginx:latest | gzip > nginx.tar.gz - save image to .tar.gz archive. Size: 50Mb
  • docker load < ./nginx.tar.gz - load image from .tar.gz or .tar archive

Network

  • docker network ls - list docker's networks
  • docker network inspect bridge - inspect bridge network of docker
  • docker network create foo-network - create new bridge network
  • docker network rm foo-network - remove network
  • docker network prune - remove all custom networks not used by at least one container

By default, docker creates networks in these pools:

  • 172.17.0.0/16 - 172.31.0.0/16
  • 192.168.0.0/20 - 192.168.240.0/20

These IP addresses may conflict with server's IP address. You may lose SSH connection trying to docker-compose up. In this case create file /etc/docker/daemon.json:

{
  "default-address-pools" : [
    {
      "base" : "172.31.0.0/16",
      "size" : 24
    }
  ]
}

In these case created networks will have Subnet: 172.31.1.0/24, 172.31.2.0/24, .., 172.31.255.0/24.

Useful commands

Copy config from working container

docker run -d httpd:2.4
docker ps
docker cp [ID of container]:/usr/local/apache2/conf/httpd.conf ./httpd.conf
docker-compose run php bash
docker ps # See Container ID
docker cp 164b0441bb1c:/usr/local/etc/php/php.ini-development ./

Docker Swarm

  • docker node ls - show virtual machines in the cluster
  • docker service ls - show services in cluster
  • docker node inspect jenkins - show detail info for node
  • docker node update jenkins --label-add db - add label db for node jenkins
  • docker service ps site_api-php - show replicas' info for one serivce

Dockerfile

Example 1

# base image
FROM python:3-onbuild

# This port the container should expose
EXPOSE 5000

# Tell the container which command it should run when it's started
CMD ['python', './app.py']

Example 2

FROM nginx:latest

ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www/frontend

Docker Errors

ERROR: network site_default is ambiguous

docker network ls
docker network rm e2d790be0302
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment