Skip to content

Instantly share code, notes, and snippets.

@wakproductions
Forked from dvdasari/gist:439ce7c1814c2392c3bf
Last active September 20, 2017 20:46
Show Gist options
  • Save wakproductions/d85bcaaab59024f9e6e0186803952a10 to your computer and use it in GitHub Desktop.
Save wakproductions/d85bcaaab59024f9e6e0186803952a10 to your computer and use it in GitHub Desktop.
Docker Commands Cheat Sheet

Base commands:

Creating a new container

docker run [-d (runs in background] [-p <host-system-port>:<container-port>] <image:tag>
* Creates a new container from an image
* If you run it 4 times, it will create 4 containers
Options:
   --name   Give the container a name, otherwise it will pick a default name.
   --link   Links containers together (so they can communicate?)
   -i       Make an interactive shell, often paired with -t option (what happens without -t?)
   -p       Maps <host-system-port> to <container-port> so 8080:80 maps localhost:8080 to :80 on the container
   -P       Maps all the open container points to random port numbers on the host. Unlike -p, which is explicit mapping.
   -t       TTY (gives you a bash prompt; without it you don't see the prompt)
   

Create container and run command:

docker run -it ubuntu bash
* starts new ubuntu container and runs bash terminal

docker-compose run web rails console
* runs the rails console

To get a bash to see what's inside container (good alternative to docker attach)

$ docker exec -i -t <container-name> /bin/bash
root@9173042a6daa:/usr/share/containername#
docker start <name|id>
* Will run a container that's stopped

docker stop <name|id>
docker ps [-a (to include stopped containers)]

Kill container

docker rm <name|id>

Kill all containers

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Remove image from the local system

docker rmi <name|id>

Remove all images:
docker rmi $(docker images -q)

May be useful:

docker logs <container-name>
docker build [-t (tag) <image name (i.e. wakproductions/hello-world)>]
* Builds image
* Follow up by docker run <image name>

docker push <image name>
* Sends it to docker hub. (You need to be logged in)

Get IP Address of Host

/sbin/ip route|awk '/default/ { print $3 }'

Docker registry related commands:

docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

Docker Stack (compose file v3)

docker stack ls              # List all running applications on this Docker host
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker stack services <appname>       # List the services associated with an app
docker stack ps <appname>   # List the running containers associated with an app
docker stack rm <appname>                             # Tear down an application

Volumes

Volumes in Docker can be used for persistent data. If we remove the MySQL container, we will lose all the data we put into our blog.To avoid this situation, we can create a Docker volume and mount it in /var/lib/mysql of the database container. The life of this volume would be totally separate from the container lifecycle.

Compose can help us with managing these so-called named volumes. They need to be defined under the volumes key in a Compose file and can be used in a service definition. Below is a snippet of our modified Compose file, which creates a mysql named volume and uses it in the mysql service.

version: '2'
services:
 mysql:  
  image: mysql
  container_name: mysql
  volumes:
    - mysql:/var/lib/mysql
...

volumes:
 mysql:

Compose will automatically create this named volume, and you will be able to see it with the docker volume ls command as well as find its path with docker volume inspect <volume_name>. Here is an example:

$ docker volume ls | grep mysql
local               vagrant_mysql
$ docker volume inspect vagrant_mysql
[
   {
       "Name": "vagrant_mysql",
       "Driver": "local",
       "Mountpoint": "/var/lib/docker/volumes/vagrant_mysql/_data"
   }
]

Be careful, however. If you bring down the application with docker-compose down, the persistent volume will be deleted and you will lose your data.

Running on Linux

Installation guide: https://docs.docker.com/engine/installation/linux/ubuntulinux/ Compose installation: https://docs.docker.com/compose/install/

  • I had some permissions problem on EC2. Had to dissect the URL and instead of curl use wget. Then rename the downloaded file to docker-compose and move it to mv ./docker-compose /usr/local/bin/docker-compose

Start the Docker Daemon

sudo service docker start

If you get the error: ERROR: Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running? you might have to use commands with sudo: sudo docker-compose up


  1. Create new container
    a) docker run -i -t ubuntu /bin/bash b) docker run --name rails_dev_container -i -t ubuntu ubuntu /bin/bash The command line flags - i -t provide an interactive shell in the new container

  2. Show running docker containers ==> docker ps

  3. Show list of current containers ==> a) docker ps -a (Show all containers, both stopped and running) b) docker ps -n x (shows the last x containers, running or stopped, ex: docker ps -n 5)

  4. Show last container that was run (whether is is running or stopped) ==> docker ps -l

  5. List docker images ==> docker images

  6. Delete a container
    a) docker rm <container_id> (ex: docker rm aa3f365f0f4e) b) docker rm <container_name> (ex: docker rm rails_dev_container)

  7. Delete all containers ==> docker rm $(docker ps -a -q)

  8. Delete an image ==> docker rmi <image_id>

  9. Delete all images ==> docker rmi $(docker images -q)

Start a stopped container a) docker start <container_id> (ex: docker start aa3f365f0f4e) b) docker start <container_name> (ex: docker start rails_dev_container)

Attach to a container a) docker attach <container_id> (ex: docker attach aa3f365f0f4e) b) docker attach <container_name> (ex: docker attach rails_dev_container)

Creating daemonized containers docker run --name daemon_container -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" (-d flag detachs the container to the background)

To see what's happening inside the container docker logs rails_dev_container docker logs -f rails_dev_container (like the tail -f) docker logs -ft rails_dev_container (prefix log entries with timestamps) docker logs --tail 10 daemon_container (get the last 10 lines of the log) docker logs --tail 0 -f daemon_container (follow the logs without having to read the whole log file)

Inspecting the container's processes docker top daemon_container

Running a process inside a container a) Background docker exec -d daemon_container touch /etc/new_config_file b) Interactive docker exec -t -i daemon_container /bin/bash

Stopping a daemonized container docker stop daemon_container or docker stop aa3f365f0f4e

Automatic container restarts docker run --restart=always --name daemon_container -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" (docker will try to restart the container no matter what exit code is returned) --restart=on-failure:5 (this will attempt to restart the container a maximum of 5 times if a non-zero exit code is received)

Finding more about our container docker inspect daemon_container docker inspect --format='{{ .State.Running }}' daemon_container docker inspect --format '{{ .HostnamePath }}' daemon_container docker inspect --format '{{ .Name }} {{ .HostnamePath }}' daemon_container list multiple containers docker inspect --format '{{ .Name }} {{ .HostnamePath }}' daemon_container dameon_cont_2

Pulling images docker pull ubuntu docker pull fedora docker pull fedora:21 docker pull jamtur01/puppetmaster

Searching for images docker search puppet

Sign into the Docker Hub docker login

Using Docker commit to create images Create a container from the ubuntu image docker run -i -t ubuntu /bin/bash Install Apache into the container apt-get -yqq update apt-get -y install apache2 exit from the container docker commit 6b84282435f8 dvdoc/apache2 or docker commit -m="A new custom image" --author="DV Suresh"
6b84282435f8 dvdoc/apache2:webserver

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