Docker container – handy commands

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"
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments