Skip to content

Instantly share code, notes, and snippets.

@dishantsethi
Last active July 17, 2019 19:19
Show Gist options
  • Save dishantsethi/bbe7360ec794afb54ecb1e830097ef9c to your computer and use it in GitHub Desktop.
Save dishantsethi/bbe7360ec794afb54ecb1e830097ef9c to your computer and use it in GitHub Desktop.
Getting started with docker commands helps and tips

Docker101

Show commands and management commands

docker

Docker version info

docker version

Show info like no. of conatiners, images etc

docker info

Containers

Create and run container in foreground

docker container run -it -p 80:80 ngnix

Create and run container in background

docker container run -d -p 80:80 ngnix

Name containers

docker container run -d -p 80:80 --name ngnix-server ngnix

NOTE:

  • Looked for image ngnix in image cache(images downloaded in your local computer)
  • If not found in your local computer(cache), it looks to the deafult image repo in Dockerhub.
  • Pulled it down(latest version) and store it in you image cache
  • Started it in a new container.
  • We specified to take port 80- on the host and forward to port 80 on the container
  • We could do "$ docker container run --publish 8000:80 --detach nginx" to use port 8000
  • We can specify versions like "nginx:1.09"

List running containers

docker container ls or docker ps

List all containers(not running also included)

docker container ls -a or docker ps -a

Stop particular container

docker container stop [container ID]

Stop all running container

docker stop $(docker ps -aq)

Remove container( cannot remove container, must stop first)

docker container rm [container ID]

To remove a running container use force(-f)

docker container rm -f [container ID]

Remove multiple containers

docker container rm [ID] [ID] [ID]

Get container logs(use name or ID)

docker container logs [NAME/ID]

List processes running in container(use name or ID)

docker container top [NAME/ID]

Images

List images we have pulled

docker image ls

Pull down image

docker pull [IMAGE]

Remove image

docker image rm [IMAGE]

Remove all images

docker rmi $(docker image -a -q)

Sample container creation

NGNIX: docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default)

APACHE: docker container run -d -p 8080:80 --name apache httpd

MONGODB: docker container run -d -p 27017:27017 --name mongo mongo

MYSQL: docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql

Container info

View info on container

docker container inspect [NAME]

Specific property(--format)

docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]

Performance stats(cpu, disk, memory, network etc)

docker container stats [NAME]

Create new ngnix container and bash into it

docker container run -it --name [NAME] nginx bash

Run/Create ubuntu container

docker container run -it --name ubuntu ubuntu

You can also make it, so when you exit the container, it does not stay, by using the -rm flag

docker container run --rm -it --name [NAME] ubuntu

Access an already created container, use -ai

docker container start -ai ubuntu

Use exec to edit config, etc

docker container exec -it mysql bash

Alpine is a very small Linux distro good for docker

docker container run -it alpine sh (use sh because it does not include bash) (alpine uses apk for its package manager - can install bash if you want)

Networking

Get port

docker container port [NAME]

List networks

docker network ls

Inspect network

docker network inspect [NETWORK_NAME] ("bridge" is default)

Create network

docker network create [NETWORK_NAME]

Create container on network

docker container run -d --name [NAME] --network [NETWORK_NAME] nginx

Connect existing container to network

docker network connect [NETWORK_NAME] [CONTAINER_NAME]

Disconnect container from network

docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]

Detach network from container

docker network disconnect

Image tagging and pushing to Dockerhub

tags are labels that point ot an image ID

docker image ls Youll see that each image has a tag

Retag existing image

docker image tag nginx dishantsethi/nginx

Upload to dockerhub

docker image push dishantsethi/nginx

If denied, do

docker login

Add tag to new image

docker image tag dishantsethi/nginx dishantsethi/nginx:testing

DOCKERFILE PARTS

  • FROM - The os used. Common is alpine, debian, ubuntu
  • ENV - Environment variables
  • RUN - Run commands/shell scripts, etc
  • EXPOSE - Ports to expose
  • CMD - Final command run when you launch a new container from image
  • WORKDIR - Sets working directory (also could use 'RUN cd /some/path')
  • COPY # Copies files from host to container

Build image from dockerfile (reponame can be whatever)

From the same directory as Dockerfile

docker image build -t [REPONAME]

TIP: CACHE & ORDER

  • If you re-run the build, it will be quick because everythging is cached.
  • If you change one line and re-run, that line and everything after will not be cached
  • Keep things that change the most toward the bottom of the Dockerfile

EXTENDING DOCKERFILE

Custom Dockerfile for html paqge with nginx

FROM nginx:latest # Extends nginx so everything included in that image is included here WORKDIR /usr/share/nginx/html COPY index.html index.html

Build image from Dockerfile

docker image build -t nginx-website

Running it

docker container run -p 80:80 --rm nginx-website

Tag and push to Dockerhub

docker image tag nginx-website:latest dishantsethi/nginx-website:latest docker image push dishantsethi/nginx-website

Docker Compose

  • Configure relationships between containers
  • Save our docker container run settings in easy to read file
  • 2 Parts: YAML File (docker.compose.yml) + CLI tool (docker-compose)

1. docker.compose.yml - Describes solutions for

  • containers
  • networks
  • volumes

2. docker-compose CLI - used for local dev/test automation with YAML files

To run

docker-compose up

You can run in background with

docker-compose up -d

To cleanup

docker-compose down

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