Skip to content

Instantly share code, notes, and snippets.

@goyalankit
Last active October 11, 2019 13:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save goyalankit/8120471 to your computer and use it in GitHub Desktop.
Save goyalankit/8120471 to your computer and use it in GitHub Desktop.
Docker Commands

Docker cheatsheet.

  • To get the list of commands:

      $ docker
    
  • To search for images in docker index:

      $ docker search tutorial
    
  • To pull the image:

      $ docker pull learn/tutorial
    
  • To run a command in contained environment:

      $ docker run learn/tutorial echo "hello world"
    
  • To install a new program:

      $ docker run learn/tutorial apt-get install -y ping
    
  • To list containers(Get container id from here):

      $ docker ps -l
    
  • To list running containers:

      $ docker ps
    
  • To make your changes persist. New layer

      $ docker commit [OPTIONS] CONTAINER_ID REPOSITORY
    
  • Inspect container to get more details:

      $ docker inspect CONTAINER_ID
    
  • Keep stdin open (-i) and allocate a pseudo-tty (-t)

      $ docker run -i -t learn/tutorial /bin/bash
    
  • You may need to enable ipv4 forwarding before using docker on virtual box.

    In /etc/sysctl.config file add the following line.

      net.ipv4.ip_forward = 1
    

    Run sysctl -p /etc/sysctl.conf to make the changes permanent.

Installing Redis using Docker

  1. Download the base image. It's a ubuntu image.

     $ docker pull base
    
  2. Start a shell in the new container

     $ docker run -i -t base /bin/bash
    
  3. Update ubuntu repo

     $ apt-get update
    
  4. Install telnet and redis server

     $ apt-get install telnet redis-server
    
  5. Login to docker, Commit the new image and push

     $ docker login
     $ docker commit continer_id	 goyalankit/redis
     $ docker push
    

    Note: It's possible to save container with run options. Check point 13.

  6. Create and enter in a new container using the new image.

     $ docker images
     $ docker run -i -t goyalankit/redis /bin/bash
    
  7. Start the redis server

     $ /etc/init.d/redis-server start
    
  8. Check that it's runnig.

     $ telnet 127.0.0.1 6379
    
  9. Note that you cannot cannot from outside the container because there are no port to connect to. Check using:

     $ docker inspect
    
  10. Exit the container and run the container with -p flag.

    $ docker run -p 6379 -i -t goyalankit/redis /usr/bin/redis-server
    
  11. Now check that the some external port at host is attached to the private port 6379.

    $ docker port CONTAINER_ID 6379
    
  12. Exit and start the container as a deamon.

    $ docker run -d -p 6379 -i -t goyalankit/redis /usr/bin/redis-server
    
  13. Test from outside.

    $ redis-cli -p HOST_PORT	
    
  14. Possible to save images with their configurations.

    $ docker commit -run '{"Cmd": ["/usr/bin/redis-server"], "PortSpecs": [":6379"]}' CONTAINER_ID goyalankit/redis		
    

Based on this blog post

  1. Docker Deamon: Docker may be installed with upstart. If you get a docker command not found then check if docker deamon is running. Errors are clear enough.

    To run deamon: sudo docker -d

  2. To Run the interactive shell and attach tty sudo docker run -i -t base /bin/bash

  3. To detach from above shell without exiting use Ctrl-p + Ctrl-q key sequence.

  4. Sudo and the docker group

    Docker deamon always run as root. You can add users to docker group so that they don't have to run each command with sudo.

	
# Add the docker group if it doesn't already exist.

sudo groupadd docker

# Add the connected user "${USERNAME}" to the docker group.
# Change the user name to match your preferred user.
# You may have to logout and log back in again for
# this to take effect.

sudo gpasswd -a ${USERNAME} docker

# Restart the docker daemon.

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