Skip to content

Instantly share code, notes, and snippets.

@patrickleet
Last active October 15, 2016 14:08
Show Gist options
  • Save patrickleet/d171f893a9125fce6b142cde84fc897a to your computer and use it in GitHub Desktop.
Save patrickleet/d171f893a9125fce6b142cde84fc897a to your computer and use it in GitHub Desktop.
docker | docker-machine | docker-compose | docker swarm cheatsheet

docker | docker-machine | docker-compose | docker swarm cheatsheet

docker-machine

Used to create VMs that run docker

create

docker-machine create -d virtualbox machinename

Using machine you created as target

eval $(docker-machine env machinename)

remove

docker-machine rm -f machinename

docker

build

docker build -t your-image-name .

docker-compose equivalent:

docker-compose-test-local.yml

app:
  build: .
  image: your-image-name
docker-compose -f docker-compose-test-local.yml build app

list images

docker images

docker-compose equivalent:

docker-compose ps

docker-compose

Dependencies

docker-compose-test-local.yml

staging-dep:
  image: your-image-name
  ports:
  - 8080:8080
  depends_on:
  - db

db:
  image: mongo
docker-compose -f docker-compose-test-local.yml up -d staging-dep

Running tests

docker-compose-test-local.yml

staging:
  extends:
    service: unit
  environment:
    - HOST_IP=localhost:8080
  network_mode: host
  command: bash -c "npm i && npm test"
docker-compose -f docker-compose-test-local.yml run --rm staging

Stop and Remove running services

docker-compose -f docker-compose-test-local.yml down

Running a Docker Registry

docker-compose-test-local.yml

registry:
  container_name: registry
  image: registry
  ports:
    - 5000:5000
  volumes:
    - .:/var/lib/registry
  restart: always
docker-compose -f docker-compose-local.yml up -d registry

Pull and push images from Docker Registry

Docker uses a naming convention to decide where to pull and push images from. If the name is prefixed with an address, the engine will use it to determine the location of the registry. Otherwise, it assumes that we want to use Docker Hub. Therefore, the first command pulled the alpine image from Docker Hub.

The second command created a tag of the alpine image. The tag is a combination of the address of our registry (localhost:5000) and the name of the image. Finally, we pushed the alpine image to the registry running on the same server.

# pull from docker hub
docker pull alpine

# tag an image
docker tag alpine localhost:5000/alpine

# push image to registry 
docker push localhost:5000/alpine

confirm images are persisted on host

ls -1 docker/registry/v2/repositories/alpine/

The output is as follows.

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