How to get bash or ssh into a running container in background mode?











up vote
833
down vote

favorite
348












I want to ssh or bash into a running docker container. Please, see example:



$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg


now I want to get something like this (go into the running container):



$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root@665b4a1e17b6:/#
However when I run the line above I get new CONTAINER ID
$ root@42f1e37bd0e5:/#


I used Vagrant and I'd like to get a similar behaviour as vagrant ssh.










share|improve this question
























  • alternatively sudo docker exec -i -t 665b4a1e17b6 /bin/sh to be able to install apt programs and packages
    – fonjeekay
    Jun 9 at 20:16















up vote
833
down vote

favorite
348












I want to ssh or bash into a running docker container. Please, see example:



$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg


now I want to get something like this (go into the running container):



$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root@665b4a1e17b6:/#
However when I run the line above I get new CONTAINER ID
$ root@42f1e37bd0e5:/#


I used Vagrant and I'd like to get a similar behaviour as vagrant ssh.










share|improve this question
























  • alternatively sudo docker exec -i -t 665b4a1e17b6 /bin/sh to be able to install apt programs and packages
    – fonjeekay
    Jun 9 at 20:16













up vote
833
down vote

favorite
348









up vote
833
down vote

favorite
348






348





I want to ssh or bash into a running docker container. Please, see example:



$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg


now I want to get something like this (go into the running container):



$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root@665b4a1e17b6:/#
However when I run the line above I get new CONTAINER ID
$ root@42f1e37bd0e5:/#


I used Vagrant and I'd like to get a similar behaviour as vagrant ssh.










share|improve this question















I want to ssh or bash into a running docker container. Please, see example:



$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg


now I want to get something like this (go into the running container):



$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root@665b4a1e17b6:/#
However when I run the line above I get new CONTAINER ID
$ root@42f1e37bd0e5:/#


I used Vagrant and I'd like to get a similar behaviour as vagrant ssh.







bash ssh docker container






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '16 at 15:16









Visruth

1035




1035










asked Aug 1 '14 at 16:19









Timur Fayzrakhmanov

17k81933




17k81933












  • alternatively sudo docker exec -i -t 665b4a1e17b6 /bin/sh to be able to install apt programs and packages
    – fonjeekay
    Jun 9 at 20:16


















  • alternatively sudo docker exec -i -t 665b4a1e17b6 /bin/sh to be able to install apt programs and packages
    – fonjeekay
    Jun 9 at 20:16
















alternatively sudo docker exec -i -t 665b4a1e17b6 /bin/sh to be able to install apt programs and packages
– fonjeekay
Jun 9 at 20:16




alternatively sudo docker exec -i -t 665b4a1e17b6 /bin/sh to be able to install apt programs and packages
– fonjeekay
Jun 9 at 20:16










15 Answers
15






active

oldest

votes

















up vote
1219
down vote



accepted










The answer is Docker's attach command. So for my example above, the solution will be:



$ sudo docker attach 665b4a1e17b6 #by ID
or
$ sudo docker attach loving_heisenberg #by Name
$ root@665b4a1e17b6:/#


For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container's shell. If we use attach we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container's shell, we just need to run the following:



$ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID


or



$ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name
$ root@665b4a1e17b6:/#





share|improve this answer



















  • 5




    Alternatively, execute sudo docker attach loving_heisenberg
    – thiagowfx
    Sep 21 '14 at 19:59








  • 39




    the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
    – Mo J. Mughrabi
    Oct 21 '14 at 8:32






  • 10




    A reminder for boot2docker users: remove sudo :)
    – Henno
    Dec 29 '14 at 13:56






  • 12




    -i -t equals -it
    – pasha.zhukov
    Aug 12 '16 at 7:57








  • 34




    This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
    – fwc
    May 10 '17 at 18:27


















up vote
608
down vote













From Docker 1.3 onwards:



docker exec -it <containerIdOrName> bash


Basically, if the Docker container was started using the /bin/bash command you can access it using attach. If not, then you need to execute the command to create a Bash instance inside the container using exec.



Also to exit Bash without leaving Bash running in a rogue process:



exit


Yep, it is that simple.






share|improve this answer























  • still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
    – WiR3D
    Oct 29 '14 at 13:46










  • Is there any way to set bash by default in dockers?
    – ipeacocks
    Apr 6 '15 at 13:13










  • @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
    – WiR3D
    Apr 7 '15 at 7:43






  • 9




    Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
    – Maiku Mori
    Mar 7 '16 at 17:48






  • 1




    I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
    – nobar
    Aug 11 '17 at 21:39


















up vote
105
down vote













Although the author of the question specifically said they are interested in a running container, it's also worth noting that if the container is not running, but you'd like to run it to poke around you can run:



docker run -i -t --entrypoint /bin/bash <imageID>






share|improve this answer



















  • 6




    That gives a different container, just like @kraxor's answer.
    – Blaisorblade
    Sep 17 '15 at 15:39


















up vote
26
down vote













Try this:



sudo docker run -i -t webserver /bin/bash


Source: https://docs.docker.com/articles/basics/#running-an-interactive-shell






share|improve this answer

















  • 13




    It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
    – Timur Fayzrakhmanov
    Aug 1 '14 at 16:31










  • link is dead, you should update
    – Ahmet Karakaya
    Apr 15 at 15:04


















up vote
15
down vote













Based on @Timur's answer I've created the following handy script



Setup



Put docker-ssh file in your $PATH with the following contents



#!/bin/bash -xe

# docker container id or name might be given as a parameter
CONTAINER=$1

if [[ "$CONTAINER" == "" ]]; then
# if no id given simply just connect to the first running container
CONTAINER=$(docker ps | grep -Eo "^[0-9a-z]{8,}b")
fi

# start an interactive bash inside the container
# note some containers don't have bash, then try: ash (alpine), or simply sh
# the -l at the end stands for login shell that reads profile files (read man)
docker exec -i -t $CONTAINER bash -l


Note: Some container do not contain bash, but ash, sh etc. In these cases bash shall be replaced in the above script.



Usage



If you have one running instance simply run



$> docker-ssh 


Otherwise provide it with a docker id parmeter that you get from docker ps (first col)



$> docker-ssh 50m3r4nd0m1d





share|improve this answer























  • May I know why we need -l at the end?
    – Nam G VU
    Nov 24 '17 at 6:46










  • to start bash as a login shell, reading the environment parameters ( described in the line above the command)
    – Matyas
    Nov 24 '17 at 11:15


















up vote
8
down vote













I've created a containerized SSH server that provides SSH capabilities to any running container. You don't need to change your container. The only requirement is that the container has bash.



If you have a container with name 'web-server1'. The following docker run command would start a second container that would provide SSH for the first container.



docker run -ti --name sshd-web-server1 -e CONTAINER=web-server1 -p 2222:22 
-v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker
jeroenpeeters/docker-ssh


For more pointers, checkout https://github.com/jeroenpeeters/docker-ssh






share|improve this answer





















  • This should be the accepted answer ^
    – Nam G VU
    Jul 8 '17 at 6:03










  • By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
    – Nam G VU
    Jul 8 '17 at 6:05




















up vote
7
down vote













If your container doesn't have bash installed you could try sh:



docker exec -it CONTAINER /bin/sh


Or look for shells in /bin first:



docker export CONTAINER|tar -t|egrep ^bin/





share|improve this answer























  • What is "consul"? Do you have a reference for it? Do you mean "console"?
    – Peter Mortensen
    Aug 5 at 13:28


















up vote
5
down vote













@jpetazzo has an awesome post about this subject. The short answer would be to use nsenter:



PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
nsenter --target $PID --mount --uts --ipc --net --pid


P.S.: Don't forget to check the discussion in the comments of the post...



Cheers






share|improve this answer



















  • 1




    That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
    – drevicko
    Mar 10 '17 at 15:46


















up vote
3
down vote













You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.



This will be more "traditional" (ssh), instead of using an application-specific command like docker attach, and will eventually make it more 'portable' across systems and versions.






share|improve this answer























  • Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
    – Timur Fayzrakhmanov
    Oct 29 '14 at 12:08








  • 2




    There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
    – radriaanse
    Oct 29 '14 at 12:30










  • You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
    – Timur Fayzrakhmanov
    Oct 29 '14 at 12:57


















up vote
2
down vote













docker run -it openjdk:8


This works :-)






