Attach and run a command in a docker container

I have a docker container running /bin/bash

So I can attach to it and run commands like

  • Error trying to install Ansible in Jenkins image
  • docker/fig: port forwarding to guest machine does not work on MacOS
  • How is Docker different from a normal virtual machine?
  • Get the host IP from my django app container
  • Docker build: unable to fetch archives
  • Installation of Kubernetes-node fails with conflicting packages on CentOs 7
  • docker attach <container id>
    bash# apt-get install package
    

    Is there a way I can do this in a single docker command instead, maybe something like this:

    docker attach <container id> "apt-get install package"
    

  • Exec sed command to a docker container
  • Where is the time spent starting a docker image?
  • How to create Ubuntu (running) base image to replicate on other environments.
  • Docker memory leak with sonarqube
  • No such file or directory in docker container
  • Docker isn't caching my RUN commands correctly
  • One Solution collect form web for “Attach and run a command in a docker container”

    This should work.

    echo 'sudo apt-get install package' | docker attach <container id>

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.