Docker Community Forums

Share and learn in the Docker community.

Issue while using docker exec to attach to containers


In my Dockerfile, I have declared a script “” to be run at ENTRYPOINT. looks like this -
export PATH=$PATH:/opt/bin
echo “ mynode” >> /etc/hosts
useradd -u $UID -m -s /bin/bash $USER
su $USER

  1. To spin a container I use -
    docker run -it --detach --name=containername -e USER=${USER} -e UID=id -u -e GID=id -g imagename bash
  2. I do a docker start and then attach to the container ‘containername’. Now, when I echo $PATH, I see /opt/bin present in the PATH
  3. I create another session to the same container using docker -exec -u $USER containername bash. Now, when I am inside the container I do not see PATH containing the /opt/bin. I am sure is running because I can see in /etc/hosts.

What am I missing here? Any help, will be greatly appreciated.


Use docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

Note: The attach command will display the output of the ENTRYPOINT/CMD process. This can appear as if the attach command is hung when in fact the process may simply not be interacting with the terminal at that time.

You can attach to the same contained process multiple times simultaneously, from different sessions on the Docker host.

To stop a container, use CTRL-c. This key sequence sends SIGKILL to the container. If --sig-proxy is true (the default),CTRL-c sends a SIGINT to the container. If the container was run with -i and -t, you can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.

Note: A process running as PID 1 inside a container is treated specially by Linux: it ignores any signal with the default action. So, the process will not terminate on SIGINT or SIGTERM unless it is coded to do so.

It is forbidden to redirect the standard input of a docker attach command while attaching to a tty-enabled container (i.e.: launched with -t).

While a client is connected to container’s stdio using docker attach, Docker uses a ~1MB memory buffer to maximize the throughput of the application. If this buffer is filled, the speed of the API connection will start to have an effect on the process output writing speed. This is similar to other applications like SSH. Because of this, it is not recommended to run performance critical applications that generate a lot of output in the foreground over a slow client connection. Instead, users should use the docker logs command to get access to the logs.