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

  • Docker can't expose mesos port 5050
  • Can You Modify the Base Image Directly?
  • Alpine Linux Docker SSHD Image Uses VOL-save?
  • Docker not port forwarding correctly?
  • Smooth solution for using docker with chef
  • Image name dropdown is empty when configuring remote interpreter Docker in PyCharm 2016
  • 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"
    

  • Environment variables and @Value can't work together on Spring Boot
  • Using Docker for Drupal Dev (Local)
  • Docker Consul service ip advertised as internal docker ip
  • Getting “can't cd” error when when building using dockerfile
  • How to connect mysql in a rails app with docker?
  • Kubernetes MySQL replication - Master service host inquiry
  • 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.