share|improve this answer






























    up vote
    2
    down vote













    Sometimes it will be handy to be able to ssh into a Docker container, especially during development. The following Docker image allows to ssh into a container using a private key:



    UbuntuWithSSH-Docker



    The gist of the Dockerfile is https://gist.github.com/devbkhadka/98792f7bca57f9778793b2db758b3d07.






    share|improve this answer






























      up vote
      1
      down vote













      GOINSIDE



      install goinside command line tool with:



      sudo npm install -g goinside


      and go inside a docker container with a proper terminal size with:



      goinside docker_container_name




      for more details check this out.






      share|improve this answer






























        up vote
        0
        down vote













        To bash into a running container, type this:



        docker exec -t -i container_name /bin/bash





        share|improve this answer

















        • 1




          this is the same answer as @AdamKalnas though
          – Bruni
          Sep 6 '16 at 17:33


















        up vote
        0
        down vote













        Just for information. If you need to login in a simple container that is not a daemon, you need to use the following commands:



        docker start {id}
        docker attach {id}





        share|improve this answer




























          up vote
          -1
          down vote













          if the container is stopped like for example a data-only container then a good solution is to run a throwaway container every time you want to attach to the data container. In this case the data container itself could be entirely empty, as the temporary container would have the OS tools.



          $ docker run --rm --volumes-from mydata -it ubuntu bash
          root@645045d3cc87:/# ls /mydata
          root@645045d3cc87:/# touch /mydata/foo
          root@645045d3cc87:/# exit
          exit





          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f505506%2fhow-to-get-bash-or-ssh-into-a-running-container-in-background-mode%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            15 Answers
            15






            active

            oldest

            votes








            15 Answers
            15






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1219
            down vote



            accepted










            The answer is Docker's attach command. So for my example above, the solution will be:



            $ sudo docker attach 665b4a1e17b6 #by ID
            or
            $ sudo docker attach loving_heisenberg #by Name
            $ root@665b4a1e17b6:/#


            For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container's shell. If we use attach we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container's shell, we just need to run the following:



            $ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID


            or



            $ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name
            $ root@665b4a1e17b6:/#





            share|improve this answer



















            • 5




              Alternatively, execute sudo docker attach loving_heisenberg
              – thiagowfx
              Sep 21 '14 at 19:59








            • 39




              the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
              – Mo J. Mughrabi
              Oct 21 '14 at 8:32






            • 10




              A reminder for boot2docker users: remove sudo :)
              – Henno
              Dec 29 '14 at 13:56






            • 12




              -i -t equals -it
              – pasha.zhukov
              Aug 12 '16 at 7:57








            • 34




              This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
              – fwc
              May 10 '17 at 18:27















            up vote
            1219
            down vote



            accepted










            The answer is Docker's attach command. So for my example above, the solution will be:



            $ sudo docker attach 665b4a1e17b6 #by ID
            or
            $ sudo docker attach loving_heisenberg #by Name
            $ root@665b4a1e17b6:/#


            For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container's shell. If we use attach we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container's shell, we just need to run the following:



            $ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID


            or



            $ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name
            $ root@665b4a1e17b6:/#





            share|improve this answer



















            • 5




              Alternatively, execute sudo docker attach loving_heisenberg
              – thiagowfx
              Sep 21 '14 at 19:59








            • 39




              the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
              – Mo J. Mughrabi
              Oct 21 '14 at 8:32






            • 10




              A reminder for boot2docker users: remove sudo :)
              – Henno
              Dec 29 '14 at 13:56






            • 12




              -i -t equals -it
              – pasha.zhukov
              Aug 12 '16 at 7:57








            • 34




              This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
              – fwc
              May 10 '17 at 18:27













            up vote
            1219
            down vote



            accepted







            up vote
            1219
            down vote



            accepted






            The answer is Docker's attach command. So for my example above, the solution will be:



            $ sudo docker attach 665b4a1e17b6 #by ID
            or
            $ sudo docker attach loving_heisenberg #by Name
            $ root@665b4a1e17b6:/#


            For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container's shell. If we use attach we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container's shell, we just need to run the following:



            $ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID


            or



            $ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name
            $ root@665b4a1e17b6:/#





            share|improve this answer














            The answer is Docker's attach command. So for my example above, the solution will be:



            $ sudo docker attach 665b4a1e17b6 #by ID
            or
            $ sudo docker attach loving_heisenberg #by Name
            $ root@665b4a1e17b6:/#


            For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container's shell. If we use attach we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container's shell, we just need to run the following:



            $ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID


            or



            $ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name
            $ root@665b4a1e17b6:/#






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 5 at 14:05









            Peter Mortensen

            1,03821016




            1,03821016










            answered Aug 5 '14 at 6:01









            Timur Fayzrakhmanov

            17k81933




            17k81933








            • 5




              Alternatively, execute sudo docker attach loving_heisenberg
              – thiagowfx
              Sep 21 '14 at 19:59








            • 39




              the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
              – Mo J. Mughrabi
              Oct 21 '14 at 8:32






            • 10




              A reminder for boot2docker users: remove sudo :)
              – Henno
              Dec 29 '14 at 13:56






            • 12




              -i -t equals -it
              – pasha.zhukov
              Aug 12 '16 at 7:57








            • 34




              This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
              – fwc
              May 10 '17 at 18:27














            • 5




              Alternatively, execute sudo docker attach loving_heisenberg
              – thiagowfx
              Sep 21 '14 at 19:59








            • 39




              the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
              – Mo J. Mughrabi
              Oct 21 '14 at 8:32






            • 10




              A reminder for boot2docker users: remove sudo :)
              – Henno
              Dec 29 '14 at 13:56






            • 12




              -i -t equals -it
              – pasha.zhukov
              Aug 12 '16 at 7:57








            • 34




              This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
              – fwc
              May 10 '17 at 18:27








            5




            5




            Alternatively, execute sudo docker attach loving_heisenberg
            – thiagowfx
            Sep 21 '14 at 19:59






            Alternatively, execute sudo docker attach loving_heisenberg
            – thiagowfx
            Sep 21 '14 at 19:59






            39




            39




            the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
            – Mo J. Mughrabi
            Oct 21 '14 at 8:32




            the attach command does not work for me, it causes the docker to freeze.. any ideas why its happening?
            – Mo J. Mughrabi
            Oct 21 '14 at 8:32




            10




            10




            A reminder for boot2docker users: remove sudo :)
            – Henno
            Dec 29 '14 at 13:56




            A reminder for boot2docker users: remove sudo :)
            – Henno
            Dec 29 '14 at 13:56




            12




            12




            -i -t equals -it
            – pasha.zhukov
            Aug 12 '16 at 7:57






            -i -t equals -it
            – pasha.zhukov
            Aug 12 '16 at 7:57






            34




            34




            This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
            – fwc
            May 10 '17 at 18:27




            This is a dangerous answer to be selected and so highly voted. docker attaching to a MongoDB instance, for example, will kill the instance. As explained in more detail in this question attach and exec are different animals.
            – fwc
            May 10 '17 at 18:27












            up vote
            608
            down vote













            From Docker 1.3 onwards:



            docker exec -it <containerIdOrName> bash


            Basically, if the Docker container was started using the /bin/bash command you can access it using attach. If not, then you need to execute the command to create a Bash instance inside the container using exec.



            Also to exit Bash without leaving Bash running in a rogue process:



            exit


            Yep, it is that simple.






            share|improve this answer























            • still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
              – WiR3D
              Oct 29 '14 at 13:46










            • Is there any way to set bash by default in dockers?
              – ipeacocks
              Apr 6 '15 at 13:13










            • @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
              – WiR3D
              Apr 7 '15 at 7:43






            • 9




              Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
              – Maiku Mori
              Mar 7 '16 at 17:48






            • 1




              I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
              – nobar
              Aug 11 '17 at 21:39















            up vote
            608
            down vote













            From Docker 1.3 onwards:



            docker exec -it <containerIdOrName> bash


            Basically, if the Docker container was started using the /bin/bash command you can access it using attach. If not, then you need to execute the command to create a Bash instance inside the container using exec.



            Also to exit Bash without leaving Bash running in a rogue process:



            exit


            Yep, it is that simple.






            share|improve this answer























            • still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
              – WiR3D
              Oct 29 '14 at 13:46










            • Is there any way to set bash by default in dockers?
              – ipeacocks
              Apr 6 '15 at 13:13










            • @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
              – WiR3D
              Apr 7 '15 at 7:43






            • 9




              Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
              – Maiku Mori
              Mar 7 '16 at 17:48






            • 1




              I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
              – nobar
              Aug 11 '17 at 21:39













            up vote
            608
            down vote










            up vote
            608
            down vote









            From Docker 1.3 onwards:



            docker exec -it <containerIdOrName> bash


            Basically, if the Docker container was started using the /bin/bash command you can access it using attach. If not, then you need to execute the command to create a Bash instance inside the container using exec.



            Also to exit Bash without leaving Bash running in a rogue process:



            exit


            Yep, it is that simple.






            share|improve this answer














            From Docker 1.3 onwards:



            docker exec -it <containerIdOrName> bash


            Basically, if the Docker container was started using the /bin/bash command you can access it using attach. If not, then you need to execute the command to create a Bash instance inside the container using exec.



            Also to exit Bash without leaving Bash running in a rogue process:



            exit


            Yep, it is that simple.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 6 at 12:46

























            answered Oct 29 '14 at 7:46









            WiR3D

            6,181164




            6,181164












            • still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
              – WiR3D
              Oct 29 '14 at 13:46










            • Is there any way to set bash by default in dockers?
              – ipeacocks
              Apr 6 '15 at 13:13










            • @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
              – WiR3D
              Apr 7 '15 at 7:43






            • 9




              Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
              – Maiku Mori
              Mar 7 '16 at 17:48






            • 1




              I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
              – nobar
              Aug 11 '17 at 21:39


















            • still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
              – WiR3D
              Oct 29 '14 at 13:46










            • Is there any way to set bash by default in dockers?
              – ipeacocks
              Apr 6 '15 at 13:13










            • @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
              – WiR3D
              Apr 7 '15 at 7:43






            • 9




              Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
              – Maiku Mori
              Mar 7 '16 at 17:48






            • 1




              I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
              – nobar
              Aug 11 '17 at 21:39
















            still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
            – WiR3D
            Oct 29 '14 at 13:46




            still havent figured out how to get nano to work. THink that may involve docker-ssh from phusion
            – WiR3D
            Oct 29 '14 at 13:46












            Is there any way to set bash by default in dockers?
            – ipeacocks
            Apr 6 '15 at 13:13




            Is there any way to set bash by default in dockers?
            – ipeacocks
            Apr 6 '15 at 13:13












            @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
            – WiR3D
            Apr 7 '15 at 7:43




            @ipeacocks yes, if the RUN command in the dockerfile is /bin/bash. But depends what you mean. If you want to run the container and have bash available immediately in that same terminal then running with -it should do it
            – WiR3D
            Apr 7 '15 at 7:43




            9




            9




            Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
            – Maiku Mori
            Mar 7 '16 at 17:48




            Using docker group is bad practice. Any user which is in docker group is essentially used with root permissions without the need to use sudo. projectatomic.io/blog/2015/08/…
            – Maiku Mori
            Mar 7 '16 at 17:48




            1




            1




            I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
            – nobar
            Aug 11 '17 at 21:39




            I think it doesn't make much difference, from a host security standpoint, whether you use sudo vs docker group. Either way, there is a security hole built into docker which can provide full privileges in the host file system from the guest -- regardless of whether you use the docker group or sudo to launch the container.
            – nobar
            Aug 11 '17 at 21:39










            up vote
            105
            down vote













            Although the author of the question specifically said they are interested in a running container, it's also worth noting that if the container is not running, but you'd like to run it to poke around you can run:



            docker run -i -t --entrypoint /bin/bash <imageID>






            share|improve this answer



















            • 6




              That gives a different container, just like @kraxor's answer.
              – Blaisorblade
              Sep 17 '15 at 15:39















            up vote
            105
            down vote













            Although the author of the question specifically said they are interested in a running container, it's also worth noting that if the container is not running, but you'd like to run it to poke around you can run:



            docker run -i -t --entrypoint /bin/bash <imageID>






            share|improve this answer



















            • 6




              That gives a different container, just like @kraxor's answer.
              – Blaisorblade
              Sep 17 '15 at 15:39













            up vote
            105
            down vote










            up vote
            105
            down vote









            Although the author of the question specifically said they are interested in a running container, it's also worth noting that if the container is not running, but you'd like to run it to poke around you can run:



            docker run -i -t --entrypoint /bin/bash <imageID>






            share|improve this answer














            Although the author of the question specifically said they are interested in a running container, it's also worth noting that if the container is not running, but you'd like to run it to poke around you can run:



            docker run -i -t --entrypoint /bin/bash <imageID>







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 7 '14 at 20:02

























            answered Nov 7 '14 at 18:46









            Adam Kalnas

            1,151175




            1,151175








            • 6




              That gives a different container, just like @kraxor's answer.
              – Blaisorblade
              Sep 17 '15 at 15:39














            • 6




              That gives a different container, just like @kraxor's answer.
              – Blaisorblade
              Sep 17 '15 at 15:39








            6




            6




            That gives a different container, just like @kraxor's answer.
            – Blaisorblade
            Sep 17 '15 at 15:39




            That gives a different container, just like @kraxor's answer.
            – Blaisorblade
            Sep 17 '15 at 15:39










            up vote
            26
            down vote













            Try this:



            sudo docker run -i -t webserver /bin/bash


            Source: https://docs.docker.com/articles/basics/#running-an-interactive-shell






            share|improve this answer

















            • 13




              It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
              – Timur Fayzrakhmanov
              Aug 1 '14 at 16:31










            • link is dead, you should update
              – Ahmet Karakaya
              Apr 15 at 15:04















            up vote
            26
            down vote













            Try this:



            sudo docker run -i -t webserver /bin/bash


            Source: https://docs.docker.com/articles/basics/#running-an-interactive-shell






            share|improve this answer

















            • 13




              It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
              – Timur Fayzrakhmanov
              Aug 1 '14 at 16:31










            • link is dead, you should update
              – Ahmet Karakaya
              Apr 15 at 15:04













            up vote
            26
            down vote










            up vote
            26
            down vote









            Try this:



            sudo docker run -i -t webserver /bin/bash


            Source: https://docs.docker.com/articles/basics/#running-an-interactive-shell






            share|improve this answer












            Try this:



            sudo docker run -i -t webserver /bin/bash


            Source: https://docs.docker.com/articles/basics/#running-an-interactive-shell







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 1 '14 at 16:29









            kraxor

            4,24431832




            4,24431832








            • 13




              It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
              – Timur Fayzrakhmanov
              Aug 1 '14 at 16:31










            • link is dead, you should update
              – Ahmet Karakaya
              Apr 15 at 15:04














            • 13




              It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
              – Timur Fayzrakhmanov
              Aug 1 '14 at 16:31










            • link is dead, you should update
              – Ahmet Karakaya
              Apr 15 at 15:04








            13




            13




            It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
            – Timur Fayzrakhmanov
            Aug 1 '14 at 16:31




            It is not suitable, because I get different CONTAINER ID (root@42f1e37bd0e5:/# and not root@665b4a1e17b6:/#)
            – Timur Fayzrakhmanov
            Aug 1 '14 at 16:31












            link is dead, you should update
            – Ahmet Karakaya
            Apr 15 at 15:04




            link is dead, you should update
            – Ahmet Karakaya
            Apr 15 at 15:04










            up vote
            15
            down vote













            Based on @Timur's answer I've created the following handy script



            Setup



            Put docker-ssh file in your $PATH with the following contents



            #!/bin/bash -xe

            # docker container id or name might be given as a parameter
            CONTAINER=$1

            if [[ "$CONTAINER" == "" ]]; then
            # if no id given simply just connect to the first running container
            CONTAINER=$(docker ps | grep -Eo "^[0-9a-z]{8,}b")
            fi

            # start an interactive bash inside the container
            # note some containers don't have bash, then try: ash (alpine), or simply sh
            # the -l at the end stands for login shell that reads profile files (read man)
            docker exec -i -t $CONTAINER bash -l


            Note: Some container do not contain bash, but ash, sh etc. In these cases bash shall be replaced in the above script.



            Usage



            If you have one running instance simply run



            $> docker-ssh 


            Otherwise provide it with a docker id parmeter that you get from docker ps (first col)



            $> docker-ssh 50m3r4nd0m1d





            share|improve this answer























            • May I know why we need -l at the end?
              – Nam G VU
              Nov 24 '17 at 6:46










            • to start bash as a login shell, reading the environment parameters ( described in the line above the command)
              – Matyas
              Nov 24 '17 at 11:15















            up vote
            15
            down vote













            Based on @Timur's answer I've created the following handy script



            Setup



            Put docker-ssh file in your $PATH with the following contents



            #!/bin/bash -xe

            # docker container id or name might be given as a parameter
            CONTAINER=$1

            if [[ "$CONTAINER" == "" ]]; then
            # if no id given simply just connect to the first running container
            CONTAINER=$(docker ps | grep -Eo "^[0-9a-z]{8,}b")
            fi

            # start an interactive bash inside the container
            # note some containers don't have bash, then try: ash (alpine), or simply sh
            # the -l at the end stands for login shell that reads profile files (read man)
            docker exec -i -t $CONTAINER bash -l


            Note: Some container do not contain bash, but ash, sh etc. In these cases bash shall be replaced in the above script.



            Usage



            If you have one running instance simply run



            $> docker-ssh 


            Otherwise provide it with a docker id parmeter that you get from docker ps (first col)



            $> docker-ssh 50m3r4nd0m1d





            share|improve this answer























            • May I know why we need -l at the end?
              – Nam G VU
              Nov 24 '17 at 6:46










            • to start bash as a login shell, reading the environment parameters ( described in the line above the command)
              – Matyas
              Nov 24 '17 at 11:15













            up vote
            15
            down vote










            up vote
            15
            down vote









            Based on @Timur's answer I've created the following handy script



            Setup



            Put docker-ssh file in your $PATH with the following contents



            #!/bin/bash -xe

            # docker container id or name might be given as a parameter
            CONTAINER=$1

            if [[ "$CONTAINER" == "" ]]; then
            # if no id given simply just connect to the first running container
            CONTAINER=$(docker ps | grep -Eo "^[0-9a-z]{8,}b")
            fi

            # start an interactive bash inside the container
            # note some containers don't have bash, then try: ash (alpine), or simply sh
            # the -l at the end stands for login shell that reads profile files (read man)
            docker exec -i -t $CONTAINER bash -l


            Note: Some container do not contain bash, but ash, sh etc. In these cases bash shall be replaced in the above script.



            Usage



            If you have one running instance simply run



            $> docker-ssh 


            Otherwise provide it with a docker id parmeter that you get from docker ps (first col)



            $> docker-ssh 50m3r4nd0m1d





            share|improve this answer














            Based on @Timur's answer I've created the following handy script



            Setup



            Put docker-ssh file in your $PATH with the following contents



            #!/bin/bash -xe

            # docker container id or name might be given as a parameter
            CONTAINER=$1

            if [[ "$CONTAINER" == "" ]]; then
            # if no id given simply just connect to the first running container
            CONTAINER=$(docker ps | grep -Eo "^[0-9a-z]{8,}b")
            fi

            # start an interactive bash inside the container
            # note some containers don't have bash, then try: ash (alpine), or simply sh
            # the -l at the end stands for login shell that reads profile files (read man)
            docker exec -i -t $CONTAINER bash -l


            Note: Some container do not contain bash, but ash, sh etc. In these cases bash shall be replaced in the above script.



            Usage



            If you have one running instance simply run



            $> docker-ssh 


            Otherwise provide it with a docker id parmeter that you get from docker ps (first col)



            $> docker-ssh 50m3r4nd0m1d






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '17 at 11:14

























            answered Apr 20 '16 at 11:39









            Matyas

            43149




            43149












            • May I know why we need -l at the end?
              – Nam G VU
              Nov 24 '17 at 6:46










            • to start bash as a login shell, reading the environment parameters ( described in the line above the command)
              – Matyas
              Nov 24 '17 at 11:15


















            • May I know why we need -l at the end?
              – Nam G VU
              Nov 24 '17 at 6:46










            • to start bash as a login shell, reading the environment parameters ( described in the line above the command)
              – Matyas
              Nov 24 '17 at 11:15
















            May I know why we need -l at the end?
            – Nam G VU
            Nov 24 '17 at 6:46




            May I know why we need -l at the end?
            – Nam G VU
            Nov 24 '17 at 6:46












            to start bash as a login shell, reading the environment parameters ( described in the line above the command)
            – Matyas
            Nov 24 '17 at 11:15




            to start bash as a login shell, reading the environment parameters ( described in the line above the command)
            – Matyas
            Nov 24 '17 at 11:15










            up vote
            8
            down vote













            I've created a containerized SSH server that provides SSH capabilities to any running container. You don't need to change your container. The only requirement is that the container has bash.



            If you have a container with name 'web-server1'. The following docker run command would start a second container that would provide SSH for the first container.



            docker run -ti --name sshd-web-server1 -e CONTAINER=web-server1 -p 2222:22 
            -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker
            jeroenpeeters/docker-ssh


            For more pointers, checkout https://github.com/jeroenpeeters/docker-ssh






            share|improve this answer





















            • This should be the accepted answer ^
              – Nam G VU
              Jul 8 '17 at 6:03










            • By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
              – Nam G VU
              Jul 8 '17 at 6:05

















            up vote
            8
            down vote













            I've created a containerized SSH server that provides SSH capabilities to any running container. You don't need to change your container. The only requirement is that the container has bash.



            If you have a container with name 'web-server1'. The following docker run command would start a second container that would provide SSH for the first container.



            docker run -ti --name sshd-web-server1 -e CONTAINER=web-server1 -p 2222:22 
            -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker
            jeroenpeeters/docker-ssh


            For more pointers, checkout https://github.com/jeroenpeeters/docker-ssh






            share|improve this answer





















            • This should be the accepted answer ^
              – Nam G VU
              Jul 8 '17 at 6:03










            • By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
              – Nam G VU
              Jul 8 '17 at 6:05















            up vote
            8
            down vote










            up vote
            8
            down vote









            I've created a containerized SSH server that provides SSH capabilities to any running container. You don't need to change your container. The only requirement is that the container has bash.



            If you have a container with name 'web-server1'. The following docker run command would start a second container that would provide SSH for the first container.



            docker run -ti --name sshd-web-server1 -e CONTAINER=web-server1 -p 2222:22 
            -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker
            jeroenpeeters/docker-ssh


            For more pointers, checkout https://github.com/jeroenpeeters/docker-ssh






            share|improve this answer












            I've created a containerized SSH server that provides SSH capabilities to any running container. You don't need to change your container. The only requirement is that the container has bash.



            If you have a container with name 'web-server1'. The following docker run command would start a second container that would provide SSH for the first container.



            docker run -ti --name sshd-web-server1 -e CONTAINER=web-server1 -p 2222:22 
            -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker
            jeroenpeeters/docker-ssh


            For more pointers, checkout https://github.com/jeroenpeeters/docker-ssh







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 3 '15 at 16:20









            Jeroen Peeters

            18913




            18913












            • This should be the accepted answer ^
              – Nam G VU
              Jul 8 '17 at 6:03










            • By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
              – Nam G VU
              Jul 8 '17 at 6:05




















            • This should be the accepted answer ^
              – Nam G VU
              Jul 8 '17 at 6:03










            • By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
              – Nam G VU
              Jul 8 '17 at 6:05


















            This should be the accepted answer ^
            – Nam G VU
            Jul 8 '17 at 6:03




            This should be the accepted answer ^
            – Nam G VU
            Jul 8 '17 at 6:03












            By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
            – Nam G VU
            Jul 8 '17 at 6:05






            By the way, how can we have .bashrc auto loaded when starting a ssh session using your solution? Also posted an issue on github github.com/jeroenpeeters/docker-ssh/issues/30
            – Nam G VU
            Jul 8 '17 at 6:05












            up vote
            7
            down vote













            If your container doesn't have bash installed you could try sh:



            docker exec -it CONTAINER /bin/sh


            Or look for shells in /bin first:



            docker export CONTAINER|tar -t|egrep ^bin/





            share|improve this answer























            • What is "consul"? Do you have a reference for it? Do you mean "console"?
              – Peter Mortensen
              Aug 5 at 13:28















            up vote
            7
            down vote













            If your container doesn't have bash installed you could try sh:



            docker exec -it CONTAINER /bin/sh


            Or look for shells in /bin first:



            docker export CONTAINER|tar -t|egrep ^bin/





            share|improve this answer























            • What is "consul"? Do you have a reference for it? Do you mean "console"?
              – Peter Mortensen
              Aug 5 at 13:28













            up vote
            7
            down vote










            up vote
            7
            down vote









            If your container doesn't have bash installed you could try sh:



            docker exec -it CONTAINER /bin/sh


            Or look for shells in /bin first:



            docker export CONTAINER|tar -t|egrep ^bin/





            share|improve this answer














            If your container doesn't have bash installed you could try sh:



            docker exec -it CONTAINER /bin/sh


            Or look for shells in /bin first:



            docker export CONTAINER|tar -t|egrep ^bin/






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 at 15:36









            JasonPlutext

            1255




            1255










            answered Jul 29 '15 at 13:40









            laktak

            3211410




            3211410












            • What is "consul"? Do you have a reference for it? Do you mean "console"?
              – Peter Mortensen
              Aug 5 at 13:28


















            • What is "consul"? Do you have a reference for it? Do you mean "console"?
              – Peter Mortensen
              Aug 5 at 13:28
















            What is "consul"? Do you have a reference for it? Do you mean "console"?
            – Peter Mortensen
            Aug 5 at 13:28




            What is "consul"? Do you have a reference for it? Do you mean "console"?
            – Peter Mortensen
            Aug 5 at 13:28










            up vote
            5
            down vote













            @jpetazzo has an awesome post about this subject. The short answer would be to use nsenter:



            PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
            nsenter --target $PID --mount --uts --ipc --net --pid


            P.S.: Don't forget to check the discussion in the comments of the post...



            Cheers






            share|improve this answer



















            • 1




              That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
              – drevicko
              Mar 10 '17 at 15:46















            up vote
            5
            down vote













            @jpetazzo has an awesome post about this subject. The short answer would be to use nsenter:



            PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
            nsenter --target $PID --mount --uts --ipc --net --pid


            P.S.: Don't forget to check the discussion in the comments of the post...



            Cheers






            share|improve this answer



















            • 1




              That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
              – drevicko
              Mar 10 '17 at 15:46













            up vote
            5
            down vote










            up vote
            5
            down vote









            @jpetazzo has an awesome post about this subject. The short answer would be to use nsenter:



            PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
            nsenter --target $PID --mount --uts --ipc --net --pid


            P.S.: Don't forget to check the discussion in the comments of the post...



            Cheers






            share|improve this answer














            @jpetazzo has an awesome post about this subject. The short answer would be to use nsenter:



            PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
            nsenter --target $PID --mount --uts --ipc --net --pid


            P.S.: Don't forget to check the discussion in the comments of the post...



            Cheers







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 10 '17 at 17:46









            A.L

            199213




            199213










            answered Mar 14 '15 at 10:12









            Richard

            5111




            5111








            • 1




              That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
              – drevicko
              Mar 10 '17 at 15:46














            • 1




              That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
              – drevicko
              Mar 10 '17 at 15:46








            1




            1




            That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
            – drevicko
            Mar 10 '17 at 15:46




            That's a rather old post that is no longer really necessary. @WiR3D's docker exec solution is rather more convenient.
            – drevicko
            Mar 10 '17 at 15:46










            up vote
            3
            down vote













            You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.



            This will be more "traditional" (ssh), instead of using an application-specific command like docker attach, and will eventually make it more 'portable' across systems and versions.






            share|improve this answer























            • Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:08








            • 2




              There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
              – radriaanse
              Oct 29 '14 at 12:30










            • You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:57















            up vote
            3
            down vote













            You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.



            This will be more "traditional" (ssh), instead of using an application-specific command like docker attach, and will eventually make it more 'portable' across systems and versions.






            share|improve this answer























            • Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:08








            • 2




              There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
              – radriaanse
              Oct 29 '14 at 12:30










            • You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:57













            up vote
            3
            down vote










            up vote
            3
            down vote









            You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.



            This will be more "traditional" (ssh), instead of using an application-specific command like docker attach, and will eventually make it more 'portable' across systems and versions.






            share|improve this answer














            You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.



            This will be more "traditional" (ssh), instead of using an application-specific command like docker attach, and will eventually make it more 'portable' across systems and versions.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 5 at 14:17









            Peter Mortensen

            1,03821016




            1,03821016










            answered Oct 29 '14 at 11:58









            radriaanse

            441214




            441214












            • Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:08








            • 2




              There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
              – radriaanse
              Oct 29 '14 at 12:30










            • You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:57


















            • Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:08








            • 2




              There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
              – radriaanse
              Oct 29 '14 at 12:30










            • You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
              – Timur Fayzrakhmanov
              Oct 29 '14 at 12:57
















            Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
            – Timur Fayzrakhmanov
            Oct 29 '14 at 12:08






            Please, add the simples way how to do it. If to be honest, I really need it, but I have no time to search the simplest solution for that. Could you post your answer here? It would be great..
            – Timur Fayzrakhmanov
            Oct 29 '14 at 12:08






            2




            2




            There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
            – radriaanse
            Oct 29 '14 at 12:30




            There are 2 ways of accomplishing this, but it isn't simple, and would become a large post. You could check out this link by yourself, for using pipework or this link, witch essentially accomplishes the same as Pipework and is a bit more simple, but you need to do it manually. So it depends about how many servers where talking. If you can't figure out something more specific, let me know. But i also don't have the time to write an full tutorial.
            – radriaanse
            Oct 29 '14 at 12:30












            You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
            – Timur Fayzrakhmanov
            Oct 29 '14 at 12:57




            You are right - there is no obvious and simple way to do it( Thanks for links, I think I will revisit it later.
            – Timur Fayzrakhmanov
            Oct 29 '14 at 12:57










            up vote
            2
            down vote













            docker run -it openjdk:8


            This works :-)






            share|improve this answer



























              up vote
              2
              down vote













              docker run -it openjdk:8


              This works :-)






              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                docker run -it openjdk:8


                This works :-)






                share|improve this answer














                docker run -it openjdk:8


                This works :-)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 6 '16 at 18:36









                edwinksl

                16.4k115385




                16.4k115385










                answered Sep 6 '16 at 15:11









                Kishan B

                1212




                1212






















                    up vote
                    2
                    down vote













                    Sometimes it will be handy to be able to ssh into a Docker container, especially during development. The following Docker image allows to ssh into a container using a private key:



                    UbuntuWithSSH-Docker



                    The gist of the Dockerfile is https://gist.github.com/devbkhadka/98792f7bca57f9778793b2db758b3d07.






                    share|improve this answer



























                      up vote
                      2
                      down vote













                      Sometimes it will be handy to be able to ssh into a Docker container, especially during development. The following Docker image allows to ssh into a container using a private key:



                      UbuntuWithSSH-Docker



                      The gist of the Dockerfile is https://gist.github.com/devbkhadka/98792f7bca57f9778793b2db758b3d07.






                      share|improve this answer

























                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        Sometimes it will be handy to be able to ssh into a Docker container, especially during development. The following Docker image allows to ssh into a container using a private key:



                        UbuntuWithSSH-Docker



                        The gist of the Dockerfile is https://gist.github.com/devbkhadka/98792f7bca57f9778793b2db758b3d07.






                        share|improve this answer














                        Sometimes it will be handy to be able to ssh into a Docker container, especially during development. The following Docker image allows to ssh into a container using a private key:



                        UbuntuWithSSH-Docker



                        The gist of the Dockerfile is https://gist.github.com/devbkhadka/98792f7bca57f9778793b2db758b3d07.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 5 at 14:14









                        Peter Mortensen

                        1,03821016




                        1,03821016










                        answered Aug 26 '17 at 15:26









                        Dev Khadka

                        212




                        212






















                            up vote
                            1
                            down vote













                            GOINSIDE



                            install goinside command line tool with:



                            sudo npm install -g goinside


                            and go inside a docker container with a proper terminal size with:



                            goinside docker_container_name




                            for more details check this out.






                            share|improve this answer



























                              up vote
                              1
                              down vote













                              GOINSIDE



                              install goinside command line tool with:



                              sudo npm install -g goinside


                              and go inside a docker container with a proper terminal size with:



                              goinside docker_container_name




                              for more details check this out.






                              share|improve this answer

























                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                GOINSIDE



                                install goinside command line tool with:



                                sudo npm install -g goinside


                                and go inside a docker container with a proper terminal size with:



                                goinside docker_container_name




                                for more details check this out.






                                share|improve this answer














                                GOINSIDE



                                install goinside command line tool with:



                                sudo npm install -g goinside


                                and go inside a docker container with a proper terminal size with:



                                goinside docker_container_name




                                for more details check this out.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Aug 13 at 10:01

























                                answered Aug 13 at 9:56









                                Soorena

                                1214




                                1214






















                                    up vote
                                    0
                                    down vote













                                    To bash into a running container, type this:



                                    docker exec -t -i container_name /bin/bash





                                    share|improve this answer

















                                    • 1




                                      this is the same answer as @AdamKalnas though
                                      – Bruni
                                      Sep 6 '16 at 17:33















                                    up vote
                                    0
                                    down vote













                                    To bash into a running container, type this:



                                    docker exec -t -i container_name /bin/bash





                                    share|improve this answer

















                                    • 1




                                      this is the same answer as @AdamKalnas though
                                      – Bruni
                                      Sep 6 '16 at 17:33













                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    To bash into a running container, type this:



                                    docker exec -t -i container_name /bin/bash





                                    share|improve this answer












                                    To bash into a running container, type this:



                                    docker exec -t -i container_name /bin/bash






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Oct 11 '15 at 1:33









                                    Agustí Sánchez

                                    1113




                                    1113








                                    • 1




                                      this is the same answer as @AdamKalnas though
                                      – Bruni
                                      Sep 6 '16 at 17:33














                                    • 1




                                      this is the same answer as @AdamKalnas though
                                      – Bruni
                                      Sep 6 '16 at 17:33








                                    1




                                    1




                                    this is the same answer as @AdamKalnas though
                                    – Bruni
                                    Sep 6 '16 at 17:33




                                    this is the same answer as @AdamKalnas though
                                    – Bruni
                                    Sep 6 '16 at 17:33










                                    up vote
                                    0
                                    down vote













                                    Just for information. If you need to login in a simple container that is not a daemon, you need to use the following commands:



                                    docker start {id}
                                    docker attach {id}





                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote













                                      Just for information. If you need to login in a simple container that is not a daemon, you need to use the following commands:



                                      docker start {id}
                                      docker attach {id}





                                      share|improve this answer























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        Just for information. If you need to login in a simple container that is not a daemon, you need to use the following commands:



                                        docker start {id}
                                        docker attach {id}





                                        share|improve this answer












                                        Just for information. If you need to login in a simple container that is not a daemon, you need to use the following commands:



                                        docker start {id}
                                        docker attach {id}






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 4 '15 at 21:05









                                        Nek

                                        171210




                                        171210






















                                            up vote
                                            -1
                                            down vote













                                            if the container is stopped like for example a data-only container then a good solution is to run a throwaway container every time you want to attach to the data container. In this case the data container itself could be entirely empty, as the temporary container would have the OS tools.



                                            $ docker run --rm --volumes-from mydata -it ubuntu bash
                                            root@645045d3cc87:/# ls /mydata
                                            root@645045d3cc87:/# touch /mydata/foo
                                            root@645045d3cc87:/# exit
                                            exit





                                            share|improve this answer

























                                              up vote
                                              -1
                                              down vote













                                              if the container is stopped like for example a data-only container then a good solution is to run a throwaway container every time you want to attach to the data container. In this case the data container itself could be entirely empty, as the temporary container would have the OS tools.



                                              $ docker run --rm --volumes-from mydata -it ubuntu bash
                                              root@645045d3cc87:/# ls /mydata
                                              root@645045d3cc87:/# touch /mydata/foo
                                              root@645045d3cc87:/# exit
                                              exit





                                              share|improve this answer























                                                up vote
                                                -1
                                                down vote










                                                up vote
                                                -1
                                                down vote









                                                if the container is stopped like for example a data-only container then a good solution is to run a throwaway container every time you want to attach to the data container. In this case the data container itself could be entirely empty, as the temporary container would have the OS tools.



                                                $ docker run --rm --volumes-from mydata -it ubuntu bash
                                                root@645045d3cc87:/# ls /mydata
                                                root@645045d3cc87:/# touch /mydata/foo
                                                root@645045d3cc87:/# exit
                                                exit





                                                share|improve this answer












                                                if the container is stopped like for example a data-only container then a good solution is to run a throwaway container every time you want to attach to the data container. In this case the data container itself could be entirely empty, as the temporary container would have the OS tools.



                                                $ docker run --rm --volumes-from mydata -it ubuntu bash
                                                root@645045d3cc87:/# ls /mydata
                                                root@645045d3cc87:/# touch /mydata/foo
                                                root@645045d3cc87:/# exit
                                                exit






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Oct 14 '15 at 10:32









                                                David Dehghan

                                                991




                                                991






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Ask Ubuntu!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f505506%2fhow-to-get-bash-or-ssh-into-a-running-container-in-background-mode%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                                    Mangá

                                                     ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