InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Docker tutorials

  • Docker quick start guide on Mac
  • Docker quick start guide on Ubuntu
  • Docker how to add a user to group docker on Linux
  • Docker container - handy commands
  • How to get docker container IP
  • Run nginx in docker container
  • Ubuntu docker - storage driver aufs not supported error
  • How to find docker host ip on Mac
  • Docker - how to rerun a container with different flags
  • Docker managing images - command line cheat sheet
  • Install nginx and php in docker ubuntu container
 
  • Home
  • > Tutorials
  • > Docker

Docker container – handy commands

on Feb 5, 2016

Some handy command to work with containers. We’ll assume a docker image ubuntu is available on current machine.

  1. Run interactive shell (or any other command) on ubuntu image and create a container (named ubuntu1).
    ## docker run -it --name [container_name] [image_id] /bin/bash
    $ docker run -it --name ubuntu1 ubuntu /bin/bash
    root@bdda3386fd58:/# pwd
    /
    

    To detach tty without existing shell, use Ctrl-p + Ctrl-q. Otherwise use Ctrl-d.

    To attach it again use

    docker attach [container_id_or_name]
    
  2. List containers

    $ docker ps  #running containers
    CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
    bdda3386fd58        ubuntu              "/bin/bash"         About a minute ago   Up About a minute                       ubuntu1
    $ docker ps -a  #all containers
    
  3. Start/stop/restart/remove/kill containers
    $ docker stop [container_id_or_name]  # stop container
    $ docker rm [container_id_or_name]  # remove container
    $ docker start [container_id_or_name] #start container
    $ docker restart [container_id_or_name] #restart container
    $ docker kill [container_id_or_name] #kill signal to container
    
  4. Run a command inside a running container
    ## docker exec [container_id_or_name] command
    $ docker exec bdda3386fd58 "ls"
    
  5. Get an interactive bash shell in a running container
    $ docker exec -it [container_id_or_name] /bin/bash
    
  6. Run multiple commands inside a running container
    ## docker exec [container_id_or_name] /bin/bash -c "command1;command2"
    $ docker exec bdda3386fd58 /bin/bash -c "cd /root; ls -a"
    .
    ..
    .bash_history
    .bashrc
    .profile
    

Suggested posts:

  1. How to use wildcard in robots.txt
  2. Docker how to add a user to group docker on Linux
  3. LXC (Linux Containers) – quick start tutorial on Ubuntu
  4. node – how to find source file location of a module
  5. Chrome extension tutorial – access active page dom
  6. Docker – how to rerun a container with different flags
  7. Mac docker – create getting-started image and run container using it
  8. Mac – create a one container pod and get shell to it
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Docker, Linux, Linux/Unix Command Line, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress