How do I enter this dockerfile / nginx container?

24,783

Solution 1

In my case the standard bash didn't exist. Using /bin/sh helped me:

docker run -it -p 80:80 dockerfile/nginx /bin/sh

Solution 2

UPDATE (a much easier method that was introduced later):

docker exec -t -i container_name /bin/bash

Original answer

Actually you can access a running container too.

Find your container's ID:

docker ps

Export the ID of the process that runs the container:

PID=$(docker inspect --format '{{.State.Pid}}' my_container_id)

"Connect" to it by changing namespaces:

nsenter --target $PID --mount --uts --ipc --net --pid

Originally this was described here: http://jpetazzo.github.io/2014/03/23/lxc-attach-nsinit-nsenter-docker-0-9/

Solution 3

First make sure to understand the difference between images and containers. Running the image:

docker run -d -p 80:80 dockerfile/nginx

creates a new container executing only nginx. This process does not interact like a shell. If you really need access to the files in this container while its running, your only option is to use nsinit, nsenter or lxc-attach. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for details.

Alternatively, you might want to try

docker run -it -p 80:80 dockerfile/nginx /bin/bash

which creates a new container executing an interactive shell instead of nginx.

Share:
24,783

Related videos on Youtube

peruvian
Author by

peruvian

Updated on January 09, 2020

Comments

  • peruvian
    peruvian over 4 years

    With centos in a docker container, I just type 'docker attach container ID' and it takes me to the shell prompt, where i can install and configure nginx.

    This one is easier: docker.com dockerfile/nginx You just run the file and everything is installed and configured.

    but i can't figure out how to get in and access the files.

  • peruvian
    peruvian almost 10 years
    docker run -d -p 80:80 dockerfile/nginx /bin/bash this gave me an error Error response from daemon: Cannot start container a126421fd50a64a939265019b64f47d86bc54a82f4dd4c29088da4e8d7f1‌​5c33: Bind for 0.0.0.0:80 failed: port is already allocated so I stopped the container first and tried it again. it worked. I got new container So I tried to attach to it but here's another error. You cannot attach to a stopped container, start it first I can restart but it stops immediately.
  • Andreas Steffan
    Andreas Steffan almost 10 years
    There was a typo in the anwer. For /bin/bash the command line parameters should be -it and NOT -d (daemon). Corrected that in my answer.
  • DarkSuniuM
    DarkSuniuM almost 5 years
    My docker instance was built based on busybox and it didn't have /bin/bash so I just used docker exec -t -i container_name sh and it did work